"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 { PROGRAMME_CONFIG, PHASE_CONFIG, PROGRAMME_ENUM_VALUES, PHASE_ENUM_VALUES, phaseDescription, } from "@/lib/config/programmes"; import type { ProgrammeKey, PhaseKey } from "@/lib/config/programmes"; type Assignment = { programme: ProgrammeKey; phase: PhaseKey; startDate: string | null; notes: string | null; }; export function ProgrammeAssignmentForm({ patientId, initial, clinician, }: { patientId: string; initial: Assignment | null; /** This patient's clinician, so the preview shows the copy they will read. */ clinician: string; }) { const [programme, setProgramme] = useState(initial?.programme ?? ""); const [phase, setPhase] = useState(initial?.phase ?? ""); const [startDate, setStartDate] = useState(initial?.startDate ?? ""); const [notes, setNotes] = useState(initial?.notes ?? ""); const [saving, setSaving] = useState(false); const [saved, setSaved] = useState(false); const [error, setError] = useState(""); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!programme || !phase) return; setSaving(true); setError(""); try { const res = await fetch(`/api/admin/patients/${patientId}/programme`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ programme, phase, startDate: startDate || null, notes: notes || null, }), }); const data = await res.json(); if (!data.success) { setError("Failed to save assignment."); return; } setSaved(true); setTimeout(() => setSaved(false), SAVED_FEEDBACK_MS); } catch { setError("Failed to save assignment."); } finally { setSaving(false); } } return (
setStartDate(e.target.value)} className={styles.assignField} />