104 lines
5.4 KiB
TypeScript
104 lines
5.4 KiB
TypeScript
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, Select, Textarea } from "../../components/ui/Field";
|
|
import { PageHeader } from "../../components/ui/PageHeader";
|
|
import { api } from "../../api";
|
|
import { useAsync } from "../../hooks/useAsync";
|
|
import { errorMessage, todayISO } from "../../lib/format";
|
|
import { REPORT_TYPE_LABEL } from "../../lib/format";
|
|
import { reportPath, reportsPath } from "../../lib/navigation";
|
|
import { navigate } from "../../lib/router";
|
|
import { isoDate, maxLength, notAfter, required, useValidation } from "../../lib/validation";
|
|
import type { ReportType } from "../../types";
|
|
|
|
const COPY: Record<ReportType, { title: string; description: string; remarks: string; create: string }> = {
|
|
SELF: {
|
|
title: "New Daily Call Report (Self)",
|
|
description: "Record the doctors and chemists you visited yourself. No approval is needed: once you submit it, the report is final.",
|
|
remarks: "Optional summary of the day",
|
|
create: "Start report",
|
|
},
|
|
PLAN: {
|
|
title: "New Daily Call Report (Plan)",
|
|
description: "List the doctors and chemists you intend to visit today and when, then send the plan to your Area Manager.",
|
|
remarks: "Optional note for your Area Manager, e.g. the area you will cover",
|
|
create: "Start plan",
|
|
},
|
|
ACTUAL: {
|
|
title: "New Daily Call Report (Done)",
|
|
description: "Once your plan is approved, record the visits you made. Planned visits you could not make need a short note.",
|
|
remarks: "Optional summary of the day",
|
|
create: "Start report",
|
|
},
|
|
};
|
|
|
|
/** A plan may be made this many days ahead (PLAN_AHEAD_DAYS on the server); Done and Self never. */
|
|
const PLAN_AHEAD_DAYS = 2;
|
|
|
|
export function NewReportPage({ type }: { type: ReportType }) {
|
|
const copy = COPY[type];
|
|
const latest = todayISO(type === "PLAN" ? PLAN_AHEAD_DAYS : 0);
|
|
const isSelf = type === "SELF";
|
|
// An Area Manager's own report is for one of the headquarters they manage.
|
|
const hqs = useAsync(() => (isSelf ? api.headquarters() : Promise.resolve([])), [isSelf]);
|
|
const [hqChoice, setHqChoice] = useState("");
|
|
const hqId = hqChoice || (hqs.data?.length === 1 ? hqs.data[0].id : "");
|
|
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(latest, "Report date", type === "PLAN" ? `${PLAN_AHEAD_DAYS} days from today` : "today")],
|
|
hq: isSelf ? [required("Headquarters")] : [],
|
|
remarks: [maxLength(2000, "Remarks")],
|
|
});
|
|
|
|
async function submit(e: FormEvent<HTMLFormElement>) {
|
|
e.preventDefault();
|
|
if (!v.check({ date, hq: hqId, remarks }, e.currentTarget)) return;
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
const created = await api.createReport({ report_date: date, report_type: type, mr_remarks: remarks || null, ...(isSelf && { headquarters_id: hqId }) });
|
|
navigate(reportPath(created));
|
|
} catch (err) {
|
|
setError(errorMessage(err, "Could not create the report"));
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="narrow">
|
|
<PageHeader back={{ to: reportsPath(type), label: REPORT_TYPE_LABEL[type] }} title={copy.title} description={copy.description} />
|
|
<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={type === "PLAN" ? `One plan per day. You can plan today or up to ${PLAN_AHEAD_DAYS} days ahead.` : isSelf ? "One report per day." : "Your Daily Call Report (Plan) for this date must already be approved by your Area Manager."}>
|
|
<Input type="date" value={date} max={latest} onChange={(e) => { setDate(e.target.value); v.retouch("date", e.target.value); }} onBlur={(e) => v.touch("date", e.target.value)} />
|
|
</Field>
|
|
{isSelf && (
|
|
<Field label="Headquarters" required error={v.errors.hq ?? hqs.error ?? undefined} hint={hqs.data?.length === 0 ? "You do not manage a headquarters yet — ask your administrator." : "Its doctors and chemists can be added to the report."}>
|
|
<Select value={hqId} onChange={(e) => { setHqChoice(e.target.value); v.retouch("hq", e.target.value); }}>
|
|
<option value="">Choose headquarters</option>
|
|
{(hqs.data ?? []).map((h) => <option key={h.id} value={h.id}>{h.code} · {h.name}</option>)}
|
|
</Select>
|
|
</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={copy.remarks} />
|
|
</Field>
|
|
<div className="form-actions">
|
|
<Button onClick={() => navigate(reportsPath(type))}>Cancel</Button>
|
|
<Button type="submit" variant="primary" loading={busy}>{copy.create}</Button>
|
|
</div>
|
|
</form>
|
|
</CardBody>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}
|