diff --git a/README.md b/README.md index 733f2dd..bb81063 100644 --- a/README.md +++ b/README.md @@ -24,4 +24,14 @@ Maybe CMS is more useful to make websites for customers with no experience in CS Site will be polish more later when/if any of the novels has traffic. Things like shopping tiers and authentications seems like maybe something CMS would help with implementing. -We will see if then CMS is more useful later. \ No newline at end of file +We will see if then CMS is more useful later. + +**TODO** +ADD MORE NOVEL SUPPORT + +STANDARDIZE THE ERROR LOGS + +ADS LATER + +SCHEDULE POSTS FOR PATREON READAHEAD + diff --git a/src/app/books/[bookId]/chapters/[chapterId]/page.tsx b/src/app/books/[bookId]/chapters/[chapterId]/page.tsx index 896d19b..6dbaf62 100644 --- a/src/app/books/[bookId]/chapters/[chapterId]/page.tsx +++ b/src/app/books/[bookId]/chapters/[chapterId]/page.tsx @@ -1,6 +1,6 @@ import React from "react"; import NavigationButtons from "@/components/NavigationButtons"; -import { Chapter } from "@/lib/types"; +import { Book, Chapter } from "@/lib/types"; import { fetchBookById } from "@/lib/api"; import Head from "next/head"; export type paramsType = Promise<{ bookId: string; chapterId: string }>; @@ -9,16 +9,12 @@ export type paramsType = Promise<{ bookId: string; chapterId: string }>; export default async function ChapterPage(props: { params: paramsType}) { const { bookId, chapterId } = await props.params; - const book = await fetchBookById(bookId); - - const chapters :Chapter[] = book.chapters; - const sorted_chapters:Chapter[] = chapters.sort((a, b) => a.number - b.number); - const current_chapter = sorted_chapters.find((chapter) => chapter.documentId === chapterId) || undefined; - const next_chapter = current_chapter ? sorted_chapters.find((chapter) => chapter.number === current_chapter.number + 1)?.documentId || "" : ""; - const prev_chapter = current_chapter ? sorted_chapters.find((chapter) => chapter.number === current_chapter.number - 1)?.documentId || "" : ""; - // Fetch chapter data - - if (current_chapter === undefined) { + let book: Book; + try{ + book = await fetchBookById(bookId); + } + catch (error) { + console.error(error); return ( <> @@ -27,17 +23,37 @@ export default async function ChapterPage(props: { params: paramsType}) {

Chapter not found !

' }}>
- +
) } - - + + const chapters :Chapter[] = book.chapters; + const sorted_chapters:Chapter[] = chapters.sort((a, b) => a.number - b.number); + const current_chapter = sorted_chapters.find((chapter) => chapter.documentId === chapterId) || null; + const next_chapter = current_chapter ? sorted_chapters.find((chapter) => chapter.number === current_chapter.number + 1)?.documentId || "" : ""; + const prev_chapter = current_chapter ? sorted_chapters.find((chapter) => chapter.number === current_chapter.number - 1)?.documentId || "" : ""; + + if(current_chapter === null){ + return ( + <> + + Chapter not found + + +
+

Chapter not found !

' }}>
+ +
+ + ) + } + return ( <> - {book.title} - Chapter {current_chapter.number} + {book.title} - Chapter {current_chapter?.number}
@@ -47,6 +63,5 @@ export default async function ChapterPage(props: { params: paramsType}) {
- ); - + ); } \ No newline at end of file diff --git a/src/app/books/[bookId]/page.tsx b/src/app/books/[bookId]/page.tsx index badda35..cef1702 100644 --- a/src/app/books/[bookId]/page.tsx +++ b/src/app/books/[bookId]/page.tsx @@ -18,7 +18,7 @@ export default async function BookPage(props: { params: paramsType }) { console.error(error); return (
- Error fetching book data. Please try again later. + Book does not exists, are you lost?
); } diff --git a/src/lib/api.tsx b/src/lib/api.tsx index caa9dd5..a6c5436 100644 --- a/src/lib/api.tsx +++ b/src/lib/api.tsx @@ -69,9 +69,9 @@ export async function fetchBookChapterLinks(bookId: string): Promise { */ export async function fetchBookById(bookId: string): Promise { const currentDateTime = new Date().toISOString(); - const data = await fetchFromAPI<{ data: Book }>( - `/api/books/${bookId}?populate[chapters][filters][release_datetime][$lte]=${currentDateTime}&populate=cover` - ); + const data = await fetchFromAPI<{ data: Book }>( + `/api/books/${bookId}?populate[chapters][filters][release_datetime][$lte]=${currentDateTime}&populate=cover` + ); return data.data; }