Skip to content

Commit a822963

Browse files
Merge pull request #1968 from RitoG09/fix/blog-pagination
feat: add pagination to blog page (10 posts per page)
2 parents 2dd1d24 + 7e2ccf5 commit a822963

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

pages/blog/index.page.tsx

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export default function StaticMarkdownPage({
102102
.split(',')
103103
.filter(isValidCategory);
104104
setCurrentFilterTags(tags.length ? tags : ['All']);
105+
setCurrentPage(1);
105106
}
106107
}, [router.query]);
107108

@@ -190,6 +191,23 @@ export default function StaticMarkdownPage({
190191
});
191192
const allTags = ['All', ...Array.from(allTagsSet)];
192193

194+
// pagination implement
195+
const POSTS_PER_PAGE = 10;
196+
const [currentPage, setCurrentPage] = useState(1);
197+
198+
const totalPages = Math.ceil(sortedFilteredPosts.length / POSTS_PER_PAGE);
199+
200+
useEffect(() => {
201+
if (currentPage > totalPages) {
202+
setCurrentPage(1);
203+
}
204+
}, [totalPages]);
205+
206+
const currentPagePosts = sortedFilteredPosts.slice(
207+
(currentPage - 1) * POSTS_PER_PAGE,
208+
currentPage * POSTS_PER_PAGE,
209+
);
210+
193211
return (
194212
// @ts-ignore
195213
<SectionContext.Provider value='blog'>
@@ -299,8 +317,8 @@ export default function StaticMarkdownPage({
299317
</div>
300318

301319
{/* Blog Posts Grid */}
302-
<div className='grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 xl:grid-cols-5 gap-6 grid-flow-row mb-20 bg-white dark:bg-slate-800 mx-auto p-4'>
303-
{sortedFilteredPosts.map((blogPost: any, idx: number) => {
320+
<div className='grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 xl:grid-cols-5 gap-6 grid-flow-row mb-16 bg-white dark:bg-slate-800 mx-auto p-4'>
321+
{currentPagePosts.map((blogPost: any, idx: number) => {
304322
const { frontmatter, content } = blogPost;
305323
const date = new Date(frontmatter.date);
306324
const postTimeToRead = Math.ceil(readingTime(content).minutes);
@@ -430,6 +448,34 @@ export default function StaticMarkdownPage({
430448
);
431449
})}
432450
</div>
451+
{/* pagination control */}
452+
<div className='flex justify-center items-center gap-4'>
453+
<button
454+
className={`px-4 py-2 rounded-md font-semibold ${
455+
currentPage === 1
456+
? 'bg-gray-300 dark:bg-slate-600 cursor-not-allowed'
457+
: 'bg-blue-600 text-white hover:bg-blue-700'
458+
}`}
459+
disabled={currentPage === 1}
460+
onClick={() => setCurrentPage((p) => p - 1)}
461+
>
462+
Previous
463+
</button>
464+
<span className='text-lg font-medium dark:text-white'>
465+
Page {currentPage} of {totalPages}
466+
</span>
467+
<button
468+
className={`px-4 py-2 rounded-md font-semibold ${
469+
currentPage === totalPages
470+
? 'bg-gray-300 dark:bg-slate-600 cursor-not-allowed'
471+
: 'bg-blue-600 text-white hover:bg-blue-700'
472+
}`}
473+
disabled={currentPage === totalPages}
474+
onClick={() => setCurrentPage((p) => p + 1)}
475+
>
476+
Next
477+
</button>
478+
</div>
433479
</div>
434480
</SectionContext.Provider>
435481
);

0 commit comments

Comments
 (0)