"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; export default function AddQuestionButton({ annonceId }: { annonceId: number }) { const router = useRouter(); const [open, setOpen] = useState(false); const [content, setContent] = useState(""); const [loading, setLoading] = useState(false); const [msg, setMsg] = useState(null); const submit = async () => { if (!content.trim()) { setMsg("Écris ta question avant d’envoyer."); return; } setLoading(true); setMsg(null); const res = await fetch("/api/question", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ annonceId, content }), }); const data = await res.json().catch(() => ({})); setLoading(false); if (!res.ok) { setMsg(data?.error || "Impossible de créer la question."); return; } setContent(""); setOpen(false); router.refresh(); }; return (
{!open ? ( ) : (