1import { createSlice, PayloadAction } from '@reduxjs/toolkit';
2import { VariablePayload } from '../state/types';
3
4type VariableEditorExtension<ExtendedProps extends {} = {}> = { [P in keyof ExtendedProps]: ExtendedProps[P] };
5export interface VariableEditorState<ExtendedProps extends {} = {}> {
6  id: string;
7  name: string;
8  errors: Record<string, string>;
9  isValid: boolean;
10  extended: VariableEditorExtension<ExtendedProps> | null;
11}
12
13export const initialVariableEditorState: VariableEditorState = {
14  id: '',
15  isValid: true,
16  errors: {},
17  name: '',
18  extended: null,
19};
20
21const variableEditorReducerSlice = createSlice({
22  name: 'templating/editor',
23  initialState: initialVariableEditorState,
24  reducers: {
25    setIdInEditor: (state: VariableEditorState, action: PayloadAction<{ id: string }>) => {
26      state.id = action.payload.id;
27    },
28    clearIdInEditor: (state: VariableEditorState, action: PayloadAction<undefined>) => {
29      state.id = '';
30    },
31    variableEditorMounted: (state: VariableEditorState, action: PayloadAction<{ name: string }>) => {
32      state.name = action.payload.name;
33    },
34    variableEditorUnMounted: (state: VariableEditorState, action: PayloadAction<VariablePayload>) => {
35      return initialVariableEditorState;
36    },
37    changeVariableNameSucceeded: (
38      state: VariableEditorState,
39      action: PayloadAction<VariablePayload<{ newName: string }>>
40    ) => {
41      state.name = action.payload.data.newName;
42      delete state.errors['name'];
43      state.isValid = Object.keys(state.errors).length === 0;
44    },
45    changeVariableNameFailed: (
46      state: VariableEditorState,
47      action: PayloadAction<{ newName: string; errorText: string }>
48    ) => {
49      state.name = action.payload.newName;
50      state.errors.name = action.payload.errorText;
51      state.isValid = Object.keys(state.errors).length === 0;
52    },
53    addVariableEditorError: (
54      state: VariableEditorState,
55      action: PayloadAction<{ errorProp: string; errorText: any }>
56    ) => {
57      state.errors[action.payload.errorProp] = action.payload.errorText;
58      state.isValid = Object.keys(state.errors).length === 0;
59    },
60    removeVariableEditorError: (state: VariableEditorState, action: PayloadAction<{ errorProp: string }>) => {
61      delete state.errors[action.payload.errorProp];
62      state.isValid = Object.keys(state.errors).length === 0;
63    },
64    changeVariableEditorExtended: (
65      state: VariableEditorState,
66      action: PayloadAction<{ propName: string; propValue: any }>
67    ) => {
68      state.extended = {
69        ...state.extended,
70        [action.payload.propName]: action.payload.propValue,
71      };
72    },
73    cleanEditorState: () => initialVariableEditorState,
74  },
75});
76
77export const variableEditorReducer = variableEditorReducerSlice.reducer;
78
79export const {
80  setIdInEditor,
81  clearIdInEditor,
82  changeVariableNameSucceeded,
83  changeVariableNameFailed,
84  variableEditorMounted,
85  variableEditorUnMounted,
86  changeVariableEditorExtended,
87  addVariableEditorError,
88  removeVariableEditorError,
89  cleanEditorState,
90} = variableEditorReducerSlice.actions;
91