Files
vitareba/components/admin/DocumentAddForm.tsx
T
g-butandClaude Sonnet 4.6 8a1209ab32 fix(api): try-catch on bookings insert + SSOT/a11y polish
- Wrap db.insert in bookings POST with try-catch → 500 on failure (was
  unhandled bubble-up, last API route missing error handling)
- DocumentAddForm: add maxLength={DOCUMENT_TITLE_MAX_LENGTH} to title
  input; replace hardcoded 3000 timeout with SAVED_FEEDBACK_MS constant
- PatientGoalsCard: add aria-labels to Cancel (add form) and Save/Cancel
  (inline score edit row) buttons — each now names the goal being edited
- InlineMessageCompose: add aria-label to Cancel button

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

93 lines
3.0 KiB
TypeScript

"use client";
import { useRef, useState } from "react";
import styles from "@/app/(admin)/admin.module.css";
import { DOCUMENT_MAX_FILE_SIZE_MB, DOCUMENT_TITLE_MAX_LENGTH, SAVED_FEEDBACK_MS } from "@/lib/config/portal";
export function DocumentAddForm({ patientId }: { patientId: string }) {
const [title, setTitle] = useState("");
const [file, setFile] = useState<File | null>(null);
const [uploading, setUploading] = useState(false);
const [progress, setProgress] = useState<"idle" | "uploading" | "done" | "error">("idle");
const [errorMsg, setErrorMsg] = useState("");
const fileRef = useRef<HTMLInputElement>(null);
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
if (!file || !title.trim()) return;
setUploading(true);
setProgress("uploading");
setErrorMsg("");
try {
const formData = new FormData();
formData.set("file", file);
formData.set("title", title.trim());
formData.set("patientId", patientId);
const res = await fetch("/api/documents/upload", { method: "POST", body: formData });
const data = await res.json();
if (!data.success) {
setProgress("error");
setErrorMsg(data.error ?? "Upload failed.");
return;
}
setTitle("");
setFile(null);
if (fileRef.current) fileRef.current.value = "";
setProgress("done");
setTimeout(() => setProgress("idle"), SAVED_FEEDBACK_MS);
} catch {
setProgress("error");
setErrorMsg("Upload failed. Please try again.");
} finally {
setUploading(false);
}
}
return (
<form onSubmit={handleSubmit} className={styles.docForm}>
<p className={styles.docFormHeading}>Add document</p>
<div className={styles.formGrid2}>
<div className={styles.formField}>
<label className={styles.formLabel} htmlFor="doc-title">Title</label>
<input
id="doc-title"
className={styles.formInput}
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Lab results, Assessment report…"
maxLength={DOCUMENT_TITLE_MAX_LENGTH}
required
/>
</div>
<div className={styles.formField}>
<label className={styles.formLabel} htmlFor="doc-file">File (max {DOCUMENT_MAX_FILE_SIZE_MB} MB)</label>
<input
id="doc-file"
ref={fileRef}
className={styles.docFileInput}
type="file"
accept=".pdf,.png,.jpg,.jpeg,.webp,.doc,.docx,.xlsx,.csv"
onChange={(e) => setFile(e.target.files?.[0] ?? null)}
required
/>
</div>
</div>
{progress === "error" && (
<p className={styles.assignError}>{errorMsg}</p>
)}
<button
type="submit"
className={styles.assignSubmit}
disabled={uploading || !file || !title.trim()}
>
{progress === "uploading" ? "Uploading…" : progress === "done" ? "Uploaded ✓" : "Upload document"}
</button>
</form>
);
}