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 = { 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(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) { 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 (
{error} { setDate(e.target.value); v.retouch("date", e.target.value); }} onBlur={(e) => v.touch("date", e.target.value)} /> {isSelf && ( )}