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.
This commit is contained in:
parent
554a6dff45
commit
97e318ce6f
@ -1,5 +1,5 @@
|
|||||||
import { addDays, subDays } from "date-fns";
|
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_URL = process.env.NEXT_PUBLIC_API_URL as string;
|
||||||
const API_TOKEN = process.env.STRAPI_API_TOKEN as string;
|
const API_TOKEN = process.env.STRAPI_API_TOKEN as string;
|
||||||
@ -25,9 +25,9 @@ export async function fetchFromAPI<T>(
|
|||||||
let currentPage = 1;
|
let currentPage = 1;
|
||||||
let totalPages = 1;
|
let totalPages = 1;
|
||||||
try {
|
try {
|
||||||
do{
|
do {
|
||||||
const url = `${API_URL}${endpoint}&pagination[page]=${currentPage}&pagination[pageSize]=25`;
|
const url = `${API_URL}${endpoint}&pagination[page]=${currentPage}&pagination[pageSize]=25`;
|
||||||
const response = await fetch(url, {...config, next: {revalidate:30}});
|
const response = await fetch(url, { ...config, next: { revalidate: 30 } });
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorData = await response.json();
|
const errorData = await response.json();
|
||||||
console.error(`Error fetching ${url}:`, errorData);
|
console.error(`Error fetching ${url}:`, errorData);
|
||||||
@ -37,7 +37,7 @@ export async function fetchFromAPI<T>(
|
|||||||
results = results.concat(responseJson.data);
|
results = results.concat(responseJson.data);
|
||||||
totalPages = responseJson.meta?.pagination?.pageCount;
|
totalPages = responseJson.meta?.pagination?.pageCount;
|
||||||
currentPage += 1;
|
currentPage += 1;
|
||||||
}while(currentPage <= totalPages)
|
} while (currentPage <= totalPages)
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Fetch error:", error);
|
console.error("Fetch error:", error);
|
||||||
@ -81,9 +81,13 @@ export async function fetchBookChapterLinks(bookId: string): Promise<Book> {
|
|||||||
*/
|
*/
|
||||||
export async function fetchBookById(bookId: string): Promise<Book> {
|
export async function fetchBookById(bookId: string): Promise<Book> {
|
||||||
const currentDateTime = new Date().toISOString();
|
const currentDateTime = new Date().toISOString();
|
||||||
|
//[chapters][filters][release_datetime][$lte]=${currentDateTime}
|
||||||
const data = await fetchFromAPI<Book>(
|
const data = await fetchFromAPI<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`
|
||||||
);
|
);
|
||||||
|
//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);
|
data[0].chapters = data[0].chapters.sort((a, b) => a.number - b.number);
|
||||||
return data[0];
|
return data[0];
|
||||||
}
|
}
|
||||||
@ -93,10 +97,8 @@ export async function fetchBookById(bookId: string): Promise<Book> {
|
|||||||
*/
|
*/
|
||||||
export async function fetchChapterByBookId(bookId: string, chapterId: string): Promise<Chapter[]> {
|
export async function fetchChapterByBookId(bookId: string, chapterId: string): Promise<Chapter[]> {
|
||||||
const currentChapter = await fetchFromAPI<Chapter>(`/api/chapters/${chapterId}?populate[book][fields][0]=title&filters[book][documentId]=${bookId}`);
|
const currentChapter = await fetchFromAPI<Chapter>(`/api/chapters/${chapterId}?populate[book][fields][0]=title&filters[book][documentId]=${bookId}`);
|
||||||
const bookWithAllChapters = await fetchFromAPI<Book>( `/api/books/${bookId}?populate[chapters][filters][number][$gte]=${
|
const bookWithAllChapters = await fetchFromAPI<Book>(`/api/books/${bookId}?populate[chapters][filters][number][$gte]=${currentChapter[0].number - 1
|
||||||
currentChapter[0].number - 1
|
}&populate[chapters][filters][number][$lte]=${currentChapter[0].number + 1
|
||||||
}&populate[chapters][filters][number][$lte]=${
|
|
||||||
currentChapter[0].number + 1
|
|
||||||
}`);
|
}`);
|
||||||
//const nextChapter = await fetchFromAPI<Chapter>(`/api/chapters?populate[book]&filters[book][id]=${bookId}&sort[number]=asc`);
|
//const nextChapter = await fetchFromAPI<Chapter>(`/api/chapters?populate[book]&filters[book][id]=${bookId}&sort[number]=asc`);
|
||||||
return bookWithAllChapters[0].chapters;
|
return bookWithAllChapters[0].chapters;
|
||||||
@ -110,8 +112,8 @@ export async function fetchEditors(): Promise<Editor[]> {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ChapterRelease = {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[]}> {
|
export async function fetchReleases(): Promise<{ current_chapters: Chapter[], future_chapters: Chapter[] }> {
|
||||||
const current_datetime = new Date()
|
const current_datetime = new Date()
|
||||||
const previous_week = subDays(current_datetime, 3);
|
const previous_week = subDays(current_datetime, 3);
|
||||||
const next_week = addDays(current_datetime, 3);
|
const next_week = addDays(current_datetime, 3);
|
||||||
@ -120,10 +122,15 @@ export async function fetchReleases(): Promise<{current_chapters:Chapter[],futur
|
|||||||
const chapters: Chapter[] = data;
|
const chapters: Chapter[] = data;
|
||||||
const future_chapters = chapters.filter(chapter => new Date(chapter.release_datetime) > new Date());
|
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());
|
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<Announcement> {
|
export async function fetchAnnouncementById(announcementId: string): Promise<Announcement> {
|
||||||
const data = await fetchFromAPI<Announcement>(`/api/announcements/${announcementId}?`);
|
const data = await fetchFromAPI<Announcement>(`/api/announcements/${announcementId}?`);
|
||||||
return data[0];
|
return data[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function fetchGlossaryByBookId(bookId: string): Promise<Glossary> {
|
||||||
|
const data = await fetchFromAPI<Glossary>(`/api/glossaries?filters[book][documentId]=${bookId}`);
|
||||||
|
return data[0];
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user