More api fixes and page fixes

This commit is contained in:
Hieuhuy Pham 2025-01-13 22:27:32 -05:00
parent 261589b025
commit 89cff766bf
2 changed files with 8 additions and 5 deletions

View File

@ -1,5 +1,5 @@
import { fetchBookById } from "@/lib/api"; import { fetchBookById } from "@/lib/api";
import { Book } from "@/lib/types"; import { Book, Chapter } from "@/lib/types";
import { formatDateToMonthDayYear } from "@/lib/utils"; import { formatDateToMonthDayYear } from "@/lib/utils";
import ChapterDropdown from "@/components/ChapterDropdown"; import ChapterDropdown from "@/components/ChapterDropdown";
import { Ad } from "@/lib/types"; import { Ad } from "@/lib/types";
@ -26,8 +26,10 @@ export default async function BookPage(props: { params: paramsType }) {
} }
const { title, author, description, chapters, cover } = book; const { title, author, description, chapters, cover } = book;
const sorted_chapters:Chapter[] = chapters.sort((a, b) => a.number - b.number);
const cover_media = cover?.at(0); const cover_media = cover?.at(0);
const recentChapters = chapters.length > 6 ? chapters.slice(chapters.length - 6, chapters.length) : chapters; const recentChapters = sorted_chapters.length > 6 ? sorted_chapters.slice(sorted_chapters.length - 6, sorted_chapters.length) : sorted_chapters;
return ( return (
<div className="max-w-6xl mx-auto py-10 px-4"> <div className="max-w-6xl mx-auto py-10 px-4">
@ -74,9 +76,9 @@ export default async function BookPage(props: { params: paramsType }) {
</ul> </ul>
<div className="flex items-center justify-between mb-4 pt-4"> <div className="flex items-center justify-between mb-4 pt-4">
<h2 className="text-3xl font-semibold">All Chapters</h2> <h2 className="text-3xl font-semibold">All Chapters</h2>
<ChapterDropdown chapters={chapters} bookId={bookId} /> <ChapterDropdown chapters={sorted_chapters} bookId={bookId} />
</div> </div>
{chapters.map((chapter) => ( {sorted_chapters.map((chapter) => (
<li key={chapter.id} className="mb-2 list-none"> <li key={chapter.id} className="mb-2 list-none">
<a <a
href={`/books/${bookId}/chapters/${chapter.documentId}`} href={`/books/${bookId}/chapters/${chapter.documentId}`}

View File

@ -45,7 +45,7 @@ export async function fetchFromAPI<T>(
* Populates optional fields like Chapters or Editors based on requirements. * Populates optional fields like Chapters or Editors based on requirements.
*/ */
export async function fetchBooks(): Promise<Book[]> { export async function fetchBooks(): Promise<Book[]> {
const data = await fetchFromAPI<{ data: Book[] }>("/api/books?populate=*"); const data = await fetchFromAPI<{ data: Book[] }>("/api/books?populate=*&chapters.sort=number:desc");
return data.data; return data.data;
} }
@ -72,6 +72,7 @@ export async function fetchBookById(bookId: string): Promise<Book> {
const data = await fetchFromAPI<{ data: Book }>( const data = await fetchFromAPI<{ data: Book }>(
`/api/books/${bookId}?populate[chapters][filters][release_datetime][$lte]=${currentDateTime}&populate=cover` `/api/books/${bookId}?populate[chapters][filters][release_datetime][$lte]=${currentDateTime}&populate=cover`
); );
data.data.chapters = data.data.chapters.sort((a, b) => a.number - b.number);
return data.data; return data.data;
} }