Production
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import { type FormEvent, useState } from "react";
|
||||
import { Alert } from "../../components/ui/Alert";
|
||||
import { Button } from "../../components/ui/Button";
|
||||
import { Card, CardBody, CardHeader } from "../../components/ui/Card";
|
||||
import { Field, Input, Textarea } from "../../components/ui/Field";
|
||||
import { PageHeader } from "../../components/ui/PageHeader";
|
||||
import { api } from "../../api";
|
||||
import { errorMessage, todayISO } from "../../lib/format";
|
||||
import { navigate } from "../../lib/router";
|
||||
import { isoDate, maxLength, notAfter, required, useValidation } from "../../lib/validation";
|
||||
|
||||
export function NewReportPage() {
|
||||
const [date, setDate] = useState(todayISO());
|
||||
const [remarks, setRemarks] = useState("");
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const v = useValidation({
|
||||
date: [required("Report date"), isoDate("Report date"), notAfter(todayISO(), "Report date", "today")],
|
||||
remarks: [maxLength(2000, "Remarks")],
|
||||
});
|
||||
|
||||
async function submit(e: FormEvent<HTMLFormElement>) {
|
||||
e.preventDefault();
|
||||
if (!v.check({ date, remarks }, e.currentTarget)) return;
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const created = await api.createReport({ report_date: date, mr_remarks: remarks || null });
|
||||
navigate(`/reports/${created.id}`);
|
||||
} catch (err) {
|
||||
setError(errorMessage(err, "Could not create the report"));
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="narrow">
|
||||
<PageHeader back={{ to: "/reports", label: "Daily Call Reports" }} title="New Daily Call Report" description="Start a draft, then add your doctor and chemist visits before submitting." />
|
||||
<Card>
|
||||
<CardHeader title="Report details" />
|
||||
<CardBody>
|
||||
<form className="form-grid" onSubmit={submit} noValidate>
|
||||
<Alert>{error}</Alert>
|
||||
<Field label="Report date" required error={v.errors.date} hint="One report per day; you can file up to the configured backdating window.">
|
||||
<Input type="date" value={date} max={todayISO()} onChange={(e) => { setDate(e.target.value); v.retouch("date", e.target.value); }} onBlur={(e) => v.touch("date", e.target.value)} />
|
||||
</Field>
|
||||
<Field label="Daily remarks" span error={v.errors.remarks} hint={`${remarks.length}/2000`}>
|
||||
<Textarea value={remarks} onChange={(e) => { setRemarks(e.target.value); v.retouch("remarks", e.target.value); }} onBlur={(e) => v.touch("remarks", e.target.value)} rows={4} placeholder="Optional summary of the day" />
|
||||
</Field>
|
||||
<div className="form-actions">
|
||||
<Button onClick={() => navigate("/reports")}>Cancel</Button>
|
||||
<Button type="submit" variant="primary" loading={busy}>Create draft</Button>
|
||||
</div>
|
||||
</form>
|
||||
</CardBody>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user