Report button support, all that jazz.
This commit is contained in:
@@ -1,9 +1,36 @@
|
||||
import { addDays, subDays } from "date-fns";
|
||||
import { Book, Chapter, Editor, Announcement, Glossary } from "./types";
|
||||
import { Book, Chapter, Editor, Announcement, Glossary, Report } from "./types";
|
||||
|
||||
const API_URL = process.env.NEXT_PUBLIC_API_URL as string;
|
||||
const API_TOKEN = process.env.STRAPI_API_TOKEN as string;
|
||||
|
||||
export async function createFromAPI<T>(
|
||||
endpoint: string,
|
||||
payload: string,
|
||||
options: RequestInit = {},
|
||||
): Promise<T> {
|
||||
|
||||
const headers: HeadersInit = {
|
||||
Authorization: `Bearer ${API_TOKEN}`,
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
|
||||
const config: RequestInit = {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: payload,
|
||||
...options,
|
||||
}
|
||||
|
||||
|
||||
const response = await fetch(`${API_URL}${endpoint}`, config)
|
||||
if(!response.ok) {
|
||||
throw new Error(`API request failed with status ${response.status}`)
|
||||
}
|
||||
return response.json()
|
||||
}
|
||||
|
||||
export async function fetchFromAPI<T>(
|
||||
endpoint: string,
|
||||
options: RequestInit = {}
|
||||
@@ -13,8 +40,9 @@ export async function fetchFromAPI<T>(
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
|
||||
const config: RequestInit = {
|
||||
method: "GET", // Default method is GET
|
||||
method: "GET",
|
||||
headers,
|
||||
...options,
|
||||
};
|
||||
@@ -133,4 +161,21 @@ export async function fetchAnnouncementById(announcementId: string): Promise<Ann
|
||||
export async function fetchGlossaryByBookId(bookId: string): Promise<Glossary> {
|
||||
const data = await fetchFromAPI<Glossary>(`/api/glossaries?filters[book][documentId]=${bookId}`);
|
||||
return data[0];
|
||||
}
|
||||
export async function createReport(
|
||||
error_type: string,
|
||||
details: string,
|
||||
book_id: string,
|
||||
chapter_id: string
|
||||
): Promise<Report> {
|
||||
const payload = {
|
||||
data: {
|
||||
error_type,
|
||||
details,
|
||||
book: book_id,
|
||||
chapter: chapter_id
|
||||
}
|
||||
}
|
||||
const data = await createFromAPI<Report>(`/api/reports`, JSON.stringify(payload))
|
||||
return data
|
||||
}
|
||||
Reference in New Issue
Block a user