mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
c7d3fec515
* 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 * undo whatever changes i did to swagger spec
35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
import { createAction } from '@reduxjs/toolkit';
|
|
import { AnyAction } from 'redux';
|
|
|
|
import { variableAdapters } from '../adapters';
|
|
|
|
import { sharedReducer } from './sharedReducer';
|
|
import { initialVariablesState, VariablesState } from './types';
|
|
|
|
export const cleanVariables = createAction<undefined>('templating/cleanVariables');
|
|
|
|
export const variablesReducer = (state: VariablesState = initialVariablesState, action: AnyAction): VariablesState => {
|
|
if (cleanVariables.match(action)) {
|
|
const globalVariables = Object.values(state).filter((v) => v.global);
|
|
if (!globalVariables) {
|
|
return initialVariablesState;
|
|
}
|
|
|
|
const variables = globalVariables.reduce<typeof state>((allVariables, state) => {
|
|
allVariables[state.id] = state;
|
|
return allVariables;
|
|
}, {});
|
|
|
|
return variables;
|
|
}
|
|
|
|
if (action?.payload?.type && variableAdapters.getIfExists(action?.payload?.type)) {
|
|
// Now that we know we are dealing with a payload that is addressed for an adapted variable let's reduce state:
|
|
// Firstly call the sharedTemplatingReducer that handles all shared actions between variable types
|
|
// Secondly call the specific variable type's reducer
|
|
return variableAdapters.get(action.payload.type).reducer(sharedReducer(state, action), action);
|
|
}
|
|
|
|
return state;
|
|
};
|