Skip to content

Commit 8191777

Browse files
committed
Use DataSourceHttpSettings
1 parent 7cb4908 commit 8191777

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

src/ConfigEditor.tsx

+28-19
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
11
import React, { ChangeEvent } from 'react';
2-
import { InlineFormLabel, Input, Legend } from '@grafana/ui';
2+
import { LegacyForms, DataSourceHttpSettings } from '@grafana/ui';
3+
34
import { DataSourcePluginOptionsEditorProps } from '@grafana/data';
45
import { JsonApiDataSourceOptions } from './types';
56
import {} from '@emotion/core';
67

8+
const { Input, FormField } = LegacyForms;
9+
710
interface Props extends DataSourcePluginOptionsEditorProps<JsonApiDataSourceOptions> {}
811

912
export const ConfigEditor: React.FC<Props> = ({ options, onOptionsChange }) => {
10-
const onUrlChange = (e: ChangeEvent<HTMLInputElement>) => {
11-
onOptionsChange({ ...options, url: e.currentTarget.value });
12-
};
1313
const onParamsChange = (e: ChangeEvent<HTMLInputElement>) => {
1414
onOptionsChange({ ...options, jsonData: { ...options.jsonData, queryParams: e.currentTarget.value } });
1515
};
16+
1617
return (
1718
<>
18-
<Legend>HTTP</Legend>
19+
<DataSourceHttpSettings
20+
defaultUrl="http://localhost:8080"
21+
dataSourceConfig={options}
22+
onChange={onOptionsChange}
23+
/>
24+
<h3 className="page-heading">Misc</h3>
1925
<div className="gf-form-group">
20-
<div className="gf-form">
21-
<InlineFormLabel tooltip={<p>URL to an endpoint that returns a JSON response.</p>}>URL</InlineFormLabel>
22-
<Input className="width-25" value={options.url} onChange={onUrlChange} placeholder="page=1&limit=100" />
23-
</div>
24-
<div className="gf-form">
25-
<InlineFormLabel tooltip={<p>Add custom query parameters to your query.</p>}>
26-
Query parameters
27-
</InlineFormLabel>
28-
<Input
29-
className="width-25"
30-
value={options.jsonData.queryParams}
31-
onChange={onParamsChange}
32-
placeholder="page=1&limit=100"
33-
/>
26+
<div className="gf-form-inline">
27+
<div className="gf-form max-width-30">
28+
<FormField
29+
label="Custom query parameters"
30+
labelWidth={14}
31+
tooltip="Add custom parameters to your queries."
32+
inputEl={
33+
<Input
34+
className="width-25"
35+
value={options.jsonData.queryParams}
36+
onChange={onParamsChange}
37+
spellCheck={false}
38+
placeholder="page=1&limit=100"
39+
/>
40+
}
41+
/>
42+
</div>
3443
</div>
3544
</div>
3645
</>

src/DataSource.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class DataSource extends DataSourceApi<JsonApiQuery, JsonApiDataSourceOpt
8080
const defaultErrorMessage = 'Cannot connect to API';
8181

8282
try {
83-
const response = await this.api.test();
83+
const response = await this.api.test(this.queryParams);
8484
if (response.status === 200) {
8585
return {
8686
status: 'success',

src/api.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ export default class Api {
2828
/**
2929
* Used as a health check.
3030
*/
31-
async test() {
32-
return getBackendSrv().datasourceRequest({
33-
url: this.baseUrl,
31+
async test(params?: string) {
32+
const req = {
33+
url: `${this.baseUrl}${params?.length ? `?${params}` : ''}`,
3434
method: 'GET',
35-
});
35+
};
36+
return getBackendSrv().datasourceRequest(req);
3637
}
3738

3839
/**

src/types.ts

-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,5 @@ export const defaultQuery: Partial<JsonApiQuery> = {
2020
};
2121

2222
export interface JsonApiDataSourceOptions extends DataSourceJsonData {
23-
url: string;
2423
queryParams?: string;
2524
}
26-
27-
export interface JsonApiSecureJsonData {}

0 commit comments

Comments
 (0)