Files
vitareba/components/admin/DocumentAddForm.tsx
CatoandClaude Opus 5 614722b817 refactor(documents): one upload, whoever is uploading
A patient uploads their own lab results; a clinician uploads a report onto a
patient's record. Two forms, two skins — and two copies of the same upload: the
size pre-check, the FormData, the `data.success` handling, the "Uploaded ✓"
that fades after SAVED_FEEDBACK_MS, the reset of the file input, and a long
`accept=".pdf,.png,…"` literal typed out twice.

The pre-check is what made this worth doing. Refusing a file over
DOCUMENT_MAX_FILE_SIZE_MB *before* sending it is what stops a patient on a
phone pushing 40 MB up a mobile connection to be told no; a stale copy of that
rule would be invisible until someone's upload hung. The server enforces the
same limit — the client check is the courtesy, not the gate.

lib/hooks/useDocumentUpload.ts now owns all of it, and `patientId` is the whole
difference between the callers: the patient form omits it and the server files
the document against the signed-in user, which is why a patient cannot upload
onto someone else's record by editing a form. The portal's `router.refresh()`
rides along as `onUploaded`, since only its list is server-rendered.

DOCUMENT_ACCEPT moves to lib/config/portal.ts beside the other DOCUMENT_*
constants — it is configuration, not hook state.

Both forms keep their own markup, wording and placeholders; nothing rendered or
requested changes.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012KAn51JHFcTrPeCjZDGXM8
2026-09-15 11:52:00 +02:00

69 lines
2.0 KiB
TypeScript

"use client";
import styles from "@/app/(admin)/admin.module.css";
import {
DOCUMENT_ACCEPT,
DOCUMENT_MAX_FILE_SIZE_MB,
DOCUMENT_TITLE_MAX_LENGTH,
} from "@/lib/config/portal";
import { useDocumentUpload } from "@/lib/hooks/useDocumentUpload";
/**
* A clinician adding a document onto a patient's record. The upload itself is
* shared with the patient's own upload form — see lib/hooks/useDocumentUpload.ts;
* `patientId` is the whole difference.
*/
export function DocumentAddForm({ patientId }: { patientId: string }) {
const {
title,
setTitle,
progress,
errorMsg,
fileRef,
handleFileChange,
handleSubmit,
submitLabel,
submitDisabled,
} = useDocumentUpload({ patientId });
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={DOCUMENT_ACCEPT}
onChange={handleFileChange}
required
/>
</div>
</div>
{progress === "error" && <p className={styles.assignError}>{errorMsg}</p>}
<button type="submit" className={styles.assignSubmit} disabled={submitDisabled}>
{submitLabel}
</button>
</form>
);
}