-
-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
11 changed files
with
262 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 && ( | ||
<nav class="pagination-wrapper" aria-label="Pagination"> | ||
<LinkButton | ||
disabled={prev === "disabled"} | ||
href={prevUrl} | ||
className={`mr-4 select-none ${prev}`} | ||
ariaLabel="Previous" | ||
> | ||
<svg xmlns="http://www.w3.org/2000/svg" class={`${prev}-svg`}> | ||
<path d="M12.707 17.293 8.414 13H18v-2H8.414l4.293-4.293-1.414-1.414L4.586 12l6.707 6.707z" /> | ||
</svg> | ||
Prev | ||
</LinkButton> | ||
{currentPage} / {totalPages} | ||
<LinkButton | ||
disabled={next === "disabled"} | ||
href={nextUrl} | ||
className={`ml-4 select-none ${next}`} | ||
ariaLabel="Next" | ||
> | ||
Next | ||
<svg xmlns="http://www.w3.org/2000/svg" class={`${next}-svg`}> | ||
<path d="m11.293 17.293 1.414 1.414L19.414 12l-6.707-6.707-1.414 1.414L15.586 11H6v2h9.586z" /> | ||
</svg> | ||
</LinkButton> | ||
</nav> | ||
) | ||
} | ||
|
||
<style> | ||
.pagination-wrapper { | ||
@apply mb-8 mt-auto flex justify-center; | ||
} | ||
.disabled { | ||
@apply pointer-events-none select-none opacity-50 hover:text-skin-base group-hover:fill-skin-base; | ||
} | ||
.disabled-svg { | ||
@apply group-hover:!fill-skin-base; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,40 @@ | ||
--- | ||
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; | ||
--- | ||
|
||
<Layout title={`Posts | ${SITE.title}`}> | ||
<Header activeNav="posts" /> | ||
<Main pageTitle="Posts" pageDesc="All the articles I've posted."> | ||
<ul> | ||
{ | ||
posts.map(({ data, slug }) => ( | ||
paginatedPosts.map(({ data, slug }) => ( | ||
<Card href={`/posts/${slug}`} frontmatter={data} /> | ||
)) | ||
} | ||
</ul> | ||
</Main> | ||
|
||
{ | ||
totalPages > 1 && ( | ||
<nav class="pagination-wrapper" aria-label="Pagination"> | ||
<LinkButton | ||
disabled={prev === "disabled"} | ||
href={`/posts${pageNum - 1 !== 1 ? "/" + (pageNum - 1) : ""}`} | ||
className={`mr-4 select-none ${prev}`} | ||
ariaLabel="Previous" | ||
> | ||
<svg xmlns="http://www.w3.org/2000/svg" class={`${prev}-svg`}> | ||
<path d="M12.707 17.293 8.414 13H18v-2H8.414l4.293-4.293-1.414-1.414L4.586 12l6.707 6.707z" /> | ||
</svg> | ||
Prev | ||
</LinkButton> | ||
<LinkButton | ||
disabled={next === "disabled"} | ||
href={`/posts/${pageNum + 1}`} | ||
className={`ml-4 select-none ${next}`} | ||
ariaLabel="Next" | ||
> | ||
Next | ||
<svg xmlns="http://www.w3.org/2000/svg" class={`${next}-svg`}> | ||
<path d="m11.293 17.293 1.414 1.414L19.414 12l-6.707-6.707-1.414 1.414L15.586 11H6v2h9.586z" /> | ||
</svg> | ||
</LinkButton> | ||
</nav> | ||
) | ||
} | ||
<Pagination | ||
{currentPage} | ||
{totalPages} | ||
prevUrl={`/posts${currentPage - 1 !== 1 ? "/" + (currentPage - 1) : ""}`} | ||
nextUrl={`/posts/${currentPage + 1}`} | ||
/> | ||
|
||
<Footer noMarginTop={totalPages > 1} /> | ||
</Layout> | ||
|
||
<style> | ||
.pagination-wrapper { | ||
@apply mb-8 mt-auto flex justify-center; | ||
} | ||
.disabled { | ||
@apply pointer-events-none select-none opacity-50 hover:text-skin-base group-hover:fill-skin-base; | ||
} | ||
.disabled-svg { | ||
@apply group-hover:!fill-skin-base; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
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 Card from "@components/Card"; | ||
import Pagination from "@components/Pagination.astro"; | ||
import { SITE } from "@config"; | ||
export interface Props { | ||
currentPage: number; | ||
totalPages: number; | ||
paginatedPosts: CollectionEntry<"blog">[]; | ||
tag: string; | ||
tagName: string; | ||
} | ||
const { currentPage, totalPages, paginatedPosts, tag, tagName } = Astro.props; | ||
--- | ||
|
||
<Layout title={`Tag: ${tagName} | ${SITE.title}`}> | ||
<Header activeNav="tags" /> | ||
<Main | ||
pageTitle={[`Tag:`, `${tagName}`]} | ||
titleTransition={tag} | ||
pageDesc={`All the articles with the tag "${tagName}".`} | ||
> | ||
<h1 slot="title" transition:name={tag}>{`Tag:${tag}`}</h1> | ||
<ul> | ||
{ | ||
paginatedPosts.map(({ data, slug }) => ( | ||
<Card href={`/posts/${slug}`} frontmatter={data} /> | ||
)) | ||
} | ||
</ul> | ||
</Main> | ||
|
||
<Pagination | ||
{currentPage} | ||
{totalPages} | ||
prevUrl={`/tags/${tag}${ | ||
currentPage - 1 !== 1 ? "/" + (currentPage - 1) : "" | ||
}`} | ||
nextUrl={`/tags/${tag}/${currentPage + 1}`} | ||
/> | ||
|
||
<Footer noMarginTop={totalPages > 1} /> | ||
</Layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
--- | ||
import { SITE } from "@config"; | ||
import { getCollection } from "astro:content"; | ||
import Posts from "@layouts/Posts.astro"; | ||
import getSortedPosts from "@utils/getSortedPosts"; | ||
import getPageNumbers from "@utils/getPageNumbers"; | ||
import { getCollection } from "astro:content"; | ||
import getPagination from "@utils/getPagination"; | ||
const posts = await getCollection("blog"); | ||
const sortedPosts = getSortedPosts(posts); | ||
const totalPages = getPageNumbers(sortedPosts.length); | ||
const paginatedPosts = sortedPosts.slice(0, SITE.postPerPage); | ||
const pagination = getPagination({ | ||
posts: sortedPosts, | ||
page: 1, | ||
isIndex: true, | ||
}); | ||
--- | ||
|
||
<Posts posts={paginatedPosts} pageNum={1} totalPages={totalPages.length} /> | ||
<Posts {...pagination} /> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
import { type CollectionEntry, getCollection } from "astro:content"; | ||
import TagPosts from "@layouts/TagPosts.astro"; | ||
import getUniqueTags from "@utils/getUniqueTags"; | ||
import getPostsByTag from "@utils/getPostsByTag"; | ||
import getPageNumbers from "@utils/getPageNumbers"; | ||
import getPagination from "@utils/getPagination"; | ||
export interface Props { | ||
post: CollectionEntry<"blog">; | ||
tag: string; | ||
tagName: string; | ||
} | ||
export async function getStaticPaths() { | ||
const posts = await getCollection("blog"); | ||
const tags = getUniqueTags(posts); | ||
return tags.flatMap(({ tag, tagName }) => { | ||
const tagPosts = getPostsByTag(posts, tag); | ||
const totalPages = getPageNumbers(tagPosts.length); | ||
return totalPages.map(page => ({ | ||
params: { tag, page }, | ||
props: { tag, tagName }, | ||
})); | ||
}); | ||
} | ||
const { page } = Astro.params; | ||
const { tag, tagName } = Astro.props; | ||
const posts = await getCollection("blog", ({ data }) => !data.draft); | ||
const postsByTag = getPostsByTag(posts, tag); | ||
const pagination = getPagination({ | ||
posts: postsByTag, | ||
page, | ||
}); | ||
--- | ||
|
||
<TagPosts {...pagination} {tag} {tagName} /> |
Oops, something went wrong.