grafana/public/app/features/variables/textbox/actions.ts
Hugo Häggmark 31ab1a4afe
Variables: Ensures all variable values are strings (#31942)
* Variables: Ensures all variable values are strings

* Chore: remove redundant typings

* Chore: fixes tests
2021-03-15 08:52:44 +01:00

32 lines
1.5 KiB
TypeScript

import { TextBoxVariableModel } from '../types';
import { ThunkResult } from '../../../types';
import { getVariable } from '../state/selectors';
import { variableAdapters } from '../adapters';
import { createTextBoxOptions } from './reducer';
import { toVariableIdentifier, toVariablePayload, VariableIdentifier } from '../state/types';
import { setOptionFromUrl } from '../state/actions';
import { UrlQueryValue } from '@grafana/data';
import { changeVariableProp } from '../state/sharedReducer';
import { ensureStringValues } from '../utils';
export const updateTextBoxVariableOptions = (identifier: VariableIdentifier): ThunkResult<void> => {
return async (dispatch, getState) => {
await dispatch(createTextBoxOptions(toVariablePayload(identifier)));
const variableInState = getVariable<TextBoxVariableModel>(identifier.id, getState());
await variableAdapters.get(identifier.type).setValue(variableInState, variableInState.options[0], true);
};
};
export const setTextBoxVariableOptionsFromUrl = (
identifier: VariableIdentifier,
urlValue: UrlQueryValue
): ThunkResult<void> => async (dispatch, getState) => {
const variableInState = getVariable<TextBoxVariableModel>(identifier.id, getState());
const stringUrlValue = ensureStringValues(urlValue);
dispatch(changeVariableProp(toVariablePayload(variableInState, { propName: 'query', propValue: stringUrlValue })));
await dispatch(setOptionFromUrl(toVariableIdentifier(variableInState), stringUrlValue));
};