'use client'; import { useState, type FormEvent } from 'react'; import { useRouter } from 'next/navigation'; export function NoteForm({ projectId }: { projectId: number }) { const router = useRouter(); const [title, setTitle] = useState(''); const [bodyMd, setBodyMd] = useState(''); const [tags, setTags] = useState(''); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); async function onSubmit(event: FormEvent) { event.preventDefault(); setLoading(true); setError(null); const res = await fetch(`/api/projects/${projectId}/notes`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title, bodyMd, tags: tags || undefined }), }); setLoading(false); if (res.ok) { const data = (await res.json()) as { note: { id: number } }; router.push(`/projects/${projectId}/notes/${data.note.id}`); router.refresh(); } else { const b = (await res.json().catch(() => null)) as { error?: { message?: string }; } | null; setError(b?.error?.message ?? '作成に失敗しました'); } } return (
setTitle(e.target.value)} className="w-full rounded border px-3 py-2" required maxLength={200} />