Skip to content

Commit 7281934

Browse files
author
WebDeveloperGuide
committed
Apply middleware in redux, Redux thunk.
1 parent 90d0cf2 commit 7281934

File tree

9 files changed

+65
-55
lines changed

9 files changed

+65
-55
lines changed

web_panel/package-lock.json

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

web_panel/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"react-slick": "^0.28.1",
1818
"react-toastify": "^8.2.0",
1919
"redux": "^4.1.2",
20+
"redux-thunk": "^2.4.1",
2021
"slick-carousel": "^1.8.1",
2122
"styled-components": "^5.3.3",
2223
"web-vitals": "^2.1.4"

web_panel/src/components/FeaturedProducts.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, {useEffect} from 'react';
2-
import axios from 'axios';
32
import {Link} from 'react-router-dom';
43
import {useSelector,useDispatch} from 'react-redux';
5-
import {setProducts} from '../redux/actions/productActions';
4+
import {getProducts} from '../redux/actions/productActions';
65
import Product from './Product';
76
import Loading from '../components/Loading';
87

@@ -18,16 +17,8 @@ const FeaturedProducts = () => {
1817

1918
const dispatch = useDispatch();
2019

21-
const fetchProducts = async() =>{
22-
const response = await axios.get("https://dummyjson.com/products").catch((err) =>{
23-
console.log(err);
24-
});
25-
26-
dispatch(setProducts(response.data.products));
27-
};
28-
2920
useEffect(()=>{
30-
fetchProducts();
21+
dispatch(getProducts());
3122
},[])
3223

3324
return(

web_panel/src/components/Products.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ import React,{useEffect} from 'react';
22
import {useSelector,useDispatch} from 'react-redux';
33
import Filters from './Filters';
44
import Product from './Product';
5-
import axios from 'axios';
6-
import {setProducts} from '../redux/actions/productActions';
5+
import {getProducts} from '../redux/actions/productActions';
76
import Loading from '../components/Loading';
87

98
const Products = () => {
109

1110
const products = useSelector((state)=> state.allProducts.products);
12-
console.log("products",products)
11+
1312
const renderList = products.map((product)=>{
1413
return(
1514
<Product detail={product} key={product.id}/>
@@ -18,16 +17,8 @@ const Products = () => {
1817

1918
const dispatch = useDispatch();
2019

21-
const fetchProducts = async() =>{
22-
const response = await axios.get("https://dummyjson.com/products").catch((err) =>{
23-
console.log(err);
24-
});
25-
26-
dispatch(setProducts(response.data.products));
27-
};
28-
2920
useEffect(()=>{
30-
fetchProducts();
21+
dispatch(getProducts());
3122
},[])
3223

3324
return(

web_panel/src/pages/ProductDetailPage.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import ProductDetail from '../components/ProductDetail';
77
import Sidebar from '../components/Sidebar';
88
import Cart from '../components/Cart';
99
import Loading from '../components/Loading';
10-
import axios from 'axios';
1110
import {setProductDetail,resetProductDetail} from '../redux/actions/productActions';
1211
import { toast } from 'react-toastify';
1312
import {ToastObjects} from "../util/toastObject";
@@ -18,22 +17,11 @@ const ProductDetailPage = () => {
1817
const {id} = useParams();
1918
const productDetail = useSelector((state)=> state.allProducts.productDetail);
2019

21-
const dispatch = useDispatch();
22-
23-
const fetchProductDetail = async() =>{
24-
const response = await axios.get(`https://dummyjson.com/products/${id}`).catch((err) =>{
25-
toast.error("No data found", ToastObjects);
26-
});
27-
28-
if(response){
29-
dispatch(setProductDetail(response.data));
30-
}
31-
32-
};
20+
const dispatch = useDispatch();
3321

3422
useEffect(()=>{
3523
if(id && id != ""){
36-
fetchProductDetail();
24+
dispatch(setProductDetail(id));
3725
}
3826

3927
return () => {
Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
11
import { ActionTypes } from '../constants';
2+
import axios from 'axios';
3+
import { toast } from "react-toastify";
4+
import {ToastObjects} from "../../util/toastObject";
25

3-
export const setProducts = (products) =>{
4-
return{
5-
type:ActionTypes.SET_PRODUCTS,
6-
payload:products
7-
}
6+
export const getProducts = () => async(dispatch) =>{
7+
try{
8+
const response = await axios.get("https://dummyjson.com/products");
9+
dispatch({ type: ActionTypes.FETCH_PRODUCTS, payload: response.data.products });
10+
11+
} catch (error){
12+
13+
const message =
14+
error.response && error.response.data.message
15+
? error.response.data.message
16+
: error.message;
17+
18+
toast.error(message, ToastObjects);
19+
}
820
}
921

10-
export const setProductDetail = (product) =>{
11-
return{
12-
type:ActionTypes.SET_PRODUCT_DETAIL,
13-
payload:product
22+
23+
export const setProductDetail = (id) => async(dispatch) =>{
24+
try{
25+
const response = await axios.get(`https://dummyjson.com/products/${id}`);
26+
console.log(response)
27+
dispatch({ type: ActionTypes.SET_PRODUCT_DETAIL, payload: response.data });
28+
29+
} catch (error){
30+
31+
const message =
32+
error.response && error.response.data.message
33+
? error.response.data.message
34+
: error.message;
35+
36+
toast.error(message, ToastObjects);
1437
}
1538
}
1639

17-
export const resetProductDetail = () =>{
18-
return{
19-
type:ActionTypes.RESET_PRODUCT_DETAIL
20-
}
40+
export const resetProductDetail = () => async(dispatch) =>{
41+
dispatch({ type: ActionTypes.RESET_PRODUCT_DETAIL});
2142
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const ActionTypes = {
2-
SET_PRODUCTS:"SET_PRODUCTS",
2+
FETCH_PRODUCTS:"FETCH_PRODUCTS",
33
SET_PRODUCT_DETAIL:"SET_PRODUCT_DETAIL",
44
RESET_PRODUCT_DETAIL:"RESET_PRODUCT_DETAIL"
55
}

web_panel/src/redux/reducers/productReducer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const initialState = {
77

88
export const productReducer = (state = initialState,{type,payload}) => {
99
switch(type){
10-
case ActionTypes.SET_PRODUCTS:
10+
case ActionTypes.FETCH_PRODUCTS:
1111
return {...state,products:payload};
1212
case ActionTypes.SET_PRODUCT_DETAIL:
1313
return {...state,productDetail:payload};

web_panel/src/redux/store.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { createStore } from "redux";
1+
import { createStore, applyMiddleware, compose } from "redux";
22
import reducers from "./reducers";
3+
import thunk from 'redux-thunk';
4+
5+
const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
6+
37

48
const store = createStore(
59
reducers,
6-
{},
7-
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
10+
composeEnhancer(applyMiddleware(thunk))
811
);
912

1013
export default store;

0 commit comments

Comments
 (0)