Report button support, all that jazz.

This commit is contained in:
2025-01-24 12:54:05 -05:00
parent 9c78ed022d
commit ad66be5039
4 changed files with 215 additions and 2 deletions

View File

@@ -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
}