Production
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
import { type FormEvent, useState } from "react";
|
||||
import { Alert } from "../ui/Alert";
|
||||
import { Button } from "../ui/Button";
|
||||
import { Field, Input, Select, Textarea } from "../ui/Field";
|
||||
import { useToast } from "../ui/Toast";
|
||||
import { api } from "../../api";
|
||||
import { useAsync } from "../../hooks/useAsync";
|
||||
import { errorMessage, nowHHMM, todayISO } from "../../lib/format";
|
||||
import { amount, maxLength, required, useValidation } from "../../lib/validation";
|
||||
import type { Chemist, Dcr, Doctor } from "../../types";
|
||||
|
||||
export function AddVisitForm({ dcr, doctors, chemists, onChange }: {
|
||||
dcr: Dcr; doctors: Doctor[]; chemists: Chemist[]; onChange: (dcr: Dcr) => void;
|
||||
}) {
|
||||
const toast = useToast();
|
||||
const products = useAsync(api.stockInventory, []);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [kind, setKind] = useState<"doctor" | "chemist">("doctor");
|
||||
const [targetId, setTargetId] = useState("");
|
||||
const [time, setTime] = useState(nowHHMM);
|
||||
const [outcome, setOutcome] = useState("MET");
|
||||
const [conversion, setConversion] = useState("NOT_STARTED");
|
||||
const [promote, setPromote] = useState("");
|
||||
const [sample, setSample] = useState("");
|
||||
const [pob, setPob] = useState("");
|
||||
const [remarks, setRemarks] = useState("");
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
const selectedDoctor = kind === "doctor" ? doctors.find((d) => d.id === targetId) : undefined;
|
||||
const options = kind === "doctor" ? doctors : chemists;
|
||||
// Visit times are local; today's report cannot hold a visit later than now.
|
||||
const isToday = dcr.report_date === todayISO();
|
||||
const label = kind === "doctor" ? "Doctor" : "Chemist";
|
||||
|
||||
const v = useValidation({
|
||||
target: [required(label)],
|
||||
time: [required("Visit time"), (t) => (isToday && t > nowHHMM() ? "A visit today cannot be later than the current time" : null)],
|
||||
promote: [maxLength(200, "Product promoted")],
|
||||
sample: [maxLength(200, "Product sample")],
|
||||
pob: [maxLength(100, "POB"), amount("POB")],
|
||||
remarks: [maxLength(2000, "Remarks")],
|
||||
});
|
||||
const values = () => ({ target: targetId, time, promote, sample, pob, remarks });
|
||||
const on = (name: string, set: (value: string) => void) => (e: { target: { value: string } }) => { set(e.target.value); v.retouch(name, e.target.value); };
|
||||
const blur = (name: string) => (e: { target: { value: string } }) => v.touch(name, e.target.value);
|
||||
|
||||
async function submit(e: FormEvent<HTMLFormElement>) {
|
||||
e.preventDefault();
|
||||
if (!v.check(values(), e.currentTarget)) return;
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const updated = kind === "doctor"
|
||||
? await api.addDoctorCall(dcr.id, {
|
||||
doctor_id: targetId, visit_time: time, call_outcome: outcome,
|
||||
conversion_status: selectedDoctor?.is_core_doctor ? conversion : null,
|
||||
product_promote: promote.trim() || null, product_sample: sample.trim() || null, remarks: remarks.trim() || null,
|
||||
})
|
||||
: await api.addChemistCall(dcr.id, { chemist_id: targetId, visit_time: time, pob: pob.trim() || null, remarks: remarks.trim() || null });
|
||||
onChange(updated);
|
||||
toast.success(`${label} visit added`);
|
||||
setTargetId(""); setPromote(""); setSample(""); setPob(""); setRemarks("");
|
||||
v.clear();
|
||||
} catch (err) {
|
||||
setError(errorMessage(err, "Could not add the visit")); // shown right here, where the MR is looking
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<form className="form-grid" onSubmit={submit} noValidate>
|
||||
<Alert>{error}</Alert>
|
||||
<Field label="Visit type">
|
||||
<Select value={kind} onChange={(e) => { setKind(e.target.value as "doctor" | "chemist"); setTargetId(""); v.clear(); }}>
|
||||
<option value="doctor">Doctor</option>
|
||||
<option value="chemist">Chemist</option>
|
||||
</Select>
|
||||
</Field>
|
||||
<Field label={label} required error={v.errors.target} hint={options.length === 0 ? `No ${kind}s are assigned to you yet — ask your administrator to assign some.` : undefined}>
|
||||
<Select value={targetId} onChange={on("target", setTargetId)} onBlur={blur("target")} disabled={options.length === 0}>
|
||||
<option value="">Choose an assigned {kind}</option>
|
||||
{options.map((o) => (
|
||||
<option key={o.id} value={o.id}>{o.name}{"is_core_doctor" in o && o.is_core_doctor ? " (core)" : ""}</option>
|
||||
))}
|
||||
</Select>
|
||||
</Field>
|
||||
<Field label="Visit time" required error={v.errors.time}>
|
||||
<Input type="time" value={time} max={isToday ? nowHHMM() : undefined} onChange={on("time", setTime)} onBlur={blur("time")} />
|
||||
</Field>
|
||||
{kind === "doctor" && (
|
||||
<Field label="Outcome">
|
||||
<Select value={outcome} onChange={(e) => setOutcome(e.target.value)}>
|
||||
<option value="MET">Met</option>
|
||||
<option value="NOT_AVAILABLE">Not available</option>
|
||||
<option value="RESCHEDULED">Rescheduled</option>
|
||||
</Select>
|
||||
</Field>
|
||||
)}
|
||||
{selectedDoctor?.is_core_doctor && (
|
||||
<Field label="Core doctor status">
|
||||
<Select value={conversion} onChange={(e) => setConversion(e.target.value)}>
|
||||
<option value="NOT_STARTED">Not started</option>
|
||||
<option value="IN_PROGRESS">In progress</option>
|
||||
<option value="CONVERTED">Converted</option>
|
||||
<option value="LOST">Lost</option>
|
||||
</Select>
|
||||
</Field>
|
||||
)}
|
||||
{kind === "doctor" ? (
|
||||
<>
|
||||
<Field label="Product promoted" error={v.errors.promote}>
|
||||
<Input list="product-suggestions" value={promote} onChange={on("promote", setPromote)} onBlur={blur("promote")} placeholder="Pick or type a product" />
|
||||
</Field>
|
||||
<Field label="Product sample" error={v.errors.sample}>
|
||||
<Input list="product-suggestions" value={sample} onChange={on("sample", setSample)} onBlur={blur("sample")} placeholder="Sample given, if any" />
|
||||
</Field>
|
||||
<datalist id="product-suggestions">
|
||||
{(products.data ?? []).map((p) => <option key={p.id} value={p.product_name} />)}
|
||||
</datalist>
|
||||
</>
|
||||
) : (
|
||||
<Field label="POB (order booked)" error={v.errors.pob} hint="Amount, e.g. 12500 or 12,500.50">
|
||||
<Input inputMode="decimal" value={pob} onChange={on("pob", setPob)} onBlur={blur("pob")} placeholder="0" />
|
||||
</Field>
|
||||
)}
|
||||
<Field label="Remarks" span error={v.errors.remarks} hint={`${remarks.length}/2000`}>
|
||||
<Textarea value={remarks} onChange={on("remarks", setRemarks)} onBlur={blur("remarks")} rows={2} />
|
||||
</Field>
|
||||
<div className="form-actions">
|
||||
<Button type="submit" variant="primary" icon="plus" loading={busy}>Add {kind} visit</Button>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user