grafana/public/app/features/variables/interval/adapter.ts
kay delaney bad048b7ba
Performance: Standardize lodash imports to use destructured members (#33040)
* Performance: Standardize lodash imports to use destructured members
Changes lodash imports of the form `import x from 'lodash/x'` to
`import { x } from 'lodash'` to reduce bundle size.

* Remove unnecessary _ import from Graph component

* Enforce lodash import style

* Fix remaining lodash imports
2021-04-21 09:38:00 +02:00

44 lines
1.8 KiB
TypeScript

import { cloneDeep } from 'lodash';
import { IntervalVariableModel } from '../types';
import { dispatch } from '../../../store/store';
import { setOptionAsCurrent, setOptionFromUrl } from '../state/actions';
import { VariableAdapter } from '../adapters';
import { initialIntervalVariableModelState, intervalVariableReducer } from './reducer';
import { toVariableIdentifier } from '../state/types';
import { IntervalVariableEditor } from './IntervalVariableEditor';
import { updateAutoValue, updateIntervalVariableOptions } from './actions';
import { optionPickerFactory } from '../pickers';
export const createIntervalVariableAdapter = (): VariableAdapter<IntervalVariableModel> => {
return {
id: 'interval',
description: 'Define a timespan interval (ex 1m, 1h, 1d)',
name: 'Interval',
initialState: initialIntervalVariableModelState,
reducer: intervalVariableReducer,
picker: optionPickerFactory<IntervalVariableModel>(),
editor: IntervalVariableEditor,
dependsOn: () => {
return false;
},
setValue: async (variable, option, emitChanges = false) => {
await dispatch(updateAutoValue(toVariableIdentifier(variable)));
await dispatch(setOptionAsCurrent(toVariableIdentifier(variable), option, emitChanges));
},
setValueFromUrl: async (variable, urlValue) => {
await dispatch(updateAutoValue(toVariableIdentifier(variable)));
await dispatch(setOptionFromUrl(toVariableIdentifier(variable), urlValue));
},
updateOptions: async (variable) => {
await dispatch(updateIntervalVariableOptions(toVariableIdentifier(variable)));
},
getSaveModel: (variable) => {
const { index, id, state, global, ...rest } = cloneDeep(variable);
return rest;
},
getValueForUrl: (variable) => {
return variable.current.value;
},
};
};