Files
vitareba/components/admin/AdminNotesForm.tsx
T
g-butandClaude Sonnet 4.6 e79a329aff fix(admin): add maxLength to AdminNotesForm and ProgrammeAssignmentForm textareas
Both admin note fields lacked maxLength enforcement on the client despite
server Zod schemas using PATIENT_NOTE_MAX_LENGTH. Now consistent.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-23 03:18:56 +02:00

88 lines
2.5 KiB
TypeScript

"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";
type Note = {
id: string;
body: string;
createdAt: string;
adminName: string | null;
};
export function AdminNotesForm({
patientId,
initialNotes,
}: {
patientId: string;
initialNotes: Note[];
}) {
const [notes, setNotes] = useState<Note[]>(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 (
<div>
{notes.length > 0 && (
<div className={styles.notesList}>
{notes.map((n) => (
<div key={n.id} className={styles.noteItem}>
<div className={styles.noteMeta}>
{n.adminName ?? "Admin"} · {formatDateShort(n.createdAt)}
</div>
<p className={styles.noteBody}>{n.body}</p>
</div>
))}
</div>
)}
<form onSubmit={handleSubmit} className={styles.noteForm}>
<textarea
aria-label="Clinical note"
value={body}
onChange={(e) => setBody(e.target.value)}
maxLength={PATIENT_NOTE_MAX_LENGTH}
placeholder="Add a clinical note…"
rows={3}
className={styles.composeTextarea}
/>
{error && <p className={styles.assignError}>{error}</p>}
<button type="submit" disabled={saving || !body.trim()} className={styles.assignSubmit}>
{saving ? "Saving…" : saved ? "Saved ✓" : "Add note"}
</button>
</form>
</div>
);
}