Files
grafana/public/app/features/variables/state/variablesReducer.ts
Hugo Häggmark 3c1f27b0e6 Chore: reduce strict errors for variables (#31241)
* Chore: reduces a lot of variable errors

* Chore: reduces variable Editor errors

* Chore: reduces variable Picker errors

* Chore: reduce error count

* Chore: reduces errors for ChangeEvent instead of FormEvent

* Chore: reduces errors with CombinedState

* Chore: reduces ComponentType errors

* Chore: reduce errors in reducers

* Chore: reduces misc errors

* Chore: reduce AdhocPicker errors

* Chore: reduce error limit

* Update public/app/features/variables/adhoc/picker/AdHocFilterValue.tsx

Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com>

* Chore: updates after PR comments

* Chore: small refactor

Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com>
2021-02-18 06:21:35 +01:00

37 lines
1.4 KiB
TypeScript

import { createAction } from '@reduxjs/toolkit';
import { variableAdapters } from '../adapters';
import { sharedReducer } from './sharedReducer';
import { VariableModel } from '../types';
import { AnyAction } from 'redux';
export interface VariablesState extends Record<string, VariableModel> {}
export const initialVariablesState: VariablesState = {};
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((allVariables, state) => {
allVariables[state.id] = state;
return allVariables;
}, {} as Record<string, VariableModel>);
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;
};