From 97e318ce6f700b30c2a8c4beefced63d92fcb5ed Mon Sep 17 00:00:00 2001 From: Hieuhuy Pham Date: Thu, 23 Jan 2025 01:44:12 -0500 Subject: [PATCH] The mystery of the glossary addition. I really gotta figure out why it refuses to populate glossary and cover with chapters at the same time. --- src/lib/api.tsx | 61 +++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/src/lib/api.tsx b/src/lib/api.tsx index 5d301e1..9cae2a2 100644 --- a/src/lib/api.tsx +++ b/src/lib/api.tsx @@ -1,5 +1,5 @@ import { addDays, subDays } from "date-fns"; -import { Book, Chapter, Editor, Announcement } from "./types"; +import { Book, Chapter, Editor, Announcement, Glossary } from "./types"; const API_URL = process.env.NEXT_PUBLIC_API_URL as string; const API_TOKEN = process.env.STRAPI_API_TOKEN as string; @@ -19,26 +19,26 @@ export async function fetchFromAPI( ...options, }; - + let results: T[] = []; let currentPage = 1; let totalPages = 1; try { - do{ + do { const url = `${API_URL}${endpoint}&pagination[page]=${currentPage}&pagination[pageSize]=25`; - const response = await fetch(url, {...config, next: {revalidate:30}}); - if (!response.ok) { - const errorData = await response.json(); - console.error(`Error fetching ${url}:`, errorData); - throw new Error(errorData.message || `API fetch error (status: ${response.status})`); - } - const responseJson = await response.json(); - results = results.concat(responseJson.data); - totalPages = responseJson.meta?.pagination?.pageCount; - currentPage += 1; - }while(currentPage <= totalPages) - + const response = await fetch(url, { ...config, next: { revalidate: 30 } }); + if (!response.ok) { + const errorData = await response.json(); + console.error(`Error fetching ${url}:`, errorData); + throw new Error(errorData.message || `API fetch error (status: ${response.status})`); + } + const responseJson = await response.json(); + results = results.concat(responseJson.data); + totalPages = responseJson.meta?.pagination?.pageCount; + currentPage += 1; + } while (currentPage <= totalPages) + } catch (error) { console.error("Fetch error:", error); throw error; @@ -81,9 +81,13 @@ export async function fetchBookChapterLinks(bookId: string): Promise { */ export async function fetchBookById(bookId: string): Promise { const currentDateTime = new Date().toISOString(); - const data = await fetchFromAPI( - `/api/books/${bookId}?populate[chapters][filters][release_datetime][$lte]=${currentDateTime}&populate=cover` - ); + //[chapters][filters][release_datetime][$lte]=${currentDateTime} + const data = await fetchFromAPI( + `/api/books/${bookId}?populate[chapters][filters][release_datetime][$lte]=${currentDateTime}&populate=cover` + ); + //I do not know why the hell it refuse to populate glossary only 1 field is allow to be populated after ???????? + const glossary = await fetchGlossaryByBookId(bookId); + data[0].glossary = glossary; data[0].chapters = data[0].chapters.sort((a, b) => a.number - b.number); return data[0]; } @@ -93,11 +97,9 @@ export async function fetchBookById(bookId: string): Promise { */ export async function fetchChapterByBookId(bookId: string, chapterId: string): Promise { const currentChapter = await fetchFromAPI(`/api/chapters/${chapterId}?populate[book][fields][0]=title&filters[book][documentId]=${bookId}`); - const bookWithAllChapters = await fetchFromAPI( `/api/books/${bookId}?populate[chapters][filters][number][$gte]=${ - currentChapter[0].number - 1 - }&populate[chapters][filters][number][$lte]=${ - currentChapter[0].number + 1 - }`); + const bookWithAllChapters = await fetchFromAPI(`/api/books/${bookId}?populate[chapters][filters][number][$gte]=${currentChapter[0].number - 1 + }&populate[chapters][filters][number][$lte]=${currentChapter[0].number + 1 + }`); //const nextChapter = await fetchFromAPI(`/api/chapters?populate[book]&filters[book][id]=${bookId}&sort[number]=asc`); return bookWithAllChapters[0].chapters; } @@ -110,20 +112,25 @@ export async function fetchEditors(): Promise { return data; } -export type ChapterRelease = {current_chapters:Chapter[],future_chapters:Chapter[]} -export async function fetchReleases(): Promise<{current_chapters:Chapter[],future_chapters:Chapter[]}> { +export type ChapterRelease = { current_chapters: Chapter[], future_chapters: Chapter[] } +export async function fetchReleases(): Promise<{ current_chapters: Chapter[], future_chapters: Chapter[] }> { const current_datetime = new Date() const previous_week = subDays(current_datetime, 3); const next_week = addDays(current_datetime, 3); - + const data = await fetchFromAPI(`/api/chapters/?populate[book][fields][0]=title&fields[0]=number&fields[1]=title&fields[2]=release_datetime&filters[release_datetime][$gte]=${previous_week.toISOString()}&filters[release_datetime][$lte]=${next_week.toISOString()}`); const chapters: Chapter[] = data; const future_chapters = chapters.filter(chapter => new Date(chapter.release_datetime) > new Date()); const current_chapters = chapters.filter(chapter => new Date(chapter.release_datetime) <= new Date()); - return {current_chapters,future_chapters} + return { current_chapters, future_chapters } } export async function fetchAnnouncementById(announcementId: string): Promise { const data = await fetchFromAPI(`/api/announcements/${announcementId}?`); return data[0]; +} + +export async function fetchGlossaryByBookId(bookId: string): Promise { + const data = await fetchFromAPI(`/api/glossaries?filters[book][documentId]=${bookId}`); + return data[0]; } \ No newline at end of file