added pagination to a given authors page

This commit is contained in:
2025-07-07 21:05:27 -04:00
parent a64b8fdceb
commit 6694f47d70
16 changed files with 350 additions and 93 deletions

View File

@@ -0,0 +1,89 @@
import * as hi from "jsr:@preact-icons/hi2";
export function PaginationControl({
paginatedData,
currentUrl,
authorId,
}: {
paginatedData: PaginatedPosts;
currentUrl: URL;
authorId: number;
}) {
const buildUrl = (page: number, limit?: number) => {
const params = new URLSearchParams(currentUrl.searchParams);
params.set("page", page.toString());
if (limit) params.set("limit", limit.toString());
return `${currentUrl.pathname}?${params.toString()}`;
};
if (paginatedData.totalPages <= 1) return null;
return (
<div class="mt-8 space-y-4">
{/* Pagination info and controls */}
<div class="flex flex-col sm:flex-row justify-center items-center gap-4">
<div class="flex items-center gap-2">
{paginatedData.hasPrevPage && (
<a
href={buildUrl(paginatedData.currentPage - 1)}
class="px-4 py-2 bg-[#45475a] text-[#cdd6f4] shadow-sm rounded hover:bg-[#6A6B7A] transition-colors"
>
<div class="flex items-center gap-2">
<hi.HiChevronDoubleLeft />
Previous
</div>
</a>
)}
{/* Page numbers */}
<div class="flex gap-1">
{Array.from(
{ length: Math.min(paginatedData.totalPages, 7) },
(_, i) => {
let pageNum;
if (paginatedData.totalPages <= 7) {
pageNum = i + 1;
} else {
const start = Math.max(1, paginatedData.currentPage - 3);
const end = Math.min(paginatedData.totalPages, start + 6);
pageNum = start + i;
if (pageNum > end) return null;
}
const isCurrentPage = pageNum === paginatedData.currentPage;
return (
<a
key={pageNum}
href={buildUrl(pageNum)}
class={`px-3 py-1 rounded text-sm shadow-sm ${
isCurrentPage
? "bg-[#6A6B7A] text-[#cdd6f4]"
: "bg-[#45475a] text-[#cdd6f4] hover:bg-[#6A6B7A]"
}`}
>
{pageNum}
</a>
);
},
)}
</div>
{paginatedData.hasNextPage && (
<a
href={buildUrl(paginatedData.currentPage + 1)}
class="px-4 py-2 bg-[#45475a] text-[#cdd6f4] shadow-sm rounded hover:bg-[#6A6B7A] transition-colors"
>
<div class="flex items-center gap-2">
Next
<hi.HiChevronDoubleRight />
</div>
</a>
)}
</div>
{/* Quick jump to page */}
</div>
</div>
);
}

View File

@@ -4,7 +4,7 @@ import { Post } from "../types/index.ts";
export const PostCard = function PostCard({ post }: { post: Post }) {
return (
<div class="p-6 bg-[#45475a] rounded-lg shadow-md transition-all duration-300 ease-in-out hover:shadow-xl hover:scale-105">
<div class="p-6 bg-[#45475a] rounded-lg shadow-xl transition-all duration-300 ease-in-out hover:shadow-xl hover:scale-105">
<a href={`${Deno.env.get("BASE_URI_WEB")}/posts/${post.post_id}`}>
<h2 class="text-white text-lg font-bold mb-2">{post.title}</h2>
<p class="text-white">
@@ -17,7 +17,7 @@ export const PostCard = function PostCard({ post }: { post: Post }) {
</a>{" "}
at {convertUtc(post.created_at)}
</p>
<p class="text-gray-400">{truncateString(post.body, 15)}</p>
<p class="text-gray-400">{truncateString(post.body, 30)}</p>
</a>
</div>
);

View File

@@ -1,3 +1,4 @@
export const truncateString = (str: string, maxLength: number) => {
str = str.replace(/<[^>]*>/g, "");
return str.length > maxLength ? `${str.slice(0, maxLength)}...` : str;
};

View File

@@ -2,13 +2,20 @@ import { FreshContext, Handlers, PageProps } from "$fresh/server.ts";
import AuthorCard from "../../components/AuthorCard.tsx";
import { Post } from "../../types/index.ts";
import { PostCarousel } from "../../components/PostCarousel.tsx";
import { PaginationControl } from "../../components/PaginationControl.tsx";
export const handler: Handlers<PageData> = {
async GET(_req: Request, ctx: FreshContext) {
async GET(req: Request, ctx: FreshContext) {
try {
const url = new URL(req.url);
const page = parseInt(url.searchParams.get("page") || "1");
const limit = parseInt(url.searchParams.get("limit") || "12");
const [authorResponse, authorPostResponse] = await Promise.all([
fetch(`${Deno.env.get("BASE_URI_API")}/authors/${ctx.params.id}`),
fetch(`${Deno.env.get("BASE_URI_API")}/authors/${ctx.params.id}/posts`),
fetch(
`${Deno.env.get("BASE_URI_API")}/authors/${ctx.params.id}/posts?page=${page}&limit=${limit}`,
),
]);
const [authorData, authorPostData] = await Promise.all([
@@ -16,9 +23,37 @@ export const handler: Handlers<PageData> = {
authorPostResponse.json(),
]);
let paginatedData: PaginatedPosts;
if (authorPostData.posts && authorPostData.total_posts !== undefined) {
const totalPages = Math.ceil(authorPostData.total_posts / limit);
paginatedData = {
posts: authorPostData.posts,
currentPage: page,
totalPages,
hasNextPage: page < totalPages,
hasPrevPage: page > 1,
totalPosts: authorPostData.total_posts,
};
} else {
const allPosts = Array.isArray(authorPostData) ? authorPostData : [];
const totalPages = Math.ceil(allPosts.length / limit);
const startIndex = (page - 1) * limit;
const endIndex = startIndex + limit;
paginatedData = {
posts: allPosts.slice(startIndex, endIndex),
currentPage: page,
totalPages,
hasNextPage: page < totalPages,
hasPrevPage: page > 1,
totalPosts: allPosts.length,
};
}
return ctx.render({
authorData,
authorPostData,
authorPostData: paginatedData,
});
} catch (error) {
return ctx.render({
@@ -30,7 +65,7 @@ export const handler: Handlers<PageData> = {
},
};
export default function AuthorIdentifier({ data }: PageProps<PageData>) {
export default function AuthorIdentifier({ data, url }: PageProps<PageData>) {
const { authorData, authorPostData, error } = data;
if (error) {
@@ -52,7 +87,12 @@ export default function AuthorIdentifier({ data }: PageProps<PageData>) {
<AuthorCard author={authorData} isIdentified={true} />
</div>
<div>
<PostCarousel posts={authorPostData} />
<PostCarousel posts={authorPostData.posts} />
<PaginationControl
paginatedData={authorPostData}
currentUrl={url}
authorId={authorData.author_id}
/>
</div>
</>
);