reducer.ts 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. import { createSlice, PayloadAction } from '@reduxjs/toolkit';
  2. import { getInstanceState } from '../state/selectors';
  3. import { initialVariablesState, VariablePayload, VariablesState } from '../state/types';
  4. import { ConstantVariableModel, initialVariableModelState, VariableHide, VariableOption } from '../types';
  5. export const initialConstantVariableModelState: ConstantVariableModel = {
  6. ...initialVariableModelState,
  7. type: 'constant',
  8. hide: VariableHide.hideVariable,
  9. query: '',
  10. current: {} as VariableOption,
  11. options: [],
  12. };
  13. export const constantVariableSlice = createSlice({
  14. name: 'templating/constant',
  15. initialState: initialVariablesState,
  16. reducers: {
  17. createConstantOptionsFromQuery: (state: VariablesState, action: PayloadAction<VariablePayload>) => {
  18. const instanceState = getInstanceState<ConstantVariableModel>(state, action.payload.id);
  19. instanceState.options = [
  20. { text: instanceState.query.trim(), value: instanceState.query.trim(), selected: false },
  21. ];
  22. },
  23. },
  24. });
  25. export const constantVariableReducer = constantVariableSlice.reducer;
  26. export const { createConstantOptionsFromQuery } = constantVariableSlice.actions;