import { useEffect, useState } from "react"; import { AlertCircle } from "lucide-react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "../../../components/ui/dialog.tsx"; import { Alert, AlertDescription } from "../../../components/ui/alert.tsx"; import { Button } from "../../../components/ui/button.tsx"; import { Label } from "../../../components/ui/label.tsx"; import { Textarea } from "../../../components/ui/textarea.tsx"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../../components/ui/select.tsx"; import type { InboundRequisition, RefusalResult, RequisitionContact } from "../../../hooks/useInboundRequisitions.ts"; import { BLANK_CONTACT, ContactFields, contactFrom, contactReady, describeContact, failureWording, newStamp, type AttemptStamp, type ContactDraft, } from "./RequisitionContactDialog.tsx"; // The server's refusal codes, worded from the laboratory's side; the server checks them. export const REASONS = [ { code: "unknown-test", label: "We do not offer that test" }, { code: "retired-test", label: "That test is retired" }, { code: "unknown-account", label: "We do not recognise the account" }, { code: "missing-field", label: "The order is missing something we require" }, { code: "not-authorised", label: "The account may not order that test" }, { code: "other", label: "Something else" }, ] as const; interface Props { requisition?: InboundRequisition; onClose: () => void; refuse: ( requisitionId: string, refusal: { reasonCode: string; message: string; contact?: Omit }, ) => Promise; } const RefuseRequisitionDialog = ({ requisition, onClose, refuse }: Props) => { const [reasonCode, setReasonCode] = useState(); const [message, setMessage] = useState(""); const [busy, setBusy] = useState(false); const [error, setError] = useState(); const [recording, setRecording] = useState(false); const [contact, setContact] = useState(BLANK_CONTACT); const [stamp, setStamp] = useState(newStamp); const [done, setDone] = useState(); useEffect(() => { setReasonCode(undefined); setMessage(""); setError(undefined); setRecording(false); setContact(BLANK_CONTACT); setStamp(newStamp()); setDone(undefined); }, [requisition?.requisitionId]); if (!requisition?.requisitionId) return null; // Paper has no Akashic channel back: staff tell the sender, and may record that they did. const paper = !requisition.akashic; const from = requisition.from ?? "the sender"; const submit = async () => { setBusy(true); setError(undefined); try { const result = await refuse(requisition.requisitionId as string, { reasonCode: reasonCode as string, message: message.trim(), ...(paper && recording ? { contact: contactFrom(contact, stamp) } : {}), }); if (paper) setDone(result); else onClose(); } catch (failure) { console.error("Error refusing an inbound requisition:", failure); setError(failureWording(failure, "refusal")); } finally { setBusy(false); } }; if (done) { return ( { if (!open) onClose(); }}> Order refused {`Refused. ${from} has not been told through Akashic.`} {(done.contacts ?? []).length === 0 ? (

No contact attempt recorded yet.

) : (done.contacts ?? []).map((attempt) => (

{`Contact attempt recorded: ${describeContact(attempt)}`}

))}
); } return ( { if (!open && !busy) onClose(); }}> Refuse order {[requisition.patientName, requisition.test ?? "Test not named"].filter(Boolean).join(" · ")} {error ? ( {error} ) : null}