Skip to content

Commit 89325f9

Browse files
committed
Clean up query editors
1 parent 894a59c commit 89325f9

File tree

5 files changed

+26
-49
lines changed

5 files changed

+26
-49
lines changed

src/components/DashboardQueryEditor.tsx

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/components/JsonPathQueryField.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ interface Props {
77
query: string;
88
onBlur: () => void;
99
onChange: (v: string) => void;
10-
suggestions: boolean;
1110
onData: () => Promise<any>;
1211
}
1312

1413
/**
1514
* JsonPathQueryField is an editor for JSON Path.
1615
*/
17-
export const JsonPathQueryField: React.FC<Props> = ({ query, onBlur, onChange, suggestions, onData }) => {
16+
export const JsonPathQueryField: React.FC<Props> = ({ query, onBlur, onChange, onData }) => {
1817
/**
1918
* The QueryField supports Slate plugins, so let's add a few useful ones.
2019
*/
@@ -36,7 +35,7 @@ export const JsonPathQueryField: React.FC<Props> = ({ query, onBlur, onChange, s
3635
additionalPlugins={plugins}
3736
query={query}
3837
cleanText={cleanText}
39-
onTypeahead={suggestions ? onTypeahead : undefined}
38+
onTypeahead={onTypeahead}
4039
onRunQuery={onBlur}
4140
onChange={onChange}
4241
portalOrigin="jsonapi"

src/components/QueryEditor.tsx

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,20 @@ import {
1212
useTheme,
1313
InfoBox,
1414
} from '@grafana/ui';
15-
import { SelectableValue, FieldType, TimeRange } from '@grafana/data';
16-
import { JsonApiQuery, defaultQuery } from '../types';
15+
import { SelectableValue, FieldType, QueryEditorProps } from '@grafana/data';
16+
import { JsonApiQuery, defaultQuery, JsonApiDataSourceOptions } from '../types';
1717
import { JsonPathQueryField } from './JsonPathQueryField';
1818
import { KeyValueEditor } from './KeyValueEditor';
1919
import AutoSizer from 'react-virtualized-auto-sizer';
2020
import { css } from 'emotion';
2121
import { Pair } from '../types';
2222
import { JsonDataSource } from 'datasource';
2323

24-
interface Props {
25-
onRunQuery: () => void;
26-
onChange: (query: JsonApiQuery) => void;
27-
query: JsonApiQuery;
24+
interface Props extends QueryEditorProps<JsonDataSource, JsonApiQuery, JsonApiDataSourceOptions> {
2825
limitFields?: number;
29-
datasource: JsonDataSource;
30-
range?: TimeRange;
31-
disableSuggestions: boolean;
3226
}
3327

34-
export const QueryEditor: React.FC<Props> = ({
35-
onRunQuery,
36-
onChange,
37-
query,
38-
limitFields,
39-
datasource,
40-
range,
41-
disableSuggestions,
42-
}) => {
28+
export const QueryEditor: React.FC<Props> = ({ onRunQuery, onChange, query, limitFields, datasource, range }) => {
4329
const [bodyType, setBodyType] = useState('plaintext');
4430
const [tabIndex, setTabIndex] = useState(0);
4531
const theme = useTheme();
@@ -77,31 +63,36 @@ export const QueryEditor: React.FC<Props> = ({
7763
};
7864

7965
const onChangePath = (i: number) => (e: string) => {
80-
fields[i] = { ...fields[i], jsonPath: e };
81-
onChange({ ...query, fields });
66+
onChange({
67+
...query,
68+
fields: fields.map((field, n) => (i === n ? { ...fields[i], jsonPath: e } : field)),
69+
});
8270
};
8371

8472
const onChangeType = (i: number) => (e: SelectableValue<string>) => {
85-
fields[i] = { ...fields[i], type: (e.value === 'auto' ? undefined : e.value) as FieldType };
86-
onChange({ ...query, fields });
73+
onChange({
74+
...query,
75+
fields: fields.map((field, n) =>
76+
i === n ? { ...fields[i], type: (e.value === 'auto' ? undefined : e.value) as FieldType } : field
77+
),
78+
});
8779
onRunQuery();
8880
};
8981

9082
const addField = (i: number) => () => {
91-
console.log(limitFields, fields.length);
9283
if (!limitFields || fields.length < limitFields) {
93-
if (fields) {
94-
fields.splice(i + 1, 0, { name: '', jsonPath: '' });
95-
}
96-
onChange({ ...query, fields });
84+
onChange({
85+
...query,
86+
fields: [...fields.slice(0, i + 1), { name: '', jsonPath: '' }, ...fields.slice(i + 1)],
87+
});
9788
}
9889
};
9990

10091
const removeField = (i: number) => () => {
101-
if (fields) {
102-
fields.splice(i, 1);
103-
}
104-
onChange({ ...query, fields });
92+
onChange({
93+
...query,
94+
fields: [...fields.slice(0, i), ...fields.slice(i + 1)],
95+
});
10596
onRunQuery();
10697
};
10798

@@ -125,7 +116,6 @@ export const QueryEditor: React.FC<Props> = ({
125116
onBlur={onRunQuery}
126117
onChange={onChangePath(index)}
127118
query={field.jsonPath}
128-
suggestions={!disableSuggestions}
129119
onData={() => datasource.metadataRequest(query, range)}
130120
/>
131121
</InlineField>

src/components/VariableQueryEditor.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ interface VariableQueryProps {
99
onChange: (query: JsonApiQuery, definition: string) => void;
1010
datasource: JsonDataSource;
1111
range: TimeRange;
12-
disableSuggestions: boolean;
1312
}
1413

1514
// VariableQueryEditor is used to query values for a dashboard variable.

src/module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { DataSourcePlugin } from '@grafana/data';
22
import { JsonDataSource } from './datasource';
33
import { ConfigEditor } from './components/ConfigEditor';
4-
import { DashboardQueryEditor } from './components/DashboardQueryEditor';
4+
import { QueryEditor } from './components/QueryEditor';
55
import { VariableQueryEditor } from './components/VariableQueryEditor';
66
import { JsonApiQuery, JsonApiDataSourceOptions } from './types';
77

88
export const plugin = new DataSourcePlugin<JsonDataSource, JsonApiQuery, JsonApiDataSourceOptions>(JsonDataSource)
99
.setConfigEditor(ConfigEditor)
10-
.setQueryEditor(DashboardQueryEditor)
10+
.setQueryEditor(QueryEditor)
1111
.setVariableQueryEditor(VariableQueryEditor);

0 commit comments

Comments
 (0)