adapter.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { cloneDeep } from 'lodash';
  2. import { dispatch } from '../../../store/store';
  3. import { VariableAdapter } from '../adapters';
  4. import { optionPickerFactory } from '../pickers';
  5. import { setOptionAsCurrent, setOptionFromUrl } from '../state/actions';
  6. import { IntervalVariableModel } from '../types';
  7. import { toKeyedVariableIdentifier } from '../utils';
  8. import { IntervalVariableEditor } from './IntervalVariableEditor';
  9. import { updateAutoValue, updateIntervalVariableOptions } from './actions';
  10. import { initialIntervalVariableModelState, intervalVariableReducer } from './reducer';
  11. export const createIntervalVariableAdapter = (): VariableAdapter<IntervalVariableModel> => {
  12. return {
  13. id: 'interval',
  14. description: 'Define a timespan interval (ex 1m, 1h, 1d)',
  15. name: 'Interval',
  16. initialState: initialIntervalVariableModelState,
  17. reducer: intervalVariableReducer,
  18. picker: optionPickerFactory<IntervalVariableModel>(),
  19. editor: IntervalVariableEditor,
  20. dependsOn: () => {
  21. return false;
  22. },
  23. setValue: async (variable, option, emitChanges = false) => {
  24. await dispatch(updateAutoValue(toKeyedVariableIdentifier(variable)));
  25. await dispatch(setOptionAsCurrent(toKeyedVariableIdentifier(variable), option, emitChanges));
  26. },
  27. setValueFromUrl: async (variable, urlValue) => {
  28. await dispatch(updateAutoValue(toKeyedVariableIdentifier(variable)));
  29. await dispatch(setOptionFromUrl(toKeyedVariableIdentifier(variable), urlValue));
  30. },
  31. updateOptions: async (variable) => {
  32. await dispatch(updateIntervalVariableOptions(toKeyedVariableIdentifier(variable)));
  33. },
  34. getSaveModel: (variable) => {
  35. const { index, id, state, global, rootStateKey, ...rest } = cloneDeep(variable);
  36. return rest;
  37. },
  38. getValueForUrl: (variable) => {
  39. return variable.current.value;
  40. },
  41. };
  42. };