Skip to content

Commit

Permalink
working on pagination dynmaic values change
Browse files Browse the repository at this point in the history
  • Loading branch information
php-team-mindspace committed Nov 6, 2024
1 parent 890c155 commit 51ce254
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
24 changes: 10 additions & 14 deletions src/webapp/src/components/Pagination.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,27 @@ import React, { useEffect, useState } from 'react';
import { Row, Col, Form, Button } from 'react-bootstrap';
import ReactPaginate from 'react-paginate';

const Pagination = (total) => {
function Pagination({ totalRec, itemsPerPage, currentPage, paginationData, onPageChange }) {

const [pageNo, setPageNo] = useState(1);
const [totalRec, setTotalrec] = useState(total.TotalCount);

const pagginationHandler = (page) => {
const currentPage = page.selected + 1;
setPageNo(currentPage);
};
const [pageCount, setPageCount] = useState(Math.ceil(totalRec / itemsPerPage));

useEffect(() => {
setTotalrec(total.TotalCount);
}, [total.TotalCount])
setPageCount(Math.ceil(totalRec / itemsPerPage)); // Recalculate on totalRec change
}, [totalRec, itemsPerPage]);


return (
<div className=''>
<Row>
<Col md={4}>
<ReactPaginate
// nextLabel={<FontAwesomeIcon icon="chevron-right" size="sm" className="m-auto" />}
onPageChange={pagginationHandler}
pageCount={pageCount}
onPageChange={onPageChange}
activeClassName="active"
pageRangeDisplayed={1}
marginPagesDisplayed={1}
pageCount={totalRec}
forcePage={1}
forcePage={currentPage - 1}
// previousLabel={<FontAwesomeIcon icon="chevron-left" size="sm" className="m-auto" />}
pageClassName="page-item"
pageLinkClassName="page-link"
Expand All @@ -38,7 +34,7 @@ const Pagination = (total) => {
breakClassName="page-item"
breakLinkClassName="page-link"
containerClassName="pagination justify-content-between d-flex "
activeClassName="active"
// activeClassName="active"
renderOnZeroPageCount={null}
/>
</Col>
Expand Down
32 changes: 26 additions & 6 deletions src/webapp/src/pages/bootcamps/BootcampsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@ const BootcampsPage = () => {
const [bootcamps, setBootcamps] = useState([]);
const [fetchError, setFetchError] = useState(null);
const [loading, setLoading] = useState(true);
const [Data, setData] = useState();
const [totalRec, setTotalRec] = useState();
const [itemsPerPage, setItemsPerPage] = useState([5]); // Default items per page
const [currentPage, setCurrentPage] = useState([1]);
const [pagination, setPagination] = useState([]);



// Load bootcamps from API
useEffect(() => {
const fetchBootcamps = async () => {
try {
const fields = ['photo', 'name', 'averageRating', 'location', 'careers', 'id'];
const res = await bootcampService.getBootcamps(fields);
var page = currentPage;
var limit = itemsPerPage;
console.log('page',page);
console.log('limit',limit);
const res = await bootcampService.getBootcamps(fields, page, limit);
console.log('res',res);
setBootcamps(res.data);
console.log('res',res.total)
setData(res.total)
setPagination(res.pagination);
setTotalRec(res.total)
setItemsPerPage(res.count)
setFetchError(null);
} catch (error) {
setFetchError(error.message);
Expand All @@ -28,8 +38,18 @@ const BootcampsPage = () => {
}
};
fetchBootcamps();
}, []);
}, [currentPage]);

const handlePaginationChange = (selectedPage) => {
console.log('selectedPage',selectedPage);
const newPage = selectedPage.selected + 1; // Convert index to actual page number
console.log('newPage',newPage);
setCurrentPage([newPage])
setItemsPerPage([5])
};


console.log('totalRec bootcamp',totalRec);

return (
<section className="browse my-5">
Expand All @@ -49,7 +69,7 @@ const BootcampsPage = () => {
))}
</div>
)}
<Pagination TotalCount={Data} />
<Pagination totalRec={totalRec} itemsPerPage={itemsPerPage} currentPage={currentPage} paginationData={pagination} onPageChange={handlePaginationChange} />
</div>
</section>
);
Expand Down
6 changes: 4 additions & 2 deletions src/webapp/src/services/bootcampService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { getAuthHeaders } from '../helpers/auth';
import { fetchApiEndPoint } from '../utils/configService';

const bootcampService = {
getBootcamps: async (fields) => {
getBootcamps: async (fields, page, limit) => {
const query = fields ? `?select=${fields.join(',')}` : '';
const uri = await fetchApiEndPoint(`/bootcamps${query}`);
const Page = page ? `page=${page.join(',')}` : '';
const Limit = limit ? `limit=${limit.join(',')}` : '';
const uri = await fetchApiEndPoint(`/bootcamps${query}&${Page}&${Limit}`);
const res = await fetch(uri);
if (!res.ok) {
console.log(`Failed to fetch bootcamps: ${res.status}`);
Expand Down

0 comments on commit 51ce254

Please sign in to comment.