Skip to content

Commit 21e5608

Browse files
chapters added
1 parent 7c0abae commit 21e5608

File tree

134 files changed

+91096
-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.

134 files changed

+91096
-0
lines changed

Chapter1/db.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"accounts": [
3+
{ "id": 1, "amount": 200 },
4+
{ "id": 2, "amount": 100 }
5+
],
6+
"bonus": [
7+
{ "id": 1, "points": 2 },
8+
{ "id": 2, "points": 3 }
9+
]
10+
}

Chapter1/index.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { createStore, applyMiddleware, combineReducers } from 'redux';
2+
import logger from 'redux-logger';
3+
import thunk from 'redux-thunk';
4+
import axios from 'axios';
5+
6+
//action name constants
7+
// const init = 'account/init';
8+
const inc = 'account/increment';
9+
const dec = 'account/decrement';
10+
const incByAmt = 'account/incrementByAmount';
11+
const getAccUserPending = 'account/getUser/pending';
12+
const getAccUserFulFilled = 'account/getUser/fulfilled';
13+
const getAccUserRejected = 'account/getUser/rejected';
14+
const incBonus = 'bonus/increment';
15+
//store
16+
const store = createStore(
17+
combineReducers({
18+
account: accountReducer,
19+
bonus: bonusReducer
20+
}),
21+
applyMiddleware(logger.default, thunk.default)
22+
);
23+
24+
const history = [];
25+
26+
//reducer
27+
28+
function accountReducer(state = { amount: 1 }, action) {
29+
switch (action.type) {
30+
case getAccUserFulFilled:
31+
return { amount: action.payload, pending:false };
32+
case getAccUserRejected:
33+
return {...state, error:action.error, pending:false };
34+
case getAccUserPending:
35+
return { ...state,pending:true };
36+
case inc:
37+
return { amount: state.amount + 1 };
38+
case dec:
39+
return { amount: state.amount - 1 };
40+
case incByAmt:
41+
return { amount: state.amount + action.payload };
42+
default:
43+
return state;
44+
}
45+
}
46+
47+
function bonusReducer(state = { points: 0 }, action) {
48+
switch (action.type) {
49+
case incBonus:
50+
return { points: state.points + 1 };
51+
case incByAmt:
52+
if(action.payload>=100)
53+
return { points: state.points + 1 };
54+
default:
55+
return state;
56+
}
57+
}
58+
59+
60+
//global state
61+
62+
// store.subscribe(() => {
63+
// history.push(store.getState());
64+
// console.log(history);
65+
// });
66+
67+
68+
//Action creators
69+
function getUserAccount(id) {
70+
return async (dispatch, getState) => {
71+
try{
72+
dispatch(getAccountUserPending());
73+
const { data } = await axios.get(`http://localhost:3000/accounts/${id}`);
74+
dispatch(getAccountUserFulFilled(data.amount));
75+
} catch(error){
76+
dispatch(getAccountUserRejected(error.message));
77+
}
78+
79+
};
80+
}
81+
function getAccountUserFulFilled(value) {
82+
return { type: getAccUserFulFilled, payload: value };
83+
}
84+
function getAccountUserRejected(error) {
85+
return { type: getAccUserRejected, error: error };
86+
}
87+
function getAccountUserPending() {
88+
return { type: getAccUserPending };
89+
}
90+
91+
function increment() {
92+
return { type: inc };
93+
}
94+
function decrement() {
95+
return { type: dec };
96+
}
97+
function incrementByAmount(value) {
98+
return { type: incByAmt, payload: value };
99+
}
100+
function incrementBonus(value) {
101+
return { type: incBonus};
102+
}
103+
104+
setTimeout(() => {
105+
store.dispatch(getUserAccount(2));
106+
// store.dispatch(incrementByAmount(200))
107+
// store.dispatch(incrementBonus());
108+
}, 2000);

Chapter1/package-lock.json

Lines changed: 154 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Chapter1/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "redux",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"axios": "^1.3.4",
15+
"redux": "^4.2.1",
16+
"redux-logger": "^3.0.6",
17+
"redux-thunk": "^2.4.2"
18+
}
19+
}

Chapter1/test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const state = { account: { amount: 1 }, bonus: { points: 2 } };
2+
const newState = { account: {...state.account}, bonus: {points: state.bonus.points+1} };
3+
4+
console.log(newState, state);
5+
state.account.amount=100;
6+
console.log(newState, state);

Chapter2/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Getting Started with Create React App
2+
3+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4+
5+
## Available Scripts
6+
7+
In the project directory, you can run:
8+
9+
### `npm start`
10+
11+
Runs the app in the development mode.\
12+
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
13+
14+
The page will reload when you make changes.\
15+
You may also see any lint errors in the console.
16+
17+
### `npm test`
18+
19+
Launches the test runner in the interactive watch mode.\
20+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21+
22+
### `npm run build`
23+
24+
Builds the app for production to the `build` folder.\
25+
It correctly bundles React in production mode and optimizes the build for the best performance.
26+
27+
The build is minified and the filenames include the hashes.\
28+
Your app is ready to be deployed!
29+
30+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31+
32+
### `npm run eject`
33+
34+
**Note: this is a one-way operation. Once you `eject`, you can't go back!**
35+
36+
If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37+
38+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
39+
40+
You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
41+
42+
## Learn More
43+
44+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45+
46+
To learn React, check out the [React documentation](https://reactjs.org/).
47+
48+
### Code Splitting
49+
50+
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51+
52+
### Analyzing the Bundle Size
53+
54+
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55+
56+
### Making a Progressive Web App
57+
58+
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59+
60+
### Advanced Configuration
61+
62+
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63+
64+
### Deployment
65+
66+
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67+
68+
### `npm run build` fails to minify
69+
70+
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)

Chapter2/db.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"accounts": [
3+
{ "id": 1, "amount": 200 },
4+
{ "id": 2, "amount": 100 }
5+
],
6+
"bonus": [
7+
{ "id": 1, "points": 2 },
8+
{ "id": 2, "points": 3 }
9+
]
10+
}

0 commit comments

Comments
 (0)