diff --git a/src/components/ReportButton.tsx b/src/components/ReportButton.tsx index 44631c9..89f0f1b 100644 --- a/src/components/ReportButton.tsx +++ b/src/components/ReportButton.tsx @@ -1,52 +1,25 @@ "use client" import React, { useState, useRef } from "react"; +import { createReport + } from "@/lib/api"; interface ReportButtonProps { bookId: string; chapterId: string; } -async function createReport( - error_type: string, - details: string, - book_id: string, - chapter_id: string, -) { - const payload = { - data:{ - error_type: error_type, - details: details, - book: book_id, - chapter: chapter_id - } - } - const response = await fetch( - '/api/reports', - { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(payload) - } - ) - - if(!response.ok) { - throw new Error(`API request failed with status ${response.status}`) - } - return response.json() -} const ReportButton: React.FC = ({ bookId, chapterId }) => { const modalRef = useRef(null) const [isOpen, setIsOpen] = useState(false) const [errorType, setErrorType] = useState('') const [details, setDetails] = useState('') - const handleSubmitReport = (event: React.FormEvent) => { + const handleSubmitReport = async (event: React.FormEvent) => { // Implement report submission here event.preventDefault() - createReport(errorType,details,bookId,chapterId) + const response = await createReport(errorType,details,bookId,chapterId) + response.status === 201 ? alert('Report submitted successfully') : alert('Failed to submit report') setErrorType('') - setDetails('') + setDetails('') setIsOpen(false) } const handleExit = (event: React.MouseEvent) => { diff --git a/src/lib/api.tsx b/src/lib/api.tsx index fccc12f..d32f819 100644 --- a/src/lib/api.tsx +++ b/src/lib/api.tsx @@ -25,7 +25,7 @@ export async function createFromAPI( const response = await fetch(`${API_URL}${endpoint}`, config) - if(!response.ok) { + if (!response.ok) { throw new Error(`API request failed with status ${response.status}`) } return response.json() @@ -162,20 +162,35 @@ export async function fetchGlossaryByBookId(bookId: string): Promise { const data = await fetchFromAPI(`/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 { + error_type: string, + details: string, + book_id: string, + chapter_id: string, +) { const payload = { data: { - error_type, - details, + error_type: error_type, + details: details, book: book_id, chapter: chapter_id } } - const data = await createFromAPI(`/api/reports`, JSON.stringify(payload)) - return data + + const response = await fetch( + '/api/reports', + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(payload) + } + ) + + if (!response.ok) { + throw new Error(`API request failed with status ${response.status}`) + } + return response } \ No newline at end of file