Skip to content

Commit 049d99d

Browse files
refactor: Converts QueryEditor to Component class
1 parent fef0cac commit 049d99d

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

src/components/QueryEditor.tsx

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { } from 'react';
1+
import React, { PureComponent } from 'react';
22
import { Button, Form, Stack } from '@grafana/ui';
33
import { QueryEditorProps } from '@grafana/data';
44
import { DataSource } from '../datasource';
@@ -8,49 +8,50 @@ import { HaystackQueryInput } from './HaystackQueryInput';
88

99
type Props = QueryEditorProps<DataSource, HaystackQuery, HaystackDataSourceOptions>;
1010

11-
export function QueryEditor({ datasource, query, onChange, onRunQuery }: Props) {
12-
const onTypeChange = (newType: string) => {
13-
onChange({ ...query, type: newType });
11+
export class QueryEditor extends PureComponent<Props> {
12+
onTypeChange(newType: string) {
13+
this.props.onChange({ ...this.props.query, type: newType });
1414
};
15-
const onQueryChange = (newQuery: string) => {
16-
if (query.type === "eval") {
17-
onChange({ ...query, eval: newQuery });
18-
} else if (query.type === "hisRead") {
19-
onChange({ ...query, hisRead: newQuery });
20-
} else if (query.type === "hisReadFilter") {
21-
onChange({ ...query, hisReadFilter: newQuery });
22-
} else if (query.type === "read") {
23-
onChange({ ...query, read: newQuery });
15+
onQueryChange(newQuery: string) {
16+
if (this.props.query.type === "eval") {
17+
this.props.onChange({ ...this.props.query, eval: newQuery });
18+
} else if (this.props.query.type === "hisRead") {
19+
this.props.onChange({ ...this.props.query, hisRead: newQuery });
20+
} else if (this.props.query.type === "hisReadFilter") {
21+
this.props.onChange({ ...this.props.query, hisReadFilter: newQuery });
22+
} else if (this.props.query.type === "read") {
23+
this.props.onChange({ ...this.props.query, read: newQuery });
2424
}
2525
};
26-
27-
function onSubmit(newQuery: Partial<HaystackQuery>) {
28-
query = { ...query, ...newQuery };
29-
onRunQuery();
26+
onSubmit(newQuery: Partial<HaystackQuery>) {
27+
this.props.onChange({ ...this.props.query, ...newQuery });
28+
this.props.onRunQuery();
3029
}
31-
32-
return (
33-
<Form onSubmit={onSubmit}>
30+
31+
render() {
32+
return (
33+
<Form onSubmit={(e) => this.onSubmit(e)}>
3434
{({ register, errors }) => {
3535
return (
3636
<Stack
3737
direction="column"
3838
alignItems="flex-start"
3939
>
4040
<HaystackQueryTypeSelector
41-
datasource={datasource}
42-
type={query.type}
43-
refId={query.refId}
44-
onChange={onTypeChange}
41+
datasource={this.props.datasource}
42+
type={this.props.query.type}
43+
refId={this.props.query.refId}
44+
onChange={(e) => this.onTypeChange(e)}
4545
/>
4646
<HaystackQueryInput
47-
query={query}
48-
onChange={onQueryChange}
47+
query={this.props.query}
48+
onChange={(e) => this.onQueryChange(e)}
4949
/>
5050
<Button type="submit" >Run</Button>
5151
</Stack>
5252
);
5353
}}
5454
</Form>
55-
);
55+
);
56+
}
5657
}

0 commit comments

Comments
 (0)