Files
Hugo Häggmark dbec2b02fd Variables: move state tree into a keyed state (#44642)
* Variables: move state tree into a keyed state

* Update public/app/features/variables/state/transactionReducer.ts

Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com>

* Chore: fix prettier error

* Chore: renamed slices and lastUid

* Chore: rename toUidAction

* Chore: rename dashboardVariableReducer

* Chore: rename state prop back to templating

* Chore renames variable.dashboardUid

* Chore: rename toDashboardVariableIdentifier

* Chore: rename getDashboardVariable

* Chore: rename getDashboardVariablesState

* Chore: rename getDashboardVariables

* Chore: some more renames

* Chore: small clean up

* Chore: small rename

* Chore: removes unused function

* Chore: rename VariableModel.stateKey

* Chore: rename KeyedVariableIdentifier.stateKey

* user essentials mob! 🔱

* user essentials mob! 🔱

* user essentials mob! 🔱

* user essentials mob! 🔱

Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com>
Co-authored-by: kay delaney <kay@grafana.com>
Co-authored-by: Alexandra Vargas <alexa1866@gmail.com>
Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
2022-02-18 06:06:04 +01:00

51 lines
2.0 KiB
TypeScript

import { rangeUtil } from '@grafana/data';
import { KeyedVariableIdentifier } from '../state/types';
import { ThunkResult } from '../../../types';
import { createIntervalOptions } from './reducer';
import { validateVariableSelectionState } from '../state/actions';
import { getVariable } from '../state/selectors';
import { IntervalVariableModel } from '../types';
import { getTimeSrv } from '../../dashboard/services/TimeSrv';
import { getTemplateSrv, TemplateSrv } from '../../templating/template_srv';
import { toKeyedAction } from '../state/keyedVariablesReducer';
import { toVariablePayload } from '../utils';
export const updateIntervalVariableOptions =
(identifier: KeyedVariableIdentifier): ThunkResult<void> =>
async (dispatch) => {
const { rootStateKey } = identifier;
await dispatch(toKeyedAction(rootStateKey, createIntervalOptions(toVariablePayload(identifier))));
await dispatch(updateAutoValue(identifier));
await dispatch(validateVariableSelectionState(identifier));
};
export interface UpdateAutoValueDependencies {
calculateInterval: typeof rangeUtil.calculateInterval;
getTimeSrv: typeof getTimeSrv;
templateSrv: TemplateSrv;
}
export const updateAutoValue =
(
identifier: KeyedVariableIdentifier,
dependencies: UpdateAutoValueDependencies = {
calculateInterval: rangeUtil.calculateInterval,
getTimeSrv: getTimeSrv,
templateSrv: getTemplateSrv(),
}
): ThunkResult<void> =>
(dispatch, getState) => {
const variableInState = getVariable<IntervalVariableModel>(identifier, getState());
if (variableInState.auto) {
const res = dependencies.calculateInterval(
dependencies.getTimeSrv().timeRange(),
variableInState.auto_count,
variableInState.auto_min
);
dependencies.templateSrv.setGrafanaVariable('$__auto_interval_' + variableInState.name, res.interval);
// for backward compatibility, to be removed eventually
dependencies.templateSrv.setGrafanaVariable('$__auto_interval', res.interval);
}
};