Skip to content

Commit 9148d80

Browse files
committedMar 4, 2025
Initial Commit
0 parents  commit 9148d80

File tree

96 files changed

+12537
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+12537
-0
lines changed
 

‎index.js

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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();

‎nested-state.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const redux =require('redux')
2+
const produce =require('immer').produce
3+
const initialState={
4+
name:'Ayush',
5+
address:{
6+
street:'Ambedkar Nagar',
7+
city:'Sonbhadra',
8+
state:'UP',
9+
},
10+
}
11+
12+
const STREET_UPDATED = 'STREET__UPDATED'
13+
14+
function updatestreet(street) //Action creator
15+
{
16+
return{
17+
type:STREET_UPDATED,
18+
payload:street, //Action
19+
}
20+
21+
}
22+
23+
const reducer =(state=initialState,action)=>{
24+
switch(action.type)
25+
{
26+
case STREET_UPDATED :
27+
// return{
28+
// ...state,
29+
// address:{
30+
// ...state.address,
31+
// street:action.payload,
32+
33+
// },
34+
// }
35+
36+
return produce(state,(draft)=>{
37+
draft.address.street=action.payload
38+
39+
})
40+
default : {
41+
return state
42+
}
43+
44+
}
45+
}
46+
47+
const store = redux.createStore(reducer)
48+
console.log('Initial State',store.getState())
49+
const unsubscribe = store.subscribe(()=>{
50+
console.log('Updated State',store.getState());
51+
52+
})
53+
store.dispatch(updatestreet('Robertsganj')) //dispatch on action to udate the store
54+
unsubscribe()

0 commit comments

Comments
 (0)
Please sign in to comment.