-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsuranceCompExample.js
89 lines (78 loc) · 2.2 KB
/
insuranceCompExample.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
/* Redux example with Insurance company setting based on Stephen Grider's Redux Explanation.
This is built using vanilla JS*/
import Redux from 'redux';
const insuranceCompany = () => {
const {createStore, combineReducers } = Redux;
// action creators (represents some kind of form)
const createClaim = (name, amountOfMoneyToClaim) => {
return {
type: 'CREATE_CLAIM',
payload: {
name,
amountOfMoneyToClaim
}
}
}
const createPolicy = (name) => {
return {
type: 'CREATE_POLICY',
payload: {
name,
amount: 1000
}
};
};
const deletePolicy = (name) => {
return {
type: 'DELETE_POLICY',
payload: {
name
}
};
};
// reducers (represents each department)
const claimsHistory = (oldListOfClaims = [], action) => {
if (action.type === 'CREATE_CLAIM') {
return [...oldListOfClaims, action.payload];
}
return oldListOfClaims;
};
const accounting = (moneyReserve = 1000000, action) => {
if (action.type === 'CREATE_CLAIM') {
return moneyReserve - action.payload.amountOfMoneyToClaim;
} else if (action.type === 'CREATE_POLICY') {
return moneyReserve + action.payload.amount
}
return moneyReserve;
}
const policies = (listOfPolicies = [], action) => {
if (action.type === 'CREATE_POLICY') {
return [...listOfPolicies, action.payload.name];
} else if (action.type === 'DELETE_POLICY') {
return listOfPolicies.filter(policy => policy !== action.payload.name);
}
return listOfPolicies;
};
// combine reducers (combine all departments portrays the setup of the company)
const ourDepartments = combineReducers({
accounting,
claimsHistory,
policies
});
// create store (represents company as whole)
const store = createStore(ourDepartments);
// dispatch (form receivers)
const test = () => {
store.dispatch(createPolicy('Jane Doe'));
console.log(store.getState());
store.dispatch(createClaim('Jane Doe', 20000))
console.log(store.getState());
store.dispatch(deletePolicy('Jane Doe'));
console.log(store.getState());
};
return {
test,
store
};
}
export default insuranceCompany;