-
Notifications
You must be signed in to change notification settings - Fork 0
/
introRedux.js
84 lines (65 loc) · 1.76 KB
/
introRedux.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
console.clear();
//various department (Ation Creator)
const createPolicy = (name, amount) =>{
return{
type: 'CREATE_POLICY',
payload:{
name: name,
amount: amount
}
};
};
const deletePolicy = (name) =>{
return {
type: 'DELETE_POLICY',
payload: {
name: name
}
};
};
const createClaim = (name, amountOfMoneyToCollect) =>{
return {
type: 'CREATE_CLAIM',
payload: {
name: name,
amountOfMoneyToCollect: amountOfMoneyToCollect
}
};
};
//Reducers (Department)
const claimHistory = (oldListOfClaims = [] ,action) =>{
if (action.type === 'CREATE_CLAIM'){
//we care about this action (Form !)
return [...oldListOfClaims, action.payload];
}
//we don't care the action (Form !)
return oldListOfClaims;
};
const accounting = (bagOfMoney = 100, action) =>{
if (action.type === 'CREATE_CLAIM'){
return bagOfMoney - action.payload.amaountOfMoneyToCollect;
} else if (action.type === 'CREATE_POLICY'){
return bagOfMoney + action.payload.amount;
}
return bagOfMoney;
};
const policies = (listOfPolicy = [], action) =>{
if (action.type === 'CREATE_POLICY'){
return [...listOfPolicy, action.payload.name];
}else if (action.type === 'DELETE_POLICY'){
return listOfPolicy.filter(name => name !== action.payload.name);
}
return listOfPolicy;
};
const { createStore, combineReducers } = Redux;
const ourDepartments = combineReducers({
accounting: accounting,
policies: policies,
claimHistory: claimHistory
});
const store = createStore(ourDepartments);
store.dispatch(createPolicy('Alex', 20));
store.dispatch(createPolicy('Jim', 30));
store.dispatch(createPolicy('Bob', 40));
store.dispatch(createClaim('Alex', 120));
console.log(store.getState());