-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducers.js
90 lines (86 loc) · 2.13 KB
/
reducers.js
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
import {genBudObj, genOpObj, genRecurenceObj, genOpFromRec} from '../utils/index'
const initialState = {ops:[], budgets:[], recurences:[]}
function opsReducer(state = initialState, action) {
let nextState
const opsCpy = [...state.ops]
const budCpy = [...state.budgets]
const recCpy = [...state.recurences]
switch (action.type) {
case 'ADD_OP':
let [value, budget, date, comment] = action.value
let op = genOpObj(value, budget, false, date, comment)
opsCpy.push(op)
nextState={
...state,
ops:opsCpy
}
return nextState || state
case 'ADD_BUDGET':
budget = genBudObj(action.value)
budCpy.push(budget)
nextState={
...state,
budgets:budCpy
}
return nextState || state
case 'ADD_RECURENCE':
recurence = genRecurenceObj(action.value)
op = genOpFromRec(action.value)
recCpy.push(recurence)
opsCpy.push(op)
nextState={
...state,
recurences:recCpy,
ops:opsCpy
}
return nextState || state
case 'CLEAN_BUDGETS': //DEV
nextState={
...state,
budgets:[]
}
return nextState || state
case 'CLEAN_RECURENCES': //DEV
nextState={
...state,
recurences:[]
}
return nextState || state
case 'CLEAN_OPS': //DEV
nextState={
...state,
ops:[]
}
return nextState || state
case 'CHECK_OPS':
opsCpy.forEach((op)=>{
if(action.value.includes(op.id)){
op.checked=true
}
})
nextState={
...state,
ops:opsCpy
}
return nextState || state
case 'UNCHECK_ALL': //DEV
opcCpy = [...state.ops]
opsCpy.forEach(op=>op.checked = false)
nextState={
...state,
ops:opsCpy
}
return nextState || state
case 'AFFECT_TO_BUDGET':
[budget, value] = action.value
budCpy[budCpy.findIndex(e=>e.id === budget)].actual -= value
nextState={
...state,
budgets:budCpy
}
return nextState || state
default:
return state
}
}
export default opsReducer