actions.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { UrlQueryValue } from '@grafana/data';
  2. import { ThunkResult } from '../../../types';
  3. import { variableAdapters } from '../adapters';
  4. import { setOptionFromUrl } from '../state/actions';
  5. import { toKeyedAction } from '../state/keyedVariablesReducer';
  6. import { getVariable } from '../state/selectors';
  7. import { changeVariableProp } from '../state/sharedReducer';
  8. import { KeyedVariableIdentifier } from '../state/types';
  9. import { TextBoxVariableModel } from '../types';
  10. import { ensureStringValues, toKeyedVariableIdentifier, toVariablePayload } from '../utils';
  11. import { createTextBoxOptions } from './reducer';
  12. export const updateTextBoxVariableOptions = (identifier: KeyedVariableIdentifier): ThunkResult<void> => {
  13. return async (dispatch, getState) => {
  14. const { rootStateKey, type } = identifier;
  15. dispatch(toKeyedAction(rootStateKey, createTextBoxOptions(toVariablePayload(identifier))));
  16. const variableInState = getVariable<TextBoxVariableModel>(identifier, getState());
  17. await variableAdapters.get(type).setValue(variableInState, variableInState.options[0], true);
  18. };
  19. };
  20. export const setTextBoxVariableOptionsFromUrl =
  21. (identifier: KeyedVariableIdentifier, urlValue: UrlQueryValue): ThunkResult<void> =>
  22. async (dispatch, getState) => {
  23. const { rootStateKey } = identifier;
  24. const variableInState = getVariable<TextBoxVariableModel>(identifier, getState());
  25. const stringUrlValue = ensureStringValues(urlValue);
  26. dispatch(
  27. toKeyedAction(
  28. rootStateKey,
  29. changeVariableProp(toVariablePayload(variableInState, { propName: 'query', propValue: stringUrlValue }))
  30. )
  31. );
  32. await dispatch(setOptionFromUrl(toKeyedVariableIdentifier(variableInState), stringUrlValue));
  33. };