mirror of
https://github.com/grafana/grafana.git
synced 2025-02-16 18:34:52 -06:00
* Narrow Variable editor state using selector functions - Explicitly type "extended" editor state in editor/reducter.ts using a union - Create selectors to narrow the types, using unique properties from each extended state to discriminate the union - Update DataSourceVariableEditor to use new style of redux connector - Update variable editor components to use new selectors * fix tests * Make adhoc variable infoText optional, because it is! * Add AdHocVariableEditor tests * DataSourceVariableEditor tests * comment * reset * Wrote tests for selectors \(that actually caught a bug, whodathunkit) * fix stray types and lint issues * Rename selector functions
67 lines
2.3 KiB
TypeScript
67 lines
2.3 KiB
TypeScript
import React, { PureComponent } from 'react';
|
|
import { connect, ConnectedProps } from 'react-redux';
|
|
import { Alert, InlineFieldRow, VerticalGroup } from '@grafana/ui';
|
|
import { DataSourceRef, SelectableValue } from '@grafana/data';
|
|
|
|
import { AdHocVariableModel } from '../types';
|
|
import { VariableEditorProps } from '../editor/types';
|
|
import { changeVariableDatasource, initAdHocVariableEditor } from './actions';
|
|
import { StoreState } from 'app/types';
|
|
import { VariableSectionHeader } from '../editor/VariableSectionHeader';
|
|
import { VariableSelectField } from '../editor/VariableSelectField';
|
|
import { getAdhocVariableEditorState } from '../editor/selectors';
|
|
|
|
const mapStateToProps = (state: StoreState) => ({
|
|
extended: getAdhocVariableEditorState(state.templating.editor),
|
|
});
|
|
|
|
const mapDispatchToProps = {
|
|
initAdHocVariableEditor,
|
|
changeVariableDatasource,
|
|
};
|
|
|
|
const connector = connect(mapStateToProps, mapDispatchToProps);
|
|
|
|
export interface OwnProps extends VariableEditorProps<AdHocVariableModel> {}
|
|
|
|
type Props = OwnProps & ConnectedProps<typeof connector>;
|
|
|
|
export class AdHocVariableEditorUnConnected extends PureComponent<Props> {
|
|
componentDidMount() {
|
|
this.props.initAdHocVariableEditor();
|
|
}
|
|
|
|
onDatasourceChanged = (option: SelectableValue<DataSourceRef>) => {
|
|
this.props.changeVariableDatasource(option.value);
|
|
};
|
|
|
|
render() {
|
|
const { variable, extended } = this.props;
|
|
const dataSources = extended?.dataSources ?? [];
|
|
const infoText = extended?.infoText ?? null;
|
|
const options = dataSources.map((ds) => ({ label: ds.text, value: ds.value }));
|
|
const value = options.find((o) => o.value?.uid === variable.datasource?.uid) ?? options[0];
|
|
|
|
return (
|
|
<VerticalGroup spacing="xs">
|
|
<VariableSectionHeader name="Options" />
|
|
<VerticalGroup spacing="sm">
|
|
<InlineFieldRow>
|
|
<VariableSelectField
|
|
name="Data source"
|
|
value={value}
|
|
options={options}
|
|
onChange={this.onDatasourceChanged}
|
|
labelWidth={10}
|
|
/>
|
|
</InlineFieldRow>
|
|
|
|
{infoText ? <Alert title={infoText} severity="info" /> : null}
|
|
</VerticalGroup>
|
|
</VerticalGroup>
|
|
);
|
|
}
|
|
}
|
|
|
|
export const AdHocVariableEditor = connector(AdHocVariableEditorUnConnected);
|