Skip to content

Commit b0ac1aa

Browse files
author
WebDeveloperGuide
committed
Products List > Pagination added
1 parent eec43ea commit b0ac1aa

File tree

5 files changed

+61
-13
lines changed

5 files changed

+61
-13
lines changed

web_admin/src/App.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,13 @@ textarea.form-control, select.form-control{
5252
.fa-trash{
5353
color: #d2322d;
5454
}
55+
56+
.page-link{
57+
color:#172d88;
58+
}
59+
60+
.page-item.active .page-link, .pagination .page-item .page-link:focus{
61+
background-color: #172d88;
62+
box-shadow: none;
63+
color:#fff;
64+
}

web_admin/src/components/pages/product/Products.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,25 @@ import Sidebar from '../../Sidebar';
66
import Footer from '../../Footer';
77
import { useDispatch, useSelector } from "react-redux";
88
import { listProducts } from "../../../redux/actions/ProductActions";
9+
import ReactPaginate from 'react-paginate';
910
import './product.css';
1011

1112
const Products = () => {
1213
const dispatch = useDispatch();
1314

1415
const productList = useSelector((state) => state.productList);
15-
const { loading, error, products } = productList;
16+
const { loading, error, products, numOfPages } = productList;
17+
18+
let pageNum = 0;
19+
let productsPerPage = 1;
20+
const handlePageClick = (data) => {
21+
pageNum = data.selected;
22+
dispatch(listProducts(pageNum,productsPerPage));
23+
}
1624

1725
useEffect(() => {
18-
dispatch(listProducts());
19-
}, [dispatch]);
26+
dispatch(listProducts(pageNum,productsPerPage));
27+
}, [productsPerPage]);
2028

2129
return(
2230
<>
@@ -54,6 +62,31 @@ const Products = () => {
5462
</tbody>
5563
</table>
5664
</div>
65+
<div className="mt-4">
66+
{
67+
68+
<ReactPaginate
69+
previousLabel={"Previous"}
70+
nextLabel={"Next"}
71+
breakLabel={"..."}
72+
pageCount={numOfPages}
73+
marginPagesDisplayed={2}
74+
pageRangeDisplayed={3}
75+
onPageChange={handlePageClick}
76+
containerClassName={"pagination justify-content-center"}
77+
pageClassName={"page-item"}
78+
pageLinkClassName={"page-link"}
79+
previousClassName={"page-item"}
80+
previousLinkClassName={"page-link"}
81+
nextClassName={"page-item"}
82+
nextLinkClassName={"page-link"}
83+
breakClassName={"page-item"}
84+
breakLinkClassName={"page-link"}
85+
activeClassName={"active"}
86+
/>
87+
}
88+
89+
</div>
5790
</div>
5891
</div>
5992
</div>

web_admin/src/redux/actions/ProductActions.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ import { logout } from "./userActions";
2020
import { toast } from "react-toastify";
2121
import {ToastObjects} from "./toastObject";
2222

23-
export const listProducts = () => async (dispatch, getState) => {
23+
export const listProducts = (pageNum,productsPerPage) => async (dispatch, getState) => {
2424
try {
2525
dispatch({ type: PRODUCT_LIST_REQUEST });
2626

27-
const responseData = await axios.get(`/products`);
28-
const data = responseData.data.data
29-
27+
const responseData = await axios.get(`/products?page=${pageNum}&limit=${productsPerPage}`);
28+
const data = responseData.data;
3029
dispatch({ type: PRODUCT_LIST_SUCCESS, payload: data });
3130
} catch (error) {
3231
const message =

web_admin/src/redux/reducers/ProductReducers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import {
1919
} from "../constants/ProductConstants";
2020

2121
// All Products
22-
export const productListReducer = (state = { products: [] }, action) => {
22+
export const productListReducer = (state = { products: [], numOfPages:0 }, action) => {
2323
switch (action.type) {
2424
case PRODUCT_LIST_REQUEST:
2525
return { loading: true, products: [] };
2626
case PRODUCT_LIST_SUCCESS:
27-
return { loading: false, products: action.payload };
27+
return { loading: false, products: action.payload.data, numOfPages: action.payload.numOfPages };
2828
case PRODUCT_LIST_FAIL:
29-
return { loading: false, error: action.payload };
29+
return { loading: false, error: action.payload.data, numOfPages: action.payload.numOfPages };
3030
default:
3131
return state;
3232
}

web_services/routes/product.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@ router.put("/:id",verifyTokenAndAdmin,async (req,res)=>{
3838

3939
//Get All Products
4040
router.get("/", async (req,res)=>{
41-
4241
try{
43-
const productData = await Product.find();
4442

43+
const itemPerPage = parseInt(req.query.limit || "10"); //Products per page
44+
const pageNum = parseInt(req.query.page || "0"); //Products page number
45+
46+
const totalProducts = await Product.countDocuments({});
47+
const productData = await Product.find({}).limit(itemPerPage).skip(itemPerPage * pageNum);
48+
49+
let numOfPages = parseInt(totalProducts/itemPerPage);
50+
4551
if(productData){
46-
res.status(200).json({success:1,message:"",data:productData});
52+
res.status(200).json({success:1,message:"", numOfPages ,data:productData});
4753
}else{
4854
res.status(200).json({success:0,message:"No Data Found!"})
4955
}

0 commit comments

Comments
 (0)