-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducer.ts
59 lines (49 loc) · 955 Bytes
/
reducer.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
export const SET_FIELD = 'SET_FIELD'
export const CLEAR_FIELD = 'CLEAR_FIELD'
export const RESET = 'RESET'
interface State {
[fieldKey: string]: any
}
interface Action {
type: string
payload?: {
fieldKey: string
value?: any
}
}
const reducer = (state: State, action: Action): State => {
const { payload } = action
switch (action.type) {
case SET_FIELD: {
if (!payload) {
return state
}
return {
...state,
[payload.fieldKey]: payload.value
}
}
case CLEAR_FIELD: {
if (!payload) {
return state
}
return {
...state,
[payload.fieldKey]: undefined
}
}
case RESET: {
return Object.keys(state).reduce(
(res: Record<string, undefined>, key) => {
res[key] = undefined
return res
},
{}
)
}
default: {
return state
}
}
}
export default reducer