feat: add pagination in tag posts (#201)

* feat: add pagination in tag posts

Implement pagination in tag posts. Refactor and extract pagination logic and pagination component.

Closes: #152

* fix: update breadcrumbs for updated tag pagination
This commit is contained in:
Sat Naing
2023-12-29 10:26:02 +06:30
committed by GitHub
parent b05b8fb842
commit 581826a5af
11 changed files with 262 additions and 140 deletions
+6 -22
View File
@@ -4,7 +4,7 @@ import Posts from "@layouts/Posts.astro";
import PostDetails from "@layouts/PostDetails.astro";
import getSortedPosts from "@utils/getSortedPosts";
import getPageNumbers from "@utils/getPageNumbers";
import { SITE } from "@config";
import getPagination from "@utils/getPagination";
export interface Props {
post: CollectionEntry<"blog">;
@@ -32,26 +32,10 @@ const posts = await getCollection("blog");
const sortedPosts = getSortedPosts(posts);
const totalPages = getPageNumbers(sortedPosts.length);
const currentPage =
slug && !isNaN(Number(slug)) && totalPages.includes(Number(slug))
? Number(slug)
: 0;
const lastPost = currentPage * SITE.postPerPage;
const startPost = lastPost - SITE.postPerPage;
const paginatedPosts = sortedPosts.slice(startPost, lastPost);
const pagination = getPagination({
posts: sortedPosts,
page: slug,
});
---
{
post ? (
<PostDetails post={post} />
) : (
<Posts
posts={paginatedPosts}
pageNum={currentPage}
totalPages={totalPages.length}
/>
)
}
{post ? <PostDetails post={post} /> : <Posts {...pagination} />}