-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
130 lines (110 loc) · 3.15 KB
/
index.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const redux = require("redux");
const reduxLogger = require("redux-logger");
const createStore = redux.createStore;
// To combine the multiple reducer to one as objects
const combineReducers = redux.combineReducers;
const applyMiddleware = redux.applyMiddleware;
// create logger which is middleware
const logger = reduxLogger.createLogger();
// make the type or tags name
const BUY_CAKE = "BUY_CAKE";
const BUY_ICECREAM = "BUY_ICECREAM";
function buyCake() {
return {
type: BUY_CAKE,
// info: "First redux action",
};
}
function buyIceCream() {
return {
type: BUY_ICECREAM,
};
}
// const initialState = {
// numOfCakes: 10,
// numOfIceCreams: 20
// }
// initial state for cake
const initialCakeState = {
numOfCakes: 10,
};
// initial state for cream
const initialIceCreamState = {
numOfIceCreams: 20,
};
// reducer method which containing the state and action objects
const reducer = (state = initialState, action) => {
switch (action.type) {
case BUY_CAKE:
return {
...state,
numOfCakes: state.numOfCakes - 1,
};
case BUY_ICECREAM:
return {
...state,
numOfIceCreams: state.numOfIceCreams - 1,
};
default:
return state;
}
};
// which cakeRedcer which reducer for buycake mark/tags to use action
const cakeReducer = (state = initialCakeState, action) => {
switch (action.type) {
case BUY_CAKE:
return {
...state,
numOfCakes: state.numOfCakes - 1,
};
default:
return state;
}
};
// which iceCreamReducer which reducer for buycake mark/tags to use action
const iceCreamReducer = (state = initialIceCreamState, action) => {
switch (action.type) {
case BUY_ICECREAM:
return {
...state,
numOfIceCreams: state.numOfIceCreams - 1,
};
default:
return state;
}
};
// combine the reducer to one objects
const rootReducer = combineReducers({
cake: cakeReducer,
iceCream: iceCreamReducer,
});
// first make the redux store and assign the use reducer method here rootReducer which combination of the objects
// contaiing with cake and iceCream reducer method
const store = createStore(rootReducer, applyMiddleware(logger));
// after that use with store getState() which initial state
console.log("Initial State ", store.getState());
// unsubscribe method
const unsubscribe = store.subscribe(() => {});
// dispathch method use with buyCake() which get the action.type and after that reducer execute
store.dispatch(buyCake());
store.dispatch(buyCake());
store.dispatch(buyCake());
store.dispatch(buyCake());
store.dispatch(buyCake());
store.dispatch(buyCake());
store.dispatch(buyCake());
store.dispatch(buyIceCream());
store.dispatch(buyIceCream());
store.dispatch(buyIceCream());
store.dispatch(buyIceCream());
store.dispatch(buyIceCream());
store.dispatch(buyIceCream());
// you can both use at one time
// store.dispatch(buyCake(),buyIceCream());
// store.dispatch(buyCake(),buyIceCream());
// store.dispatch(buyCake(),buyIceCream());
// store.dispatch(buyCake(),buyIceCream());
// store.dispatch(buyCake(),buyIceCream());
// store.dispatch(buyCake(),buyIceCream());
// unsubscribe the method
// unsubscribe();