-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (68 loc) · 1.9 KB
/
index.js
File metadata and controls
88 lines (68 loc) · 1.9 KB
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
const redux = require('redux');
const reduxLogger = require('redux-logger')
const applyMiddleware = redux.applyMiddleware;
const createStore = redux.createStore; //alias
const combineReducers = redux.combineReducers;
const logger = reduxLogger.createLogger()
//Constant created to store a sting as an indentifier of Action
const BUY_CAKE = "BUY_CAKE";
const BUY_COOKIE = "BUY_COOKIE";
//Action Funtion retuning a Object to be mapped by reducer
function buyCakeAction() {
return {
type : BUY_CAKE,
info : 'Specific Action Caller for Cake'
}
}
function buyCookieAction() {
return {
type : BUY_COOKIE,
info : 'Specific Action Caller for Cookie'
}
}
//Temp store object
const cakeState = {
numberOfCakes : 10
}
const cookieState = {
numberOfCookies : 20
}
//********************************************************************
//Reducer function (Pure Function) (previousState, action) => newState
const cakeReducer = (state = cakeState, action) => {
switch(action.type){
case BUY_CAKE : return {
...state,
numberOfCakes : state.numberOfCakes - 1
}
default : return state;
}
}
const cookieReducer = (state = cookieState, action) => {
switch(action.type){
case BUY_COOKIE : return {
...state,
numberOfCookies : state.numberOfCookies - 1
}
default : return state;
}
}
//***********************
//Combining Reducers
const rootReducer = combineReducers({
cake : cakeReducer,
cookie : cookieReducer
})
//store creation
const store = createStore(rootReducer, applyMiddleware(logger));
console.log('Initial State - ', store.getState());
//make a subscriber
const unsubscribe = store.subscribe(() => {})
//Action Caller a.k.a dispatch(actionCallerFunction to return action object)
store.dispatch(buyCakeAction())
store.dispatch(buyCakeAction())
store.dispatch(buyCakeAction())
store.dispatch(buyCookieAction())
store.dispatch(buyCookieAction())
store.dispatch(buyCookieAction())
unsubscribe()