Files
grafana/public/app/features/variables/adhoc/AdHocVariableEditor.tsx
Marcus Andersson a7a1406415 Variables: migrates ad hoc variable type to react/redux. (#22784)
* Refactor: moves all the newVariables part to features/variables directory

* Feature: adds datasource type

* Tests: adds reducer tests

* Tests: covers data source actions with tests

* Chore: reduces strict null errors

* boilerplate that will be replaced by real code.

* added old editor template.

* added initial version of ad hoc editor.

* added working (apart from add) version of the editor.

* Added placeholder for picker.

* Have a working UI. Need to connect it so we refresh the variables on changes.

* variable should be updated now.

* removed console.log

* made the url work.

* cleaned up the adapter.

* added possiblity to create filter directly from table.

* moved infotext from general reducer to extended value of adhoc.

* fixed strict null errors.

* fixed strict null errors.

* fixed issue where remove was displayed before being added.

* fixed issue with fragment key.

* changed so template_src is using the redux variables.

* minor refactorings.

* moved adhoc picker to adhoc variable.

* adding tests for reducer and fixed bug.

* added tests or urlparser.

* added tests for ad hoc actions.

* added more tests.

* added more tests.

* fixed strict null error.

* fixed copy n pase error.

* added utilit for getting new variable index.

* removed console.log

* added location to reducerTester type and created a module type for it.

* changed so we only have one builder pattern.

* fixed tests to use static expected values.

* fixed strict errors.

* fixed more strict errors.

Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>
2020-03-23 09:00:36 +01:00

86 lines
2.8 KiB
TypeScript

import React, { PureComponent } from 'react';
import { AdHocVariableModel } from '../../templating/variable';
import { VariableEditorProps } from '../editor/types';
import { VariableEditorState } from '../editor/reducer';
import { AdHocVariableEditorState } from './reducer';
import { initAdHocVariableEditor, changeVariableDatasource } from './actions';
import { connectWithStore } from 'app/core/utils/connectWithReduxStore';
import { MapDispatchToProps, MapStateToProps } from 'react-redux';
import { StoreState } from 'app/types';
export interface OwnProps extends VariableEditorProps<AdHocVariableModel> {}
interface ConnectedProps {
editor: VariableEditorState<AdHocVariableEditorState>;
}
interface DispatchProps {
initAdHocVariableEditor: typeof initAdHocVariableEditor;
changeVariableDatasource: typeof changeVariableDatasource;
}
type Props = OwnProps & ConnectedProps & DispatchProps;
export class AdHocVariableEditorUnConnected extends PureComponent<Props> {
componentDidMount() {
this.props.initAdHocVariableEditor();
}
onDatasourceChanged = (event: React.ChangeEvent<HTMLSelectElement>) => {
this.props.changeVariableDatasource(event.target.value);
};
render() {
const { variable, editor } = this.props;
const dataSources = editor.extended?.dataSources ?? [];
const infoText = editor.extended?.infoText ?? null;
return (
<>
<div className="gf-form-group">
<h5 className="section-heading">Options</h5>
<div className="gf-form max-width-21">
<span className="gf-form-label width-8">Data source</span>
<div className="gf-form-select-wrapper max-width-14">
<select
className="gf-form-input"
required
onChange={this.onDatasourceChanged}
value={variable.datasource ?? ''}
aria-label="Variable editor Form AdHoc DataSource select"
>
{dataSources.map(ds => (
<option key={ds.value ?? ''} value={ds.value ?? ''} label={ds.text}>
{ds.text}
</option>
))}
</select>
</div>
</div>
</div>
{infoText && (
<div className="alert alert-info gf-form-group" aria-label="Variable editor Form Alert">
{infoText}
</div>
)}
</>
);
}
}
const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = (state, ownProps) => ({
editor: state.templating.editor as VariableEditorState<AdHocVariableEditorState>,
});
const mapDispatchToProps: MapDispatchToProps<DispatchProps, OwnProps> = {
initAdHocVariableEditor,
changeVariableDatasource,
};
export const AdHocVariableEditor = connectWithStore(
AdHocVariableEditorUnConnected,
mapStateToProps,
mapDispatchToProps
);