Server backup 2026-11-07 of uncommitted work in projects/pharma-web-celtic
Snapshot of the working tree on srv1071960 (branch main), taken during server cleanup. .env files, node_modules, builds, logs and media were excluded.
This commit is contained in:
@@ -8,18 +8,22 @@ 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";
|
||||
import type { Chemist, Dcr, Doctor, Stockist } from "../../types";
|
||||
|
||||
/** Pre-selects a doctor/chemist, e.g. "Record visit" on a planned line. A new `key` re-applies it. */
|
||||
export interface VisitPreset { kind: "doctor" | "chemist"; targetId: string; key: number }
|
||||
export type VisitKind = "doctor" | "chemist" | "stockist";
|
||||
|
||||
export function AddVisitForm({ dcr, doctors, chemists, onChange, preset }: {
|
||||
dcr: Dcr; doctors: Doctor[]; chemists: Chemist[]; onChange: (dcr: Dcr) => void; preset?: VisitPreset | null;
|
||||
/** Pre-selects a doctor/chemist/stockist, e.g. "Record visit" on a planned line. A new `key` re-applies it. */
|
||||
export interface VisitPreset { kind: VisitKind; targetId: string; key: number }
|
||||
|
||||
const KIND_LABEL: Record<VisitKind, string> = { doctor: "Doctor", chemist: "Chemist", stockist: "Stockist" };
|
||||
|
||||
export function AddVisitForm({ dcr, doctors, chemists, stockists, onChange, preset }: {
|
||||
dcr: Dcr; doctors: Doctor[]; chemists: Chemist[]; stockists: Stockist[]; onChange: (dcr: Dcr) => void; preset?: VisitPreset | null;
|
||||
}) {
|
||||
const toast = useToast();
|
||||
const products = useAsync(api.stockInventory, []);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [kind, setKind] = useState<"doctor" | "chemist">("doctor");
|
||||
const [kind, setKind] = useState<VisitKind>("doctor");
|
||||
const [targetId, setTargetId] = useState("");
|
||||
const [time, setTime] = useState(nowHHMM);
|
||||
const [outcome, setOutcome] = useState("MET");
|
||||
@@ -33,7 +37,7 @@ export function AddVisitForm({ dcr, doctors, chemists, onChange, preset }: {
|
||||
const formRef = useRef<HTMLFormElement>(null);
|
||||
// A day plan records who and when only; outcomes, products and POB come at the end of the day.
|
||||
const isPlan = dcr.report_type === "PLAN";
|
||||
// An Area Manager's own report lists the headquarters' doctors and chemists, not assigned ones.
|
||||
// An Area Business Manager's own report lists the headquarters' doctors and chemists, not assigned ones.
|
||||
const isSelf = dcr.report_type === "SELF";
|
||||
const productNames = (products.data ?? []).map((p) => p.product_name);
|
||||
|
||||
@@ -47,11 +51,11 @@ export function AddVisitForm({ dcr, doctors, chemists, onChange, preset }: {
|
||||
}, [preset?.key]);
|
||||
|
||||
const selectedDoctor = kind === "doctor" ? doctors.find((d) => d.id === targetId) : undefined;
|
||||
const options = kind === "doctor" ? doctors : chemists;
|
||||
const options = kind === "doctor" ? doctors : kind === "chemist" ? chemists : stockists;
|
||||
// Visit times are local; today's report cannot hold a visit later than now.
|
||||
const isToday = !isPlan && dcr.report_date === todayISO();
|
||||
const timeLabel = isPlan ? "Planned time" : "Visit time";
|
||||
const label = kind === "doctor" ? "Doctor" : "Chemist";
|
||||
const label = KIND_LABEL[kind];
|
||||
|
||||
const v = useValidation({
|
||||
target: [required(label)],
|
||||
@@ -70,7 +74,9 @@ export function AddVisitForm({ dcr, doctors, chemists, onChange, preset }: {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const updated = isPlan
|
||||
const updated = kind === "stockist"
|
||||
? await api.addStockistCall(dcr.id, { stockist_id: targetId, visit_time: time, pob: isPlan ? null : pob.trim() || null, remarks: remarks.trim() || null })
|
||||
: isPlan
|
||||
? kind === "doctor"
|
||||
? await api.addDoctorCall(dcr.id, { doctor_id: targetId, visit_time: time, remarks: remarks.trim() || null })
|
||||
: await api.addChemistCall(dcr.id, { chemist_id: targetId, visit_time: time, remarks: remarks.trim() || null })
|
||||
@@ -96,14 +102,15 @@ export function AddVisitForm({ dcr, doctors, chemists, onChange, preset }: {
|
||||
<form ref={formRef} 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(); }}>
|
||||
<Select value={kind} onChange={(e) => { setKind(e.target.value as VisitKind); setTargetId(""); v.clear(); }}>
|
||||
<option value="doctor">Doctor</option>
|
||||
<option value="chemist">Chemist</option>
|
||||
<option value="stockist">Stockist</option>
|
||||
</Select>
|
||||
</Field>
|
||||
<Field label={label} required error={v.errors.target} hint={options.length === 0 ? (isSelf ? `This headquarters has no ${kind}s yet.` : `No ${kind}s are assigned to you yet — ask your administrator to assign some.`) : undefined}>
|
||||
<Field label={label} required error={v.errors.target} hint={options.length === 0 ? (isSelf ? `This headquarters has no ${kind}s yet.` : kind === "stockist" ? "Your headquarters has no stockists yet — ask your administrator to add some." : `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="">{isSelf ? `Choose a ${kind}` : `Choose an assigned ${kind}`}</option>
|
||||
<option value="">{isSelf || kind === "stockist" ? `Choose a ${kind}` : `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>
|
||||
))}
|
||||
|
||||
Reference in New Issue
Block a user