-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
90 lines (71 loc) · 2 KB
/
script.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
90
//-----------------------------------
console.clear();
// People dropping off a form (Action Creators)
const createPolicy = (name, amount) => {
return { // Action (a form in our analogy)
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 (Departments!)
const claimsHistory = (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.amountOfMoneyToCollect;
} else if (action.type === 'CREATE_POLICY') {
return bagOfMoney + action.payload.amount;
}
return bagOfMoney;
};
const policies = (listOfPolicies = [], action) => {
if (action.type === 'CREATE_POLICY') {
return [...listOfPolicies, action.payload.name];
} else if (action.type === 'DELETE_POLICY') {
return listOfPolicies.filter(name => name !== action.payload.name);
}
return listOfPolicies;
};
const { createStore, combineReducers } = Redux;
const ourDepartments = combineReducers({
accounting: accounting,
claimsHistory: claimsHistory,
policies: policies
});
const store = createStore(ourDepartments);
createPolicy('Aizaz', 20)
createClaim('imran', 120)
deletePolicy('ALi')
store.dispatch(createPolicy('Aizaz', 20));
store.dispatch(createPolicy('imran', 30));
store.dispatch(createPolicy('Ali', 40));
store.dispatch(createClaim('Aizaz', 120));
store.dispatch(createClaim('Imran', 50));
store.dispatch(deletePolicy('ALi'));
console.log(store.getState());