├── src
├── App.css
├── redux
│ ├── cake
│ │ ├── cakeTypes.js
│ │ ├── cakeActions.js
│ │ └── cakeReducer.js
│ ├── iceCream
│ │ ├── iceCreamTypes.js
│ │ ├── iceCreamActions.js
│ │ └── iceCreamReducer.js
│ ├── index.js
│ ├── sagas
│ │ ├── rootSaga.js
│ │ └── postSaga.js
│ ├── user
│ │ ├── userTypes.js
│ │ ├── userReducer.js
│ │ └── userActions.js
│ ├── posts
│ │ ├── postTypes.js
│ │ ├── postReducer.js
│ │ └── postActions.js
│ ├── rootReducer.js
│ └── store.js
├── setupTests.js
├── index.css
├── reportWebVitals.js
├── index.js
├── components
│ ├── CakeContainer
│ │ └── CakeContainer.js
│ ├── BuyIceCream
│ │ └── BuyIceCream.js
│ ├── HooksCakeContainer
│ │ └── HooksCakeContainer.js
│ ├── CakeWtihPayload
│ │ └── CakeWtihPayload.js
│ ├── USerContainer
│ │ └── UserContainer.js
│ ├── postSaga
│ │ └── postSaga.js
│ └── ItemContainer
│ │ └── ItemContainer.js
└── App.js
├── public
├── robots.txt
├── favicon.ico
├── logo192.png
├── logo512.png
├── manifest.json
└── index.html
├── .gitignore
├── package.json
├── redux-basics.js
├── asyncActions.js
├── README.md
└── .eslintcache
/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | text-align: center;
3 | }
4 |
--------------------------------------------------------------------------------
/src/redux/cake/cakeTypes.js:
--------------------------------------------------------------------------------
1 | export const BUY_CAKE = 'BUY_CAKE'
--------------------------------------------------------------------------------
/src/redux/iceCream/iceCreamTypes.js:
--------------------------------------------------------------------------------
1 | export const BUY_ICECREAM = 'BUY_ICECREAM'
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sahebmohammadi/Redux-tutorial/HEAD/public/favicon.ico
--------------------------------------------------------------------------------
/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sahebmohammadi/Redux-tutorial/HEAD/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sahebmohammadi/Redux-tutorial/HEAD/public/logo512.png
--------------------------------------------------------------------------------
/src/redux/index.js:
--------------------------------------------------------------------------------
1 | export { buyCake } from "./cake/cakeActions";
2 | export { buyIceCream } from "./iceCream/iceCreamActions";
3 | export * from "./user/userActions";
4 |
--------------------------------------------------------------------------------
/src/redux/sagas/rootSaga.js:
--------------------------------------------------------------------------------
1 | import { all } from "redux-saga/effects";
2 | import { watchFetchPost } from "./postSaga";
3 |
4 | export function* rootSaga() {
5 | yield all([watchFetchPost()]);
6 | }
7 |
--------------------------------------------------------------------------------
/src/redux/iceCream/iceCreamActions.js:
--------------------------------------------------------------------------------
1 | import { BUY_ICECREAM } from "./iceCreamTypes";
2 |
3 | export const buyIceCream = () => {
4 | return {
5 | type: BUY_ICECREAM,
6 | value: 5,
7 | };
8 | };
9 |
--------------------------------------------------------------------------------
/src/redux/user/userTypes.js:
--------------------------------------------------------------------------------
1 | export const FETCH_USERS_REQUEST = "FETCH_USERS_REQUEST";
2 | export const FETCH_USERS_SUCCESS = "FETCH_USERS_SUCCESS";
3 | export const FETCH_USERS_FAILURE = "FETCH_USERS_FAILURE";
4 |
--------------------------------------------------------------------------------
/src/redux/posts/postTypes.js:
--------------------------------------------------------------------------------
1 | export const FETCH_POSTS_REQUEST = "FETCH_POSTS_REQUEST";
2 | export const FETCH_POSTS_SUCCESS = "FETCH_POSTS_SUCCESS";
3 | export const FETCH_POSTS_FAILURE = "FETCH_POSTS_FAILURE";
4 |
--------------------------------------------------------------------------------
/src/redux/cake/cakeActions.js:
--------------------------------------------------------------------------------
1 | import { BUY_CAKE } from "./cakeTypes";
2 |
3 | export const buyCake = (cake = 1) => {
4 | return {
5 | type: BUY_CAKE,
6 | payload: cake,
7 | };
8 | };
9 |
10 | // export default buyCake;
11 |
--------------------------------------------------------------------------------
/src/setupTests.js:
--------------------------------------------------------------------------------
1 | // jest-dom adds custom jest matchers for asserting on DOM nodes.
2 | // allows you to do things like:
3 | // expect(element).toHaveTextContent(/react/i)
4 | // learn more: https://github.com/testing-library/jest-dom
5 | import '@testing-library/jest-dom';
6 |
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5 | sans-serif;
6 | -webkit-font-smoothing: antialiased;
7 | -moz-osx-font-smoothing: grayscale;
8 | }
9 |
10 | code {
11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12 | monospace;
13 | }
14 |
--------------------------------------------------------------------------------
/src/reportWebVitals.js:
--------------------------------------------------------------------------------
1 | const reportWebVitals = onPerfEntry => {
2 | if (onPerfEntry && onPerfEntry instanceof Function) {
3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
4 | getCLS(onPerfEntry);
5 | getFID(onPerfEntry);
6 | getFCP(onPerfEntry);
7 | getLCP(onPerfEntry);
8 | getTTFB(onPerfEntry);
9 | });
10 | }
11 | };
12 |
13 | export default reportWebVitals;
14 |
--------------------------------------------------------------------------------
/src/redux/cake/cakeReducer.js:
--------------------------------------------------------------------------------
1 | import { BUY_CAKE } from "./cakeTypes";
2 |
3 | const initialState = {
4 | numberOfCakes: 10,
5 | };
6 |
7 | const cakeReducer = (state = initialState, action) => {
8 | switch (action.type) {
9 | case BUY_CAKE:
10 | return {
11 | ...state,
12 | numberOfCakes: state.numberOfCakes - action.payload,
13 | };
14 | default:
15 | return state;
16 | }
17 | };
18 |
19 | export default cakeReducer;
20 |
--------------------------------------------------------------------------------
/src/redux/rootReducer.js:
--------------------------------------------------------------------------------
1 | import { combineReducers } from "redux";
2 | import cakeReducer from "./cake/cakeReducer";
3 | import iceCreamReducer from "./iceCream/iceCreamReducer";
4 | import userReducer from "./user/userReducer";
5 | import postReducer from "./posts/postReducer";
6 |
7 | const rootReducer = combineReducers({
8 | cake: cakeReducer,
9 | iceCream: iceCreamReducer,
10 | users: userReducer,
11 | post: postReducer,
12 | });
13 |
14 | export default rootReducer;
15 |
--------------------------------------------------------------------------------
/src/redux/iceCream/iceCreamReducer.js:
--------------------------------------------------------------------------------
1 | import { BUY_ICECREAM } from './iceCreamTypes';
2 |
3 | const initialState = {
4 | numberOfIceCreams: 20,
5 | };
6 |
7 | const iceCreamReducer = (state = initialState, action) => {
8 | switch (action.type) {
9 | case BUY_ICECREAM:
10 | return {
11 | ...state,
12 | numberOfIceCreams: state.numberOfIceCreams - action.value,
13 | };
14 | default:
15 | return state;
16 | }
17 | };
18 |
19 | export default iceCreamReducer;
20 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import './index.css';
4 | import App from './App';
5 | import reportWebVitals from './reportWebVitals';
6 |
7 | ReactDOM.render(
8 |
9 |
10 | ,
11 | document.getElementById('root')
12 | );
13 |
14 | // If you want to start measuring performance in your app, pass a function
15 | // to log results (for example: reportWebVitals(console.log))
16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
17 | reportWebVitals();
18 |
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/src/redux/store.js:
--------------------------------------------------------------------------------
1 | import { createStore, applyMiddleware } from "redux";
2 | import rootReducer from "./rootReducer";
3 | import logger from "redux-logger";
4 | import { composeWithDevTools } from "redux-devtools-extension";
5 | import thunk from "redux-thunk";
6 |
7 | import createSagaMiddleware from "redux-saga";
8 | import { rootSaga } from "./sagas/rootSaga";
9 | // create the saga middleware
10 | const sagaMiddleware = createSagaMiddleware();
11 |
12 | const store = createStore(
13 | rootReducer,
14 | composeWithDevTools(applyMiddleware(logger, thunk, sagaMiddleware))
15 | );
16 |
17 | sagaMiddleware.run(rootSaga);
18 |
19 | export default store;
20 |
--------------------------------------------------------------------------------
/src/redux/sagas/postSaga.js:
--------------------------------------------------------------------------------
1 | import {takeEvery, call, put } from "@redux-saga/core/effects";
2 | import axios from "axios";
3 | import { FETCH_POSTS_REQUEST } from "../posts/postTypes";
4 | import { fetchUsersSuccess, fetchUsersFailure } from "../posts/postActions";
5 |
6 | function* fetchPost(action) {
7 | try {
8 | const apiEndPoint = `https://jsonplaceholder.typicode.com/posts/${action.payload}`;
9 | const response = yield call(() => axios.get(apiEndPoint));
10 | yield put(fetchUsersSuccess(response.data));
11 | } catch (e) {
12 | yield put(fetchUsersFailure(e.message));
13 | }
14 | }
15 |
16 | export function* watchFetchPost() {
17 | yield takeEvery(FETCH_POSTS_REQUEST, fetchPost);
18 | }
19 |
--------------------------------------------------------------------------------
/src/components/CakeContainer/CakeContainer.js:
--------------------------------------------------------------------------------
1 | import { connect } from "react-redux";
2 | import { buyCake } from "../../redux";
3 |
4 | const CakeContainer = (props) => {
5 | return (
6 |
7 |
Number of Cakes - {props.numberOfCakes}
8 |
9 |
10 | );
11 | };
12 |
13 | //? get redux state as prameter :
14 | const mapStatToProps = (state) => {
15 | return {
16 | numberOfCakes: state.cake.numberOfCakes,
17 | };
18 | };
19 |
20 | const mapDispatchToProps = (dispatch) => {
21 | return {
22 | buyCake: () => dispatch(buyCake()),
23 | };
24 | };
25 | export default connect(mapStatToProps, mapDispatchToProps)(CakeContainer);
26 |
27 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 | /src/components/AxiosLecture
8 | /src/components/Formik
9 | /src/components/LoginForm
10 | /src/components/SignUpForm
11 | /src/components/containers
12 | /src/components/hooksLecture
13 | /src/components/Table
14 | /src/common
15 | /src/utils
16 | /src/hoc
17 | /src/services
18 | /src/hooks
19 | /axios.js
20 | /src/config.json
21 | # testing
22 | /coverage
23 |
24 | # production
25 | /build
26 |
27 | # misc
28 | .DS_Store
29 | .env.local
30 | .env.development.local
31 | .env.test.local
32 | .env.production.local
33 |
34 | npm-debug.log*
35 | yarn-debug.log*
36 | yarn-error.log*
37 |
--------------------------------------------------------------------------------
/src/components/BuyIceCream/BuyIceCream.js:
--------------------------------------------------------------------------------
1 | import { connect } from "react-redux";
2 | import { buyIceCream } from "../../redux";
3 |
4 | const BuyIceCream = (props) => {
5 | return (
6 |
7 |
Number of Cakes - {props.numberOfIceCreams}
8 |
9 |
10 | );
11 | };
12 |
13 | //? get redux state as prameter :
14 | const mapStatToProps = (state) => {
15 | return {
16 | numberOfIceCreams: state.iceCream.numberOfIceCreams,
17 | };
18 | };
19 |
20 | const mapDispatchToProps = (dispatch) => {
21 | return {
22 | buyIceCream: () => dispatch(buyIceCream()),
23 | };
24 | };
25 | export default connect(mapStatToProps, mapDispatchToProps)(BuyIceCream);
26 |
--------------------------------------------------------------------------------
/src/components/HooksCakeContainer/HooksCakeContainer.js:
--------------------------------------------------------------------------------
1 | import { buyCake } from "../../redux";
2 | import { useSelector, useDispatch } from "react-redux";
3 |
4 | const HooksCakeContainer = (props) => {
5 | //? this hooks accepts a function as its parameter : the selector function
6 | //? selector fucntion receive redux stats as arguments === mapTostate
7 | //? useSelector hooks returns whatever returns by selector function
8 | const numberOfCakes = useSelector((state) => state.cake.numberOfCakes);
9 |
10 | //? useDispatch hooks returns a reference to the dispatch function in redux store
11 | const dispatch = useDispatch();
12 |
13 | return (
14 |
15 |
Number of Cakes - {numberOfCakes}
16 |
17 |
18 | );
19 | };
20 |
21 | export default HooksCakeContainer;
22 |
--------------------------------------------------------------------------------
/src/redux/posts/postReducer.js:
--------------------------------------------------------------------------------
1 | import {
2 | FETCH_POSTS_FAILURE,
3 | FETCH_POSTS_REQUEST,
4 | FETCH_POSTS_SUCCESS,
5 | } from "./postTypes";
6 |
7 | const initialState = {
8 | loading: true,
9 | data: [],
10 | error: "",
11 | };
12 |
13 | const postReducer = (state = initialState, action) => {
14 | switch (action.type) {
15 | case FETCH_POSTS_REQUEST:
16 | return {
17 | ...state,
18 | loading: true,
19 | };
20 | case FETCH_POSTS_SUCCESS:
21 | return {
22 | ...state,
23 | loading: false,
24 | data: action.payload,
25 | error: "",
26 | };
27 | case FETCH_POSTS_FAILURE:
28 | return {
29 | ...state,
30 | loading: false,
31 | data: [],
32 | error: action.payload,
33 | };
34 |
35 | default:
36 | return state;
37 | }
38 | };
39 |
40 | export default postReducer;
41 |
--------------------------------------------------------------------------------
/src/redux/user/userReducer.js:
--------------------------------------------------------------------------------
1 | import {
2 | FETCH_USERS_REQUEST,
3 | FETCH_USERS_SUCCESS,
4 | FETCH_USERS_FAILURE,
5 | } from "./userTypes";
6 |
7 | const initialState = {
8 | loading: true,
9 | data: [],
10 | error: "",
11 | };
12 |
13 | const userReducer = (state = initialState, action) => {
14 | switch (action.type) {
15 | case FETCH_USERS_REQUEST:
16 | return {
17 | ...state,
18 | loading: true,
19 | };
20 | case FETCH_USERS_SUCCESS:
21 | return {
22 | ...state,
23 | loading: false,
24 | data: action.payload,
25 | error: "",
26 | };
27 | case FETCH_USERS_FAILURE:
28 | return {
29 | ...state,
30 | loading: false,
31 | data: [],
32 | error: action.payload,
33 | };
34 |
35 | default:
36 | return state;
37 | }
38 | };
39 |
40 | export default userReducer;
41 |
--------------------------------------------------------------------------------
/src/components/CakeWtihPayload/CakeWtihPayload.js:
--------------------------------------------------------------------------------
1 | import { connect } from "react-redux";
2 | import { buyCake } from "../../redux";
3 | import { useState } from "react";
4 |
5 | const CakeWithPayload = ({ numberOfCakes, buyCake }) => {
6 | const [cake, setCake] = useState(0);
7 |
8 | return (
9 |
10 |
Number of Cakes - {numberOfCakes}
11 | setCake(e.target.value)}
15 | />
16 |
17 |
18 | );
19 | };
20 |
21 | //? get redux state as prameter :
22 | const mapStatToProps = (state) => {
23 | return {
24 | numberOfCakes: state.cake.numberOfCakes,
25 | };
26 | };
27 |
28 | const mapDispatchToProps = (dispatch) => {
29 | return {
30 | buyCake: (cake) => dispatch(buyCake(cake)),
31 | };
32 | };
33 |
34 | export default connect(mapStatToProps, mapDispatchToProps)(CakeWithPayload);
35 |
--------------------------------------------------------------------------------
/src/components/USerContainer/UserContainer.js:
--------------------------------------------------------------------------------
1 | import { connect } from "react-redux";
2 | import { useEffect } from "react";
3 | import { fetchUsers } from "../../redux";
4 |
5 | const UserContainer = ({ usersData, fetchUsers2 }) => {
6 | useEffect(() => {
7 | fetchUsers2();
8 | }, []);
9 |
10 | return usersData.loading ? (
11 | Loading
12 | ) : usersData.errror ? (
13 | {usersData.errror}
14 | ) : (
15 |
16 |
User List
17 |
18 | {usersData &&
19 | usersData.data &&
20 | usersData.data.map((user) =>
{user.name})}
21 |
22 |
23 | );
24 | };
25 |
26 | const mapStateToProps = (state) => {
27 | return {
28 | usersData: state.users,
29 | };
30 | };
31 |
32 | const mapDispatchToProps = (dispatch) => {
33 | return {
34 | fetchUsers2: () => dispatch(fetchUsers()),
35 | };
36 | };
37 |
38 | export default connect(mapStateToProps, mapDispatchToProps)(UserContainer);
39 |
--------------------------------------------------------------------------------
/src/redux/posts/postActions.js:
--------------------------------------------------------------------------------
1 | import {
2 | FETCH_POSTS_FAILURE,
3 | FETCH_POSTS_REQUEST,
4 | FETCH_POSTS_SUCCESS,
5 | } from "./postTypes";
6 |
7 | export const fetchUsersRequest = (id) => {
8 | return {
9 | type: FETCH_POSTS_REQUEST,
10 | payload: id,
11 | };
12 | };
13 |
14 | export const fetchUsersSuccess = (users) => {
15 | return {
16 | type: FETCH_POSTS_SUCCESS,
17 | payload: users,
18 | };
19 | };
20 |
21 | export const fetchUsersFailure = (error) => {
22 | return {
23 | type: FETCH_POSTS_FAILURE,
24 | payload: error,
25 | };
26 | };
27 |
28 | // export const fetchUsers = () => {
29 | // return (dispatch) => {
30 | // dispatch(fetchUsersRequest());
31 | // axios
32 | // .get("https://jsonplaceholder.typicode.com/posts")
33 | // .then((response) => {
34 | // dispatch(fetchUsersSuccess(response.data));
35 | // })
36 | // .catch((error) => {
37 | // dispatch(fetchUsersFailure(error.message));
38 | // });
39 | // };
40 | // };
41 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import "./App.css";
2 | import CakeContainer from "./components/CakeContainer/CakeContainer";
3 | import { Provider } from "react-redux";
4 | import store from "./redux/store";
5 | import HooksCakeContainer from "./components/HooksCakeContainer/HooksCakeContainer";
6 | import BuyIceCream from "./components/BuyIceCream/BuyIceCream";
7 | import CakeWithPaylod from "./components/CakeWtihPayload/CakeWtihPayload";
8 | import ItemContainer from "./components/ItemContainer/ItemContainer";
9 | import UserContainer from "./components/USerContainer/UserContainer";
10 | import PostSaga from "./components/postSaga/postSaga";
11 |
12 | function App() {
13 | return (
14 |
15 |
16 |
17 | {/*
18 |
*/}
19 | {/*
*/}
20 | {/*
*/}
21 | {/*
*/}
22 | {/*
*/}
23 | {/*
*/}
24 |
25 |
26 | );
27 | }
28 |
29 | export default App;
30 |
--------------------------------------------------------------------------------
/src/components/postSaga/postSaga.js:
--------------------------------------------------------------------------------
1 | import { useDispatch, useSelector } from "react-redux";
2 | import { useState } from "react";
3 | import { fetchUsersRequest } from "../../redux/posts/postActions";
4 |
5 | const PostSaga = () => {
6 | const [postId, setPostId] = useState("");
7 | const postData = useSelector((state) => state.post);
8 | const dispatch = useDispatch();
9 |
10 | return (
11 | <>
12 | Redux-Saga middleware
13 | setPostId(e.target.value)}
17 | />
18 |
21 | {postData.loading ? (
22 | Loading ...
23 | ) : postData.error ? (
24 | {postData.error}
25 | ) : (
26 |
27 | post NO {postData.data.id}
28 | title : {postData.data.title}
29 | body : {postData.data.body}
30 |
31 | )}
32 | >
33 | );
34 | };
35 |
36 | export default PostSaga;
37 |
--------------------------------------------------------------------------------
/src/redux/user/userActions.js:
--------------------------------------------------------------------------------
1 | import axios from "axios";
2 | import {
3 | FETCH_USERS_REQUEST,
4 | FETCH_USERS_SUCCESS,
5 | FETCH_USERS_FAILURE,
6 | } from "./userTypes";
7 |
8 | export const fetchUsersRequest = () => {
9 | return {
10 | type: FETCH_USERS_REQUEST,
11 | };
12 | };
13 |
14 | export const fetchUsersSuccess = (users) => {
15 | return {
16 | type: FETCH_USERS_SUCCESS,
17 | payload: users,
18 | };
19 | };
20 |
21 | export const fetchUsersFailure = (error) => {
22 | return {
23 | type: FETCH_USERS_FAILURE,
24 | payload: error,
25 | };
26 | };
27 |
28 | //? we dispatch the appropiate actions :
29 | export const fetchUsers = () => {
30 | //? recieves the dispach method as arguments
31 | return (dispatch) => {
32 | dispatch(fetchUsersRequest());
33 | axios
34 | .get("https://jsonplaceholder.typicode.com/users")
35 | .then((response) => {
36 | // const users = response.data.map((user) => user.name);
37 | dispatch(fetchUsersSuccess(response.data));
38 | // dispatch({type:FETCH_USERS_SUCCESS,payload:response.data});
39 | })
40 | .catch((error) => {
41 | dispatch(fetchUsersFailure(error.message));
42 | });
43 | };
44 | };
45 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "redux-structure",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@testing-library/jest-dom": "^5.11.9",
7 | "@testing-library/react": "^11.2.3",
8 | "@testing-library/user-event": "^12.6.0",
9 | "axios": "^0.21.1",
10 | "mobx": "^6.1.8",
11 | "mobx-react": "^7.1.0",
12 | "react": "^17.0.1",
13 | "react-dom": "^17.0.1",
14 | "react-redux": "^7.2.2",
15 | "react-scripts": "4.0.1",
16 | "redux": "^4.0.5",
17 | "redux-devtools-extension": "^2.13.8",
18 | "redux-logger": "^3.0.6",
19 | "redux-saga": "^1.1.3",
20 | "redux-thunk": "^2.3.0",
21 | "web-vitals": "^0.2.4"
22 | },
23 | "scripts": {
24 | "start": "react-scripts start",
25 | "build": "react-scripts build",
26 | "test": "react-scripts test",
27 | "eject": "react-scripts eject"
28 | },
29 | "eslintConfig": {
30 | "extends": [
31 | "react-app",
32 | "react-app/jest"
33 | ]
34 | },
35 | "browserslist": {
36 | "production": [
37 | ">0.2%",
38 | "not dead",
39 | "not op_mini all"
40 | ],
41 | "development": [
42 | "last 1 chrome version",
43 | "last 1 firefox version",
44 | "last 1 safari version"
45 | ]
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/components/ItemContainer/ItemContainer.js:
--------------------------------------------------------------------------------
1 | import { connect } from "react-redux";
2 | import { buyCake, buyIceCream } from "../../redux";
3 | import { useState } from "react";
4 |
5 | const CakeWithPayload = ({ item, buyItem }) => {
6 | return (
7 |
8 |
Item - {item}
9 |
10 |
11 | );
12 | };
13 |
14 | //? get redux state as prameter :
15 | //? this function accepts two parameters : (redux-state,props of the component itSelf)
16 |
17 | const mapStatToProps = (state, ownProps) => {
18 | //? conditionally assign the reudx state :
19 | //? if cake props was passed in
20 | const itemState = ownProps.cake
21 | ? state.cake.numberOfCakes
22 | : state.iceCream.numberOfIceCreams;
23 | return {
24 | item: itemState,
25 | };
26 | };
27 |
28 | const mapDispatchToProps = (dispatch, ownProps) => {
29 | const dispatchFunction = ownProps.cake
30 | ? () => dispatch(buyCake())
31 | : () => dispatch(buyIceCream());
32 | return {
33 | buyItem: dispatchFunction,
34 | };
35 | };
36 |
37 | //? the common use case is when you click on the particular item you pass
38 | //? the Id as props and then fetch the data from redux for that producnt
39 | export default connect(mapStatToProps, mapDispatchToProps)(CakeWithPayload);
40 |
--------------------------------------------------------------------------------
/redux-basics.js:
--------------------------------------------------------------------------------
1 | const redux = require("redux");
2 | const reduxLogger = require("redux-logger");
3 |
4 | const createStore = redux.createStore;
5 | const combineReducers = redux.combineReducers;
6 | const applyMiddleWare = redux.applyMiddleware;
7 | const logger = reduxLogger.createLogger();
8 |
9 | //? ACTION
10 | const BUY_CAKE = "BUT_CAKE";
11 | const BUY_ICECREAM = "BUY_ICECREAM";
12 |
13 | function buyCake() {
14 | return {
15 | type: BUY_CAKE,
16 | };
17 | }
18 | function buyIcecream(value) {
19 | return {
20 | type: BUY_ICECREAM,
21 | value: value,
22 | };
23 | }
24 | //? REDUCER : (prevState,action)=> newState
25 | const initialCakeState = {
26 | numberOfCakes: 10,
27 | };
28 | const initialIcecreamState = {
29 | numberOfIcecreams: 20,
30 | };
31 | const cakeReducer = (state = initialCakeState, action) => {
32 | switch (action.type) {
33 | case BUY_CAKE:
34 | return {
35 | ...state,
36 | numberOfCakes: state.numberOfCakes - 1,
37 | };
38 | default:
39 | return state;
40 | }
41 | };
42 | const icecreamReducer = (state = initialIcecreamState, action) => {
43 | switch (action.type) {
44 | case "BUY_ICECREAM":
45 | return {
46 | ...state,
47 | numberOfIcecreams: state.numberOfIcecreams - action.value,
48 | };
49 | default:
50 | return state;
51 | }
52 | };
53 |
54 | //? STORE
55 | const rootReducer = combineReducers({
56 | cake: cakeReducer,
57 | icecream: icecreamReducer,
58 | });
59 | const store = createStore(rootReducer, applyMiddleWare(logger));
60 |
61 | //? DISPATCHING AN ACTION :
62 | const unsubscribe = store.subscribe(() => console.log(store.getState()));
63 | store.dispatch(buyCake());
64 | store.dispatch(buyIcecream(4));
65 |
66 | //? SUBSCRIPTION :
67 | unsubscribe();
68 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 |
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/asyncActions.js:
--------------------------------------------------------------------------------
1 | const axios = require("axios");
2 | const redux = require("redux");
3 | const createStore = redux.createStore;
4 | const applyMiddleWare = redux.applyMiddleware;
5 | const thunkMiddleWare = require("redux-thunk").default;
6 |
7 | //? actions
8 | const FETCH_USERS_REQUEST = "FETCH_USERS_REQUEST";
9 | const FETCH_USERS_SUCCESS = "FETCH_USERS_SUCCESS";
10 | const FETCH_USERS_FAILURE = "FETCH_USERS_FAILURE";
11 |
12 | const fetchUsersRequest = () => {
13 | return {
14 | type: FETCH_USERS_REQUEST,
15 | };
16 | };
17 |
18 | const fetchUsersSuccess = (users) => {
19 | return {
20 | type: FETCH_USERS_SUCCESS,
21 | payload: users,
22 | };
23 | };
24 |
25 | const fetchUsersFailure = (error) => {
26 | return {
27 | type: FETCH_USERS_FAILURE,
28 | payload: error,
29 | };
30 | };
31 |
32 | //? REDUCER :
33 | const initialState = {
34 | loading: false,
35 | users: [],
36 | error: "",
37 | };
38 |
39 | const reducer = (state = initialState, action) => {
40 | switch (action.type) {
41 | case FETCH_USERS_REQUEST:
42 | return {
43 | ...state,
44 | loading: true,
45 | };
46 | case FETCH_USERS_SUCCESS:
47 | return {
48 | ...state,
49 | loading: false,
50 | users: action.payload,
51 | error: "",
52 | };
53 | case FETCH_USERS_FAILURE:
54 | return {
55 | ...state,
56 | loading: false,
57 | users: [],
58 | error: action.payload,
59 | };
60 |
61 | default:
62 | return state;
63 | }
64 | };
65 |
66 | const fetchUsers = () => {
67 | return function (dispatch) {
68 | dispatch(fetchUsersRequest());
69 | axios
70 | .get("https://jsonplaceholder.typicode.com/users")
71 | .then((response) => {
72 | const users = response.data.map((user) => user.id);
73 | dispatch(fetchUsersSuccess(users));
74 | })
75 | .catch((err) => dispatch(fetchUsersFailure(err)));
76 | };
77 | };
78 |
79 | //? STORE
80 |
81 | const store = createStore(reducer, applyMiddleWare(thunkMiddleWare));
82 | store.subscribe(() => console.log(store.getState()));
83 | store.dispatch(fetchUsers());
84 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
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 the browser.
13 |
14 | The page will reload if you make edits.\
15 | You will 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)
71 |
--------------------------------------------------------------------------------
/.eslintcache:
--------------------------------------------------------------------------------
1 | [{"D:\\projects\\redux-course\\src\\index.js":"1","D:\\projects\\redux-course\\src\\App.js":"2","D:\\projects\\redux-course\\src\\reportWebVitals.js":"3","D:\\projects\\redux-course\\src\\redux\\store.js":"4","D:\\projects\\redux-course\\src\\components\\HooksCakeContainer\\HooksCakeContainer.js":"5","D:\\projects\\redux-course\\src\\components\\CakeContainer\\CakeContainer.js":"6","D:\\projects\\redux-course\\src\\components\\CakeWtihPayload\\CakeWtihPayload.js":"7","D:\\projects\\redux-course\\src\\components\\postSaga\\postSaga.js":"8","D:\\projects\\redux-course\\src\\components\\ItemContainer\\ItemContainer.js":"9","D:\\projects\\redux-course\\src\\components\\USerContainer\\UserContainer.js":"10","D:\\projects\\redux-course\\src\\components\\BuyIceCream\\BuyIceCream.js":"11","D:\\projects\\redux-course\\src\\redux\\rootReducer.js":"12","D:\\projects\\redux-course\\src\\redux\\sagas\\rootSaga.js":"13","D:\\projects\\redux-course\\src\\redux\\posts\\postActions.js":"14","D:\\projects\\redux-course\\src\\redux\\index.js":"15","D:\\projects\\redux-course\\src\\redux\\posts\\postReducer.js":"16","D:\\projects\\redux-course\\src\\redux\\cake\\cakeReducer.js":"17","D:\\projects\\redux-course\\src\\redux\\iceCream\\iceCreamReducer.js":"18","D:\\projects\\redux-course\\src\\redux\\user\\userReducer.js":"19","D:\\projects\\redux-course\\src\\redux\\posts\\postTypes.js":"20","D:\\projects\\redux-course\\src\\redux\\user\\userActions.js":"21","D:\\projects\\redux-course\\src\\redux\\iceCream\\iceCreamActions.js":"22","D:\\projects\\redux-course\\src\\redux\\cake\\cakeActions.js":"23","D:\\projects\\redux-course\\src\\redux\\cake\\cakeTypes.js":"24","D:\\projects\\redux-course\\src\\redux\\iceCream\\iceCreamTypes.js":"25","D:\\projects\\redux-course\\src\\redux\\user\\userTypes.js":"26","D:\\projects\\redux-course\\src\\redux\\sagas\\postSaga.js":"27"},{"size":517,"mtime":1636999944241,"results":"28","hashOfConfig":"29"},{"size":1016,"mtime":1637008565712,"results":"30","hashOfConfig":"29"},{"size":375,"mtime":1636999944251,"results":"31","hashOfConfig":"29"},{"size":586,"mtime":1637005375200,"results":"32","hashOfConfig":"29"},{"size":766,"mtime":1636999944238,"results":"33","hashOfConfig":"29"},{"size":606,"mtime":1636999944207,"results":"34","hashOfConfig":"29"},{"size":841,"mtime":1636999944208,"results":"35","hashOfConfig":"29"},{"size":989,"mtime":1637015249193,"results":"36","hashOfConfig":"29"},{"size":1166,"mtime":1636999944239,"results":"37","hashOfConfig":"29"},{"size":843,"mtime":1636999944240,"results":"38","hashOfConfig":"29"},{"size":639,"mtime":1636999944206,"results":"39","hashOfConfig":"29"},{"size":417,"mtime":1637008193090,"results":"40","hashOfConfig":"29"},{"size":159,"mtime":1637014152175,"results":"41","hashOfConfig":"29"},{"size":871,"mtime":1637008408241,"results":"42","hashOfConfig":"29"},{"size":143,"mtime":1636999944247,"results":"43","hashOfConfig":"29"},{"size":746,"mtime":1637008758602,"results":"44","hashOfConfig":"29"},{"size":380,"mtime":1636999944243,"results":"45","hashOfConfig":"29"},{"size":410,"mtime":1636999944245,"results":"46","hashOfConfig":"29"},{"size":746,"mtime":1636999944250,"results":"47","hashOfConfig":"29"},{"size":177,"mtime":1637005565905,"results":"48","hashOfConfig":"29"},{"size":1068,"mtime":1636999944249,"results":"49","hashOfConfig":"29"},{"size":149,"mtime":1636999944245,"results":"50","hashOfConfig":"29"},{"size":176,"mtime":1636999944242,"results":"51","hashOfConfig":"29"},{"size":34,"mtime":1636999944244,"results":"52","hashOfConfig":"29"},{"size":42,"mtime":1636999944246,"results":"53","hashOfConfig":"29"},{"size":177,"mtime":1636999944250,"results":"54","hashOfConfig":"29"},{"size":640,"mtime":1637014175945,"results":"55","hashOfConfig":"29"},{"filePath":"56","messages":"57","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"jcad4f",{"filePath":"58","messages":"59","errorCount":0,"warningCount":6,"fixableErrorCount":0,"fixableWarningCount":0,"source":null},{"filePath":"60","messages":"61","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"62","messages":"63","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"64","messages":"65","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"66","messages":"67","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"68","messages":"69","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"70","messages":"71","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"72","messages":"73","errorCount":0,"warningCount":1,"fixableErrorCount":0,"fixableWarningCount":0,"source":null},{"filePath":"74","messages":"75","errorCount":0,"warningCount":1,"fixableErrorCount":0,"fixableWarningCount":0,"source":null},{"filePath":"76","messages":"77","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"78","messages":"79","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"80","messages":"81","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"82","messages":"83","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"84","messages":"85","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"86","messages":"87","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"88","messages":"89","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"90","messages":"91","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"92","messages":"93","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"94","messages":"95","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"96","messages":"97","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"98","messages":"99","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"100","messages":"101","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"102","messages":"103","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"104","messages":"105","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"106","messages":"107","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},{"filePath":"108","messages":"109","errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"D:\\projects\\redux-course\\src\\index.js",[],"D:\\projects\\redux-course\\src\\App.js",["110","111","112","113","114","115"],"D:\\projects\\redux-course\\src\\reportWebVitals.js",[],"D:\\projects\\redux-course\\src\\redux\\store.js",[],"D:\\projects\\redux-course\\src\\components\\HooksCakeContainer\\HooksCakeContainer.js",[],"D:\\projects\\redux-course\\src\\components\\CakeContainer\\CakeContainer.js",[],"D:\\projects\\redux-course\\src\\components\\CakeWtihPayload\\CakeWtihPayload.js",[],"D:\\projects\\redux-course\\src\\components\\postSaga\\postSaga.js",[],"D:\\projects\\redux-course\\src\\components\\ItemContainer\\ItemContainer.js",["116"],"D:\\projects\\redux-course\\src\\components\\USerContainer\\UserContainer.js",["117"],"D:\\projects\\redux-course\\src\\components\\BuyIceCream\\BuyIceCream.js",[],"D:\\projects\\redux-course\\src\\redux\\rootReducer.js",[],"D:\\projects\\redux-course\\src\\redux\\sagas\\rootSaga.js",[],"D:\\projects\\redux-course\\src\\redux\\posts\\postActions.js",[],"D:\\projects\\redux-course\\src\\redux\\index.js",[],"D:\\projects\\redux-course\\src\\redux\\posts\\postReducer.js",[],"D:\\projects\\redux-course\\src\\redux\\cake\\cakeReducer.js",[],"D:\\projects\\redux-course\\src\\redux\\iceCream\\iceCreamReducer.js",[],"D:\\projects\\redux-course\\src\\redux\\user\\userReducer.js",[],"D:\\projects\\redux-course\\src\\redux\\posts\\postTypes.js",[],"D:\\projects\\redux-course\\src\\redux\\user\\userActions.js",[],"D:\\projects\\redux-course\\src\\redux\\iceCream\\iceCreamActions.js",[],"D:\\projects\\redux-course\\src\\redux\\cake\\cakeActions.js",[],"D:\\projects\\redux-course\\src\\redux\\cake\\cakeTypes.js",[],"D:\\projects\\redux-course\\src\\redux\\iceCream\\iceCreamTypes.js",[],"D:\\projects\\redux-course\\src\\redux\\user\\userTypes.js",[],"D:\\projects\\redux-course\\src\\redux\\sagas\\postSaga.js",[],{"ruleId":"118","severity":1,"message":"119","line":2,"column":8,"nodeType":"120","messageId":"121","endLine":2,"endColumn":21},{"ruleId":"118","severity":1,"message":"122","line":5,"column":8,"nodeType":"120","messageId":"121","endLine":5,"endColumn":26},{"ruleId":"118","severity":1,"message":"123","line":6,"column":8,"nodeType":"120","messageId":"121","endLine":6,"endColumn":19},{"ruleId":"118","severity":1,"message":"124","line":7,"column":8,"nodeType":"120","messageId":"121","endLine":7,"endColumn":22},{"ruleId":"118","severity":1,"message":"125","line":8,"column":8,"nodeType":"120","messageId":"121","endLine":8,"endColumn":21},{"ruleId":"118","severity":1,"message":"126","line":9,"column":8,"nodeType":"120","messageId":"121","endLine":9,"endColumn":21},{"ruleId":"118","severity":1,"message":"127","line":3,"column":10,"nodeType":"120","messageId":"121","endLine":3,"endColumn":18},{"ruleId":"128","severity":1,"message":"129","line":8,"column":6,"nodeType":"130","endLine":8,"endColumn":8,"suggestions":"131"},"no-unused-vars","'CakeContainer' is defined but never used.","Identifier","unusedVar","'HooksCakeContainer' is defined but never used.","'BuyIceCream' is defined but never used.","'CakeWithPaylod' is defined but never used.","'ItemContainer' is defined but never used.","'UserContainer' is defined but never used.","'useState' is defined but never used.","react-hooks/exhaustive-deps","React Hook useEffect has a missing dependency: 'fetchUsers2'. Either include it or remove the dependency array. If 'fetchUsers2' changes too often, find the parent component that defines it and wrap that definition in useCallback.","ArrayExpression",["132"],{"desc":"133","fix":"134"},"Update the dependencies array to be: [fetchUsers2]",{"range":"135","text":"136"},[224,226],"[fetchUsers2]"]
--------------------------------------------------------------------------------