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 { initialVariableModelState, TextBoxVariableModel, VariableOption } from '../types';
  5. export const initialTextBoxVariableModelState: TextBoxVariableModel = {
  6. ...initialVariableModelState,
  7. type: 'textbox',
  8. query: '',
  9. current: {} as VariableOption,
  10. options: [],
  11. originalQuery: null,
  12. };
  13. export const textBoxVariableSlice = createSlice({
  14. name: 'templating/textbox',
  15. initialState: initialVariablesState,
  16. reducers: {
  17. createTextBoxOptions: (state: VariablesState, action: PayloadAction<VariablePayload>) => {
  18. const instanceState = getInstanceState<TextBoxVariableModel>(state, action.payload.id);
  19. const option = { text: instanceState.query.trim(), value: instanceState.query.trim(), selected: false };
  20. instanceState.options = [option];
  21. instanceState.current = option;
  22. },
  23. },
  24. });
  25. export const textBoxVariableReducer = textBoxVariableSlice.reducer;
  26. export const { createTextBoxOptions } = textBoxVariableSlice.actions;