adapter.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { cloneDeep } from 'lodash';
  2. import { dispatch } from '../../../store/store';
  3. import { VariableAdapter } from '../adapters';
  4. import { ALL_VARIABLE_TEXT } from '../constants';
  5. import { optionPickerFactory } from '../pickers';
  6. import { setOptionAsCurrent, setOptionFromUrl } from '../state/actions';
  7. import { CustomVariableModel } from '../types';
  8. import { isAllVariable, toKeyedVariableIdentifier } from '../utils';
  9. import { CustomVariableEditor } from './CustomVariableEditor';
  10. import { updateCustomVariableOptions } from './actions';
  11. import { customVariableReducer, initialCustomVariableModelState } from './reducer';
  12. export const createCustomVariableAdapter = (): VariableAdapter<CustomVariableModel> => {
  13. return {
  14. id: 'custom',
  15. description: 'Define variable values manually',
  16. name: 'Custom',
  17. initialState: initialCustomVariableModelState,
  18. reducer: customVariableReducer,
  19. picker: optionPickerFactory<CustomVariableModel>(),
  20. editor: CustomVariableEditor,
  21. dependsOn: () => {
  22. return false;
  23. },
  24. setValue: async (variable, option, emitChanges = false) => {
  25. await dispatch(setOptionAsCurrent(toKeyedVariableIdentifier(variable), option, emitChanges));
  26. },
  27. setValueFromUrl: async (variable, urlValue) => {
  28. await dispatch(setOptionFromUrl(toKeyedVariableIdentifier(variable), urlValue));
  29. },
  30. updateOptions: async (variable) => {
  31. await dispatch(updateCustomVariableOptions(toKeyedVariableIdentifier(variable)));
  32. },
  33. getSaveModel: (variable) => {
  34. const { index, id, state, global, rootStateKey, ...rest } = cloneDeep(variable);
  35. return rest;
  36. },
  37. getValueForUrl: (variable) => {
  38. if (isAllVariable(variable)) {
  39. return ALL_VARIABLE_TEXT;
  40. }
  41. return variable.current.value;
  42. },
  43. };
  44. };