From 8243ea0b5217e2350898aba07ab4be6a765c3647 Mon Sep 17 00:00:00 2001 From: satnaing Date: Thu, 28 Dec 2023 21:05:44 +0630 Subject: [PATCH 1/2] feat: add pagination in tag posts Implement pagination in tag posts. Refactor and extract pagination logic and pagination component. Closes: #152 --- src/components/Pagination.astro | 57 ++++++++++++++++++++++++++ src/layouts/Posts.astro | 64 +++++++----------------------- src/layouts/TagPosts.astro | 49 +++++++++++++++++++++++ src/pages/posts/[slug]/index.astro | 28 +++---------- src/pages/posts/index.astro | 16 ++++---- src/pages/tags/[tag].astro | 58 --------------------------- src/pages/tags/[tag]/[page].astro | 44 ++++++++++++++++++++ src/pages/tags/[tag]/index.astro | 32 +++++++++++++++ src/utils/getPagination.ts | 35 ++++++++++++++++ src/utils/getPostsByTag.ts | 7 +++- 10 files changed, 250 insertions(+), 140 deletions(-) create mode 100644 src/components/Pagination.astro create mode 100644 src/layouts/TagPosts.astro delete mode 100644 src/pages/tags/[tag].astro create mode 100644 src/pages/tags/[tag]/[page].astro create mode 100644 src/pages/tags/[tag]/index.astro create mode 100644 src/utils/getPagination.ts diff --git a/src/components/Pagination.astro b/src/components/Pagination.astro new file mode 100644 index 000000000..fb64089e0 --- /dev/null +++ b/src/components/Pagination.astro @@ -0,0 +1,57 @@ +--- +import LinkButton from "./LinkButton.astro"; + +export interface Props { + currentPage: number; + totalPages: number; + prevUrl: string; + nextUrl: string; +} + +const { currentPage, totalPages, prevUrl, nextUrl } = Astro.props; + +const prev = currentPage > 1 ? "" : "disabled"; +const next = currentPage < totalPages ? "" : "disabled"; +--- + +{ + totalPages > 1 && ( + + ) +} + + diff --git a/src/layouts/Posts.astro b/src/layouts/Posts.astro index c856a019d..5d5119958 100644 --- a/src/layouts/Posts.astro +++ b/src/layouts/Posts.astro @@ -1,23 +1,20 @@ --- -import { SITE } from "@config"; +import type { CollectionEntry } from "astro:content"; import Layout from "@layouts/Layout.astro"; import Main from "@layouts/Main.astro"; import Header from "@components/Header.astro"; import Footer from "@components/Footer.astro"; +import Pagination from "@components/Pagination.astro"; import Card from "@components/Card"; -import LinkButton from "@components/LinkButton.astro"; -import type { CollectionEntry } from "astro:content"; +import { SITE } from "@config"; export interface Props { - pageNum: number; + currentPage: number; totalPages: number; - posts: CollectionEntry<"blog">[]; + paginatedPosts: CollectionEntry<"blog">[]; } -const { pageNum, totalPages, posts } = Astro.props; - -const prev = pageNum > 1 ? "" : "disabled"; -const next = pageNum < totalPages ? "" : "disabled"; +const { currentPage, totalPages, paginatedPosts } = Astro.props; --- @@ -25,52 +22,19 @@ const next = pageNum < totalPages ? "" : "disabled";
    { - posts.map(({ data, slug }) => ( + paginatedPosts.map(({ data, slug }) => ( )) }
- { - totalPages > 1 && ( - - ) - } + +