"use client"; import { useState } from "react"; import styles from "@/app/(admin)/admin.module.css"; import { SAVED_FEEDBACK_MS, PATIENT_NOTE_MAX_LENGTH } from "@/lib/config/portal"; import { formatDateShort } from "@/lib/utils/format"; import { TextBox } from "@/components/shared/TextBox"; type Note = { id: string; body: string; createdAt: string; adminName: string | null; }; export function AdminNotesForm({ patientId, initialNotes, }: { patientId: string; initialNotes: Note[]; }) { const [notes, setNotes] = useState(initialNotes); const [body, setBody] = useState(""); const [saving, setSaving] = useState(false); const [saved, setSaved] = useState(false); const [error, setError] = useState(""); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!body.trim()) return; setSaving(true); setError(""); try { const res = await fetch(`/api/admin/patients/${patientId}/notes`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ body: body.trim() }), }); const data = await res.json(); if (!data.success) { setError("Failed to save note."); return; } setNotes((prev) => [data.data, ...prev]); setBody(""); setSaved(true); setTimeout(() => setSaved(false), SAVED_FEEDBACK_MS); } catch { setError("Failed to save note."); } finally { setSaving(false); } } return (
{notes.length > 0 && (
{notes.map((n) => (
{n.adminName ?? "Admin"} · {formatDateShort(n.createdAt)}

{n.body}

))}
)}
setBody(v)} maxLength={PATIENT_NOTE_MAX_LENGTH} placeholder="Add a clinical note…" rows={3} /> {error &&

{error}

}
); }