reducer.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { createSlice, PayloadAction } from '@reduxjs/toolkit';
  2. import { AdHocVariableFilter, AdHocVariableModel, initialVariableModelState } from 'app/features/variables/types';
  3. import { getInstanceState } from '../state/selectors';
  4. import { initialVariablesState, VariablePayload, VariablesState } from '../state/types';
  5. export interface AdHocVariabelFilterUpdate {
  6. index: number;
  7. filter: AdHocVariableFilter;
  8. }
  9. export const initialAdHocVariableModelState: AdHocVariableModel = {
  10. ...initialVariableModelState,
  11. type: 'adhoc',
  12. datasource: null,
  13. filters: [],
  14. };
  15. export const adHocVariableSlice = createSlice({
  16. name: 'templating/adhoc',
  17. initialState: initialVariablesState,
  18. reducers: {
  19. filterAdded: (state: VariablesState, action: PayloadAction<VariablePayload<AdHocVariableFilter>>) => {
  20. const instanceState = getInstanceState<AdHocVariableModel>(state, action.payload.id);
  21. instanceState.filters.push(action.payload.data);
  22. },
  23. filterRemoved: (state: VariablesState, action: PayloadAction<VariablePayload<number>>) => {
  24. const instanceState = getInstanceState<AdHocVariableModel>(state, action.payload.id);
  25. const index = action.payload.data;
  26. instanceState.filters.splice(index, 1);
  27. },
  28. filterUpdated: (state: VariablesState, action: PayloadAction<VariablePayload<AdHocVariabelFilterUpdate>>) => {
  29. const instanceState = getInstanceState<AdHocVariableModel>(state, action.payload.id);
  30. const { filter, index } = action.payload.data;
  31. instanceState.filters[index] = filter;
  32. },
  33. filtersRestored: (state: VariablesState, action: PayloadAction<VariablePayload<AdHocVariableFilter[]>>) => {
  34. const instanceState = getInstanceState<AdHocVariableModel>(state, action.payload.id);
  35. instanceState.filters = action.payload.data;
  36. },
  37. },
  38. });
  39. export const { filterAdded, filterRemoved, filterUpdated, filtersRestored } = adHocVariableSlice.actions;
  40. export const adHocVariableReducer = adHocVariableSlice.reducer;