adapter.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { cloneDeep } from 'lodash';
  2. import { dispatch } from '../../../store/store';
  3. import { VariableAdapter } from '../adapters';
  4. import { setOptionAsCurrent } from '../state/actions';
  5. import { TextBoxVariableModel } from '../types';
  6. import { toKeyedVariableIdentifier } from '../utils';
  7. import { TextBoxVariableEditor } from './TextBoxVariableEditor';
  8. import { TextBoxVariablePicker } from './TextBoxVariablePicker';
  9. import { setTextBoxVariableOptionsFromUrl, updateTextBoxVariableOptions } from './actions';
  10. import { initialTextBoxVariableModelState, textBoxVariableReducer } from './reducer';
  11. export const createTextBoxVariableAdapter = (): VariableAdapter<TextBoxVariableModel> => {
  12. return {
  13. id: 'textbox',
  14. description: 'Define a textbox variable, where users can enter any arbitrary string',
  15. name: 'Text box',
  16. initialState: initialTextBoxVariableModelState,
  17. reducer: textBoxVariableReducer,
  18. picker: TextBoxVariablePicker,
  19. editor: TextBoxVariableEditor,
  20. dependsOn: (variable, variableToTest) => {
  21. return false;
  22. },
  23. setValue: async (variable, option, emitChanges = false) => {
  24. await dispatch(setOptionAsCurrent(toKeyedVariableIdentifier(variable), option, emitChanges));
  25. },
  26. setValueFromUrl: async (variable, urlValue) => {
  27. await dispatch(setTextBoxVariableOptionsFromUrl(toKeyedVariableIdentifier(variable), urlValue));
  28. },
  29. updateOptions: async (variable) => {
  30. await dispatch(updateTextBoxVariableOptions(toKeyedVariableIdentifier(variable)));
  31. },
  32. getSaveModel: (variable, saveCurrentAsDefault) => {
  33. const { index, id, state, global, originalQuery, rootStateKey, ...rest } = cloneDeep(variable);
  34. if (variable.query !== originalQuery && !saveCurrentAsDefault) {
  35. const origQuery = originalQuery ?? '';
  36. const current = { selected: false, text: origQuery, value: origQuery };
  37. return { ...rest, query: origQuery, current, options: [current] };
  38. }
  39. return rest;
  40. },
  41. getValueForUrl: (variable) => {
  42. return variable.current.value;
  43. },
  44. beforeAdding: (model) => {
  45. return { ...cloneDeep(model), originalQuery: model.query };
  46. },
  47. };
  48. };