adapter.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { cloneDeep } from 'lodash';
  2. import { dispatch } from '../../../store/store';
  3. import { VariableAdapter } from '../adapters';
  4. import { AdHocVariableModel } from '../types';
  5. import { toKeyedVariableIdentifier } from '../utils';
  6. import { AdHocVariableEditor } from './AdHocVariableEditor';
  7. import { setFiltersFromUrl } from './actions';
  8. import { AdHocPicker } from './picker/AdHocPicker';
  9. import { adHocVariableReducer, initialAdHocVariableModelState } from './reducer';
  10. import * as urlParser from './urlParser';
  11. const noop = async () => {};
  12. export const createAdHocVariableAdapter = (): VariableAdapter<AdHocVariableModel> => {
  13. return {
  14. id: 'adhoc',
  15. description: 'Add key/value filters on the fly.',
  16. name: 'Ad hoc filters',
  17. initialState: initialAdHocVariableModelState,
  18. reducer: adHocVariableReducer,
  19. picker: AdHocPicker,
  20. editor: AdHocVariableEditor,
  21. dependsOn: () => false,
  22. setValue: noop,
  23. setValueFromUrl: async (variable, urlValue) => {
  24. const filters = urlParser.toFilters(urlValue);
  25. await dispatch(setFiltersFromUrl(toKeyedVariableIdentifier(variable), filters));
  26. },
  27. updateOptions: noop,
  28. getSaveModel: (variable) => {
  29. const { index, id, state, global, rootStateKey, ...rest } = cloneDeep(variable);
  30. return rest;
  31. },
  32. getValueForUrl: (variable) => {
  33. const filters = variable?.filters ?? [];
  34. return urlParser.toUrl(filters);
  35. },
  36. };
  37. };