Skip to content

Commit b8db062

Browse files
author
WebDeveloperGuide
committed
Clean Up some code, Products page > Show new products as user scroll down.
1 parent 8d619f1 commit b8db062

File tree

6 files changed

+46
-24
lines changed

6 files changed

+46
-24
lines changed

web_panel/public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
rel="stylesheet"
3030
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css"
3131
/>
32-
<title>React App</title>
32+
<title>Online Shopping Store</title>
3333
</head>
3434
<body>
3535
<noscript>You need to enable JavaScript to run this app.</noscript>

web_panel/src/components/Products.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ import React,{useEffect,useState} from 'react';
22
import {useSelector,useDispatch} from 'react-redux';
33
import Filters from './Filters';
44
import Product from './Product';
5-
import {getProducts} from '../redux/actions/productActions';
5+
import {getProducts,resetProducts} from '../redux/actions/productActions';
66
import Loading from '../components/Loading';
77

88
const Products = () => {
99

10+
const dispatch = useDispatch();
1011
const [currentPage, setCurrentPage] = useState(0);
1112
const [priceFilter,setPriceFilter] = useState(10000);
1213
const [searchTerm, setSearchTerm] = useState('');
1314
const [sortByFilter, setSortByFilter] = useState('');
15+
const allProducts = useSelector((state)=> state.allProducts);
16+
const { products, numOfPages, sortBy, searchText, price } = allProducts;
17+
const productsPerPage = 9;
1418

1519
const changePrice = (e) => {
1620
setPriceFilter(e.target.value)
@@ -20,40 +24,47 @@ const Products = () => {
2024
setSearchTerm(e.target.value)
2125
}
2226

23-
const allProducts = useSelector((state)=> state.allProducts);
24-
const { products, numOfPages, sortBy, searchText, price } = allProducts;
27+
const loadMoreProduct = (e) => {
28+
if (window.innerHeight + document.documentElement.scrollTop > (document.documentElement.offsetHeight - 200)) {
29+
setCurrentPage((page) => page + 1);
30+
}
31+
};
2532

2633

2734
const renderList = products.map((product)=>{
2835
return(
2936
<Product detail={product} key={product._id}/>
3037
)
3138
})
32-
33-
const dispatch = useDispatch();
34-
let pageNum = 0;
35-
let productsPerPage = 9;
3639

3740

3841
useEffect(()=>{
39-
dispatch(getProducts(pageNum,productsPerPage, sortBy, searchText, price));
40-
},[])
41-
42+
if(currentPage <= numOfPages){
43+
dispatch(getProducts(currentPage,productsPerPage, sortBy, searchTerm, price));
44+
}
45+
},[currentPage])
4246

47+
4348
//Call Function after stop typing text
4449
useEffect(() => {
45-
const delaySearchFunc = setTimeout(() => {
46-
setCurrentPage(0);
47-
dispatch(getProducts(pageNum,productsPerPage, sortByFilter, searchTerm, priceFilter));
50+
dispatch(resetProducts());
51+
const delaySearchFunc = setTimeout(() => {
52+
setCurrentPage(0);
4853
}, 1500)
4954

50-
return () => clearTimeout(delaySearchFunc)
55+
window.addEventListener("scroll", loadMoreProduct);
56+
57+
return () => {
58+
clearTimeout(delaySearchFunc);
59+
window.removeEventListener("scroll", loadMoreProduct);
60+
}
61+
5162
}, [searchTerm, priceFilter])
5263

5364
const handleSortBy = (e) => {
5465
const sortByValue = e.target.value;
5566
setCurrentPage(0);
56-
dispatch(getProducts(pageNum,productsPerPage, sortByValue, searchText, price));
67+
dispatch(getProducts(currentPage,productsPerPage, sortByValue, searchTerm, price));
5768
}
5869

5970
return(

web_panel/src/index.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ Single Product Page
560560
.single-product-img {
561561
height: 25rem;
562562
border-radius: var(--borderRadius);
563+
object-fit:contain;
563564
}
564565
.single-product-title {
565566
margin-bottom: 0;

web_panel/src/redux/actions/productActions.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ActionTypes } from '../constants';
22
import axios from 'axios';
33
import { toast } from "react-toastify";
44
import {ToastObjects} from "../../util/toastObject";
5+
const { FETCH_PRODUCTS, SET_PRODUCT_DETAIL, RESET_PRODUCT_DETAIL, RESET_PRODUCTS} = ActionTypes;
56

67
export const getProducts = (pageNum,productsPerPage,sortBy,searchText,price) => async(dispatch) =>{
78
try{
@@ -12,7 +13,7 @@ export const getProducts = (pageNum,productsPerPage,sortBy,searchText,price) =>
1213
responseData['searchText'] = searchText;
1314
responseData['price'] = price;
1415

15-
dispatch({ type: ActionTypes.FETCH_PRODUCTS, payload: responseData });
16+
dispatch({ type: FETCH_PRODUCTS, payload: responseData });
1617

1718
} catch (error){
1819

@@ -29,7 +30,7 @@ export const getProducts = (pageNum,productsPerPage,sortBy,searchText,price) =>
2930
export const setProductDetail = (id) => async(dispatch) =>{
3031
try{
3132
const response = await axios.get(`products/find/${id}`);
32-
dispatch({ type: ActionTypes.SET_PRODUCT_DETAIL, payload: response.data });
33+
dispatch({ type: SET_PRODUCT_DETAIL, payload: response.data });
3334

3435
} catch (error){
3536

@@ -43,5 +44,9 @@ export const setProductDetail = (id) => async(dispatch) =>{
4344
}
4445

4546
export const resetProductDetail = () => async(dispatch) =>{
46-
dispatch({ type: ActionTypes.RESET_PRODUCT_DETAIL});
47+
dispatch({ type: RESET_PRODUCT_DETAIL});
48+
}
49+
50+
export const resetProducts = () => async(dispatch) =>{
51+
dispatch({ type: RESET_PRODUCTS});
4752
}

web_panel/src/redux/constants/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export const ActionTypes = {
1010
CART_SAVE_SHIPPING_ADDRESS:"CART_SAVE_SHIPPING_ADDRESS",
1111
CART_SAVE_PAYMENT_METHOD:"CART_SAVE_PAYMENT_METHOD",
1212
CLEAR_CART_ITEM:"CLEAR_CART_ITEM",
13-
CART_REMOVE_SHIPPING_ADDRESS:"CART_REMOVE_SHIPPING_ADDRESS"
13+
CART_REMOVE_SHIPPING_ADDRESS:"CART_REMOVE_SHIPPING_ADDRESS",
14+
RESET_PRODUCTS:"RESET_PRODUCTS"
1415
}
1516

1617

web_panel/src/redux/reducers/productReducer.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ActionTypes } from '../constants';
2+
const { FETCH_PRODUCTS, SET_PRODUCT_DETAIL, RESET_PRODUCT_DETAIL, RESET_PRODUCTS} = ActionTypes;
23

34
const initialState = {
45
products:[],
@@ -10,13 +11,16 @@ const initialState = {
1011
}
1112

1213
export const productReducer = (state = initialState,{type,payload}) => {
14+
1315
switch(type){
14-
case ActionTypes.FETCH_PRODUCTS:
15-
return {...state,products:payload.data, numOfPages:payload.numOfPages, sortBy:payload.sortBy, searchText:payload.searchText, price:payload.price};
16-
case ActionTypes.SET_PRODUCT_DETAIL:
16+
case FETCH_PRODUCTS:
17+
return {...state,products:[...state.products,...payload.data], numOfPages:payload.numOfPages, sortBy:payload.sortBy, searchText:payload.searchText, price:payload.price};
18+
case SET_PRODUCT_DETAIL:
1719
return {...state,productDetail:payload};
18-
case ActionTypes.RESET_PRODUCT_DETAIL:
20+
case RESET_PRODUCT_DETAIL:
1921
return {...state,productDetail:[]};
22+
case RESET_PRODUCTS:
23+
return {...state,products:[]};
2024
default:
2125
return state;
2226
}

0 commit comments

Comments
 (0)