"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { Button } from "./ui"; import { useTranslation } from "../../lib/i18n/context"; interface MarkBookReadButtonProps { bookId: string; currentStatus: string; } export function MarkBookReadButton({ bookId, currentStatus }: MarkBookReadButtonProps) { const { t } = useTranslation(); const [loading, setLoading] = useState(false); const router = useRouter(); const isRead = currentStatus === "read"; const targetStatus = isRead ? "unread" : "read"; const label = isRead ? t("markRead.markUnread") : t("markRead.markAsRead"); const handleClick = async () => { setLoading(true); try { const res = await fetch(`/api/books/${bookId}/progress`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ status: targetStatus }), }); if (!res.ok) { const body = await res.json().catch(() => ({ error: res.statusText })); console.error("Failed to update reading progress:", body.error); } router.refresh(); } catch (err) { console.error("Failed to update reading progress:", err); } finally { setLoading(false); } }; return ( ); }