1
+ //import redux from redux; use this in case pf rect application
2
+
3
+ const redux = require ( 'redux' )
4
+ const createStore = redux . createStore
5
+ const bindActionCreator = redux . bindActionCreators
6
+ const combinedReduxer = redux . combineReducers
7
+ const applyMiddleware = redux . applyMiddleware
8
+ const reduxLogger = require ( 'redux-logger' )
9
+ const logger = reduxLogger . createLogger ( )
10
+
11
+
12
+
13
+
14
+ const CAKE_ORDERED = 'cake_ordered'
15
+ const CAKE_RESTOCKED = 'cake_restocked' //Defines different action types as constants to avoid typos.
16
+
17
+ const ICECREAM_ORDERED = 'ICECREAM_ORDERED'
18
+ const ICECREAM_RESTOCKED = 'ICECREAM_RESTOCKED'
19
+ function orderCake ( ) //action creator . It is used so that we dont have to make changes in all dispatche in future
20
+ { // we can directly copy the action object in
21
+ // store.dispatch({ type:CAKE_ORDERED, //Action
22
+ //quantity : 1,) }
23
+
24
+ return {
25
+ type :CAKE_ORDERED , //Action
26
+ payload : 1 ,
27
+ }
28
+
29
+
30
+ }
31
+ function restockCake ( qty = 1 )
32
+ {
33
+ return {
34
+ type :CAKE_RESTOCKED ,
35
+ payload :qty ,
36
+ }
37
+
38
+ }
39
+
40
+ function orderIceCream ( qty = 1 )
41
+ {
42
+ return {
43
+ type :ICECREAM_ORDERED ,
44
+ payload :qty
45
+ }
46
+ }
47
+ function restockIceCream ( qty = 1 )
48
+ {
49
+ return {
50
+ type :ICECREAM_RESTOCKED ,
51
+ payload :qty
52
+ }
53
+ }
54
+
55
+ // const initialState= {
56
+ // numOfCakes :10,
57
+ // numOfIceCream:20, //default or initial state of application
58
+ // }
59
+ const initialCakeState = {
60
+ numOfCakes :10 ,
61
+ //default or initial state of seperate cake application
62
+ }
63
+ const initialIceCreamState = {
64
+ numOfIceCream :20 , //default or initial state of iceCream application
65
+ }
66
+
67
+ //(previousState,action)=>newState
68
+
69
+ const cakereducer = ( state = initialCakeState , action ) => { //shopkeeper=reducer there can be multiple shopkeepers
70
+ switch ( action . type ) {
71
+ case CAKE_ORDERED :
72
+ return {
73
+ ...state , // if we want to change only a particular property of statte
74
+ // we make a copy of it using spread operator this way no
75
+ //change will occur to left over properties
76
+ numOfCakes : state . numOfCakes - 1 ,
77
+ }
78
+ case CAKE_RESTOCKED :
79
+ return {
80
+ ...state ,
81
+ numOfCakes : state . numOfCakes + action . payload ,
82
+ }
83
+ default :
84
+ return state
85
+ }
86
+ }
87
+
88
+ const iceCreamreducer = ( state = initialIceCreamState , action ) => { //shopkeeper=reducer there can be multiple shopkeepers
89
+ switch ( action . type ) {
90
+ case ICECREAM_ORDERED :
91
+ return {
92
+ ...state , // if we want to change only a particular property of statte
93
+ // we make a copy of it using spread operator this way no
94
+ //change will occur to left over properties
95
+ numOfIceCream : state . numOfIceCream - 1 ,
96
+ }
97
+ case ICECREAM_RESTOCKED :
98
+ return {
99
+ ...state ,
100
+ numOfIceCream : state . numOfIceCream + action . payload ,
101
+ }
102
+ default :
103
+ return state
104
+ }
105
+ }
106
+
107
+
108
+ const rootReducer = combinedReduxer ( {
109
+ cake :cakereducer ,
110
+ icecream :iceCreamreducer ,
111
+ } )
112
+ const store = createStore ( rootReducer , applyMiddleware ( logger ) ) // create store
113
+
114
+
115
+
116
+ console . log ( 'Initial state' , store . getState ( ) )
117
+ const unsubscribe = store . subscribe ( ( ) => { } ) ;
118
+ // store.dispatch(orderCake())
119
+ // store.dispatch(orderCake())
120
+ // store.dispatch(orderCake())
121
+ // store.dispatch(restockCake(3))
122
+ const actions = bindActionCreator ( { orderCake, restockCake, orderIceCream, restockIceCream} , store . dispatch )
123
+ actions . orderCake ( )
124
+ actions . restockCake ( 3 )
125
+ actions . orderIceCream ( )
126
+ actions . orderIceCream ( )
127
+ actions . restockIceCream ( 9 )
128
+
129
+ actions . orderCake ( )
130
+ unsubscribe ( ) ;
0 commit comments