-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReduxDemo.js
110 lines (94 loc) · 3.11 KB
/
ReduxDemo.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Clear console with each rerun.
console.clear();
// This is a demo of Redux using an insurance company analogy.
//================
// Action Creators
//================
// Synonymous with people dropping off different forms
// Each one returns a plain JavaScript object which we call an action AKA a form.
const createPolicy = (name, amount) => {
return { // Action, i.e., form
type: 'CREATE_POLICY',
payload: {
name: name,
amount: amount
}
};
};
const deletePolicy = name => {
return { // Action, i.e., form
type: 'DELETE_POLICY',
payload: {
name: name
}
};
};
const createClaim = (name, amount) => {
return {
type: 'CREATE_CLAIM',
payload: {
name: name,
amount: amount
}
};
};
//=========
// Reducers
//=========
// Synonymous with departments
// Each one implements some logic and then returns data to the central repository.
const claimsHistory = (oldListOfClaims = [], action) => {
if (action.type === 'CREATE_CLAIM'){
// We care about this action (i.e., form). Pass back revised list of claims with new one added.
return [...oldListOfClaims, action.payload];
}
// We don't care about the action (i.e., form). Return claims list unchanged.
return oldListOfClaims;
};
const accounting = (bagOfMoney = 100, action) => {
if (action.type === 'CREATE_CLAIM') {
// Return bag of money with amount reduced by claim paid.
return bagOfMoney - action.payload.amount;
}
else if (action.type === 'CREATE_POLICY') {
// Return bag of money with amount increased by premium paid.
return bagOfMoney + action.payload.amount;
}
return bagOfMoney;
};
const policies = (listOfPolicies = [], action) => {
if (action.type === 'CREATE_POLICY') {
// Pass back revised list of policies with new one added.
return [...listOfPolicies, action.payload.name];
}
else if (action.type === 'DELETE_POLICY') {
// Return list of policies minus the one we are deleting.
return listOfPolicies.filter(name => name !== action.payload.name);
}
return listOfPolicies;
};
//============
// Redux Store
//============
// Create Redux Store with our reducers.
const { createStore, combineReducers } = Redux;
const ourDepartments = combineReducers({
accounting: accounting,
claimsHistory: claimsHistory,
policies: policies
});
const store = createStore(ourDepartments);
//===============================================
// Create actions that alter state in Redux Store
//===============================================
// Create a few actions for new policies and feed each one to the Redux store.
store.dispatch(createPolicy('Alex', 20));
store.dispatch(createPolicy('Jim', 30));
store.dispatch(createPolicy('Bob', 40));
// Create some claim actions and feed them to the Redux store.
store.dispatch(createClaim('Alex', 120));
store.dispatch(createClaim('Jim', 50));
// Create a policy deletion action and feed it to the Redux store.
store.dispatch(deletePolicy('Bob'));
// Check current state at any time after a dispatch() invocation.
console.log(store.getState());