Skip to content

Commit

Permalink
#24
Browse files Browse the repository at this point in the history
tried building a good pagination
  • Loading branch information
k-deepak04 committed Apr 22, 2023
1 parent 8d7b741 commit 695a06b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default class App extends Component {
<Route exact path="/blog" element={<Blog />} />
<Route exact path="/contact" element={<Contact />} />
<Route exact path="/portfolio" element={<Portfolio />} />
<Route exact path="/blog" element={<Blog />} />
<Route path="*" element={<NotFound />} />
</Routes>
<Footer />
Expand Down
59 changes: 50 additions & 9 deletions src/components/Blog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function Blog() {
const [likes, setLikes] = useState(0);
const [dislikes, setDislikes] = useState(0);


useEffect(() => {
// Fetch blogs from local JSON server
fetch('http://localhost:3000/blog')
Expand All @@ -24,17 +25,40 @@ export default function Blog() {
.catch(error => console.log(error));
}, []);

// Get current posts based on pagination
const indexOfLastPost = currentPage * postsPerPage;
const indexOfFirstPost = indexOfLastPost - postsPerPage;
const currentPosts = blogs.slice(indexOfFirstPost, indexOfLastPost);
//pagination
const itemsPerPage = 5;
const totalPages = 10;
const startIndex = (currentPage - 1) * itemsPerPage;
const endIndex = startIndex + itemsPerPage;
const currentBlogData = blogData.slice(startIndex, endIndex);
const maxPages = 5; // Maximum number of pages to display
const halfMaxPages = Math.floor(maxPages / 2);

// Change page
const handlePageChange = (event, value) => {
setCurrentPage(value);
navigate(`/blog/page/\${value}`);
};
let startPage = 1;
let endPage = totalPages;

if (totalPages > maxPages) {
if (currentPage <= halfMaxPages) {
endPage = maxPages;
} else if (currentPage >= totalPages - halfMaxPages) {
startPage = totalPages - maxPages + 1;
} else {
startPage = currentPage - halfMaxPages;
endPage = currentPage + halfMaxPages;
}
}
const paginationLinks = [];
for (let i = startPage; i <= endPage; i++) {
paginationLinks.push(
<button
key={i}
onClick={() => setCurrentPage(i)}
className={i === currentPage ? "active" : ""}
>
{i}
</button>
);
}
// Handle form submission for comments
const handleSubmit = event => {
event.preventDefault();
Expand Down Expand Up @@ -81,6 +105,23 @@ export default function Blog() {
<Box display="flex" justifyContent="center">
<Pagination count={Math.ceil(blogs.length / postsPerPage)} page={currentPage} onChange={handlePageChange} />
</Box>
<div className="pagination-links">
<button
className="prev"
disabled={currentPage === 1}
onClick={() => setCurrentPage(currentPage - 1)}
>
Prev
</button>
{paginationLinks}
<button
className="next"
disabled={currentPage === totalPages}
onClick={() => setCurrentPage(currentPage + 1)}
>
Next
</button>
</div>
</Box>
);
}

0 comments on commit 695a06b

Please sign in to comment.