grafana/public/app/features/variables/textbox/actions.ts
Josh Hunt 4b4d546e32
Typed variables pt4: Remove generics from getVariable (#53017)
* wip

* make diff easier to read

* Update template_srv getVariables to return new TypedVariableModel

* update VariableType to use the type from TypedVariableModel

* tidy things up

* Chore: Use type-accurate mock variables in tests

* Chore: Type VariableState to use TypedVariableModel

* fix typo

* remove type assertion from template_srv.getVariables

* use typescript/no-redeclare for compatibility with ts overloads

* remove generics from getVariable() and overload it to only return undefined based on arguments

* update usages of getVariable()

* Fix Interval variable options picker not working
2022-08-05 13:44:52 +01:00

46 lines
1.7 KiB
TypeScript

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