-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaygroundSlice.ts
97 lines (91 loc) · 2.81 KB
/
playgroundSlice.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import { AccessPoint } from '../../../app/types';
type PlaygroundState = {
value: AccessPoint[];
old: AccessPoint[];
toBeUpdated: AccessPoint[];
};
export const playgroundSlice = createSlice({
name: 'playground',
initialState: {
value: [],
old: [],
toBeUpdated: [],
} satisfies PlaygroundState as PlaygroundState,
reducers: {
initializePlayground: (state, action: PayloadAction<AccessPoint[]>) => {
return { ...state, value: action.payload, old: action.payload };
},
replacePlayground: (state, action: PayloadAction<AccessPoint[]>) => {
return { ...state, value: action.payload };
},
addPlayground: (state, action: PayloadAction<AccessPoint>) => {
return { ...state, value: [...state.value, action.payload] };
},
removePlayground: (state, action) => {
return {
...state,
value: state.value.filter((item) => item !== action.payload),
};
},
updatePlayground: (state, action: PayloadAction<AccessPoint>) => {
const newArray = state.value.map((item) => {
if (item.id === action.payload.id) {
return action.payload;
}
return item;
});
state = { ...state, value: newArray };
},
replaceOldPlayground: (state, action: PayloadAction<AccessPoint[]>) => {
return { ...state, old: action.payload };
},
addToUpdatePlayground: (state, action: PayloadAction<AccessPoint>) => {
for (let i = 0; i < state.toBeUpdated.length; i++) {
if (state.toBeUpdated[i].id === action.payload.id) {
return {
...state,
toBeUpdated: state.toBeUpdated.map((item) => {
if (item.id === action.payload.id) {
return action.payload;
}
return item;
}),
};
}
}
return { ...state, toBeUpdated: [...state.toBeUpdated, action.payload] };
},
clearToUpdatePlayground: (state) => {
return { ...state, toBeUpdated: [] };
},
removeFromUpdatePlayground: (state, action: PayloadAction<AccessPoint>) => {
return {
...state,
toBeUpdated: state.toBeUpdated.filter(
(item) => item !== action.payload
),
};
},
replaceToUpdatePlayground: (
state,
action: PayloadAction<AccessPoint[]>
) => {
return { ...state, toBeUpdated: action.payload };
},
},
});
export const {
initializePlayground,
replacePlayground,
addPlayground,
removePlayground,
updatePlayground,
replaceOldPlayground,
addToUpdatePlayground,
clearToUpdatePlayground,
removeFromUpdatePlayground,
replaceToUpdatePlayground,
} = playgroundSlice.actions;
export default playgroundSlice.reducer;
// https://redux-toolkit.js.org/usage/usage-with-typescript