Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added pagination to bolgs #3579

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion pages/blog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export default function BlogIndexPage() {
: []
);
const [isClient, setIsClient] = useState(false);
const [currentPage, setCurrentPage] = useState(1);
const [pageRange, setPageRange] = useState([1, 10]);
const postsPerPage = 12;

const onFilter = (data: IBlogPost[]) => setPosts(data);
const toFilter = [
Expand All @@ -59,6 +62,34 @@ export default function BlogIndexPage() {
const description = 'Find the latest and greatest stories from our community';
const image = '/img/social/blog.webp';

const handlePageChange = (pageNumber: number) => {
setCurrentPage(pageNumber);
if (pageNumber > pageRange[1] - 1) {
setPageRange([pageRange[0] + 3, pageRange[1] + 3]);
} else if (pageNumber < pageRange[0] + 1) {
const newStart = Math.max(pageRange[0] - 3, 1);
const newEnd = newStart + 9;

setPageRange([newStart, newEnd]);
}
};

const handlePrevious = () => {
if (currentPage > 1) {
handlePageChange(currentPage - 1);
}
};

const handleNext = () => {
if (currentPage < Math.ceil(posts.length / postsPerPage)) {
handlePageChange(currentPage + 1);
}
};

const indexOfLastPost = currentPage * postsPerPage;
const indexOfFirstPost = indexOfLastPost - postsPerPage;
const currentPosts = posts.slice(indexOfFirstPost, indexOfLastPost);

useEffect(() => {
setIsClient(true);
}, []);
Expand Down Expand Up @@ -122,7 +153,7 @@ export default function BlogIndexPage() {
)}
{Object.keys(posts).length > 0 && isClient && (
<ul className='mx-auto mt-12 grid max-w-lg gap-5 lg:max-w-none lg:grid-cols-3'>
{posts.map((post, index) => (
{currentPosts.map((post, index) => (
<BlogPostItem key={index} post={post} />
))}
</ul>
Expand All @@ -133,6 +164,42 @@ export default function BlogIndexPage() {
</div>
)}
</div>
<div className='mt-8 flex justify-center'>
<button
onClick={handlePrevious}
className={`mx-1 rounded-md border px-3 py-1 ${currentPage === 1 ? 'bg-gray-300' : 'bg-white'}`}
disabled={currentPage === 1}
>
Previous
</button>
{pageRange[0] > 1 && <span className='mx-1 px-3 py-1'>...</span>}
{Array.from(
{
length: Math.min(12, Math.ceil(posts.length / postsPerPage) - pageRange[0] + 1)
},
(_, index) => {
const pageNumber = pageRange[0] + index;

return (
<button
key={pageNumber}
onClick={() => handlePageChange(pageNumber)}
className={`mx-1 rounded-md border px-3 py-1 ${currentPage === pageNumber ? 'bg-gray-300' : 'bg-white'}`}
>
{pageNumber}
</button>
);
}
)}
{pageRange[1] < Math.ceil(posts.length / postsPerPage) && <span className='mx-1 px-3 py-1'>...</span>}
<button
onClick={handleNext}
className={`mx-1 rounded-md border px-3 py-1 ${currentPage === Math.ceil(posts.length / postsPerPage) ? 'bg-gray-300' : 'bg-white'}`}
disabled={currentPage === Math.ceil(posts.length / postsPerPage)}
>
Next
</button>
</div>
</div>
</div>
</GenericLayout>
Expand Down
Loading