grafana/public/app/plugins/datasource/testdata/QueryEditor.tsx

67 lines
1.8 KiB
TypeScript
Raw Normal View History

// Libraries
2019-01-17 10:37:34 -06:00
import React, { PureComponent } from 'react';
import _ from 'lodash';
2019-01-17 10:37:34 -06:00
// Services & Utils
import { getBackendSrv } from '@grafana/runtime';
// Components
import { FormLabel, Select } from '@grafana/ui';
import { QueryEditorProps, SelectableValue } from '@grafana/data';
// Types
import { TestDataDataSource } from './datasource';
2019-01-18 10:19:35 -06:00
import { TestDataQuery, Scenario } from './types';
interface State {
scenarioList: Scenario[];
current: Scenario | null;
2019-01-17 10:37:34 -06:00
}
type Props = QueryEditorProps<TestDataDataSource, TestDataQuery>;
2019-01-18 10:19:35 -06:00
export class QueryEditor extends PureComponent<Props> {
backendSrv = getBackendSrv();
state: State = {
scenarioList: [],
current: null,
};
async componentDidMount() {
2019-01-18 12:11:30 -06:00
const { query, datasource } = this.props;
query.scenarioId = query.scenarioId || 'random_walk';
2019-01-18 12:11:30 -06:00
// const scenarioList = await this.backendSrv.get('/api/tsdb/testdata/scenarios');
const scenarioList = await datasource.getScenarios();
2019-04-15 05:11:52 -05:00
const current: any = _.find(scenarioList, { id: query.scenarioId });
this.setState({ scenarioList: scenarioList, current: current });
}
onScenarioChange = (item: SelectableValue<string>) => {
this.props.onChange({
...this.props.query,
scenarioId: item.value,
});
};
2019-01-17 10:37:34 -06:00
render() {
const { query } = this.props;
const options = this.state.scenarioList.map(item => ({ label: item.name, value: item.id }));
const current = options.find(item => item.value === query.scenarioId);
2019-01-17 10:37:34 -06:00
return (
<div className="gf-form-inline">
<div className="gf-form">
<FormLabel className="query-keyword" width={7}>
Scenario
</FormLabel>
<Select options={options} value={current} onChange={this.onScenarioChange} />
</div>
</div>
2019-01-17 10:37:34 -06:00
);
}
}