2024-12-02 18:29:45 -05:00
|
|
|
import { FreshContext, Handlers, PageProps } from "$fresh/server.ts";
|
|
|
|
import AuthorCard from "../../components/AuthorCard.tsx";
|
2025-02-25 13:22:45 -05:00
|
|
|
import { Post } from "../../types/index.ts";
|
2024-12-02 18:29:45 -05:00
|
|
|
import { PostCarousel } from "../../components/PostCarousel.tsx";
|
2025-07-07 21:05:27 -04:00
|
|
|
import { PaginationControl } from "../../components/PaginationControl.tsx";
|
2024-12-02 18:29:45 -05:00
|
|
|
|
|
|
|
export const handler: Handlers<PageData> = {
|
2025-07-07 21:05:27 -04:00
|
|
|
async GET(req: Request, ctx: FreshContext) {
|
2024-12-02 18:29:45 -05:00
|
|
|
try {
|
2025-07-07 21:05:27 -04:00
|
|
|
const url = new URL(req.url);
|
|
|
|
const page = parseInt(url.searchParams.get("page") || "1");
|
|
|
|
const limit = parseInt(url.searchParams.get("limit") || "12");
|
|
|
|
|
2024-12-02 18:29:45 -05:00
|
|
|
const [authorResponse, authorPostResponse] = await Promise.all([
|
|
|
|
fetch(`${Deno.env.get("BASE_URI_API")}/authors/${ctx.params.id}`),
|
2025-07-07 21:05:27 -04:00
|
|
|
fetch(
|
|
|
|
`${Deno.env.get("BASE_URI_API")}/authors/${ctx.params.id}/posts?page=${page}&limit=${limit}`,
|
|
|
|
),
|
2024-12-02 18:29:45 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
const [authorData, authorPostData] = await Promise.all([
|
|
|
|
authorResponse.json(),
|
|
|
|
authorPostResponse.json(),
|
|
|
|
]);
|
|
|
|
|
2025-07-07 21:05:27 -04:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-12-02 18:29:45 -05:00
|
|
|
return ctx.render({
|
|
|
|
authorData,
|
2025-07-07 21:05:27 -04:00
|
|
|
authorPostData: paginatedData,
|
2024-12-02 18:29:45 -05:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
return ctx.render({
|
|
|
|
error: error.message,
|
|
|
|
authorData: null,
|
|
|
|
authorPostData: [],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2025-07-07 21:05:27 -04:00
|
|
|
export default function AuthorIdentifier({ data, url }: PageProps<PageData>) {
|
2024-12-02 18:29:45 -05:00
|
|
|
const { authorData, authorPostData, error } = data;
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<h1>Error Loading Author Information</h1>
|
|
|
|
<p>{error}</p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!authorData) {
|
|
|
|
return <div>No author found</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
<AuthorCard author={authorData} isIdentified={true} />
|
|
|
|
</div>
|
|
|
|
<div>
|
2025-07-07 21:05:27 -04:00
|
|
|
<PostCarousel posts={authorPostData.posts} />
|
|
|
|
<PaginationControl
|
|
|
|
paginatedData={authorPostData}
|
|
|
|
currentUrl={url}
|
|
|
|
authorId={authorData.author_id}
|
|
|
|
/>
|
2024-12-02 18:29:45 -05:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface PageData {
|
|
|
|
error?: string;
|
|
|
|
authorData: AuthorResponse;
|
|
|
|
authorPostData: Array<Post>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type AuthorResponse = {
|
|
|
|
author_id: number;
|
|
|
|
first_name: string;
|
|
|
|
last_name: string;
|
|
|
|
bio: string;
|
|
|
|
image?: string;
|
|
|
|
};
|