import { useState } from "react"; import { Loader2 } from "lucide-react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "../../../components/ui/dialog"; import { Button } from "../../../components/ui/button"; import { useReferralWorkbenchService } from "../../../services/ReferralWorkbenchService"; import { stillOpen, type ReferralRequestScope } from "../../../utils/referralRequestScope"; import { relayServerFailure } from "../../../utils/fetchFailureMessage"; import type { ReferralQueueRow, ReferralWithdrawalReason } from "../../../hooks/useReferralQueue"; import "./referralDialog.css"; // - WITHDRAW A REFERRAL PLACED IN ERROR, as referrals.html#s-withdraw-referral draws the confirmation. // // The sender corrects an error without claiming the other practice accepted or booked anything. The server // records who and why, stops any pending Akashic delivery or claim and keeps the order. What it cannot do - // and what this screen must never promise - is recall a copy already in another practice's hands, so the // warning tells the sender to contact them. export const WITHDRAWAL_REASON_LABELS: Record = { "wrong-practice": "Wrong practice", "placed-in-error": "Placed in error", other: "Other", }; const WithdrawReferralDialog = ({ row, onOpenChange, onWithdrawn, scope, }: { row: ReferralQueueRow | null; onOpenChange: (open: boolean) => void; onWithdrawn: () => void; /** The opening this dialog belongs to: its requests are admitted by it, and nothing is shown once it retires. */ scope?: ReferralRequestScope; }) => { const { withdrawReferral } = useReferralWorkbenchService(); const [reason, setReason] = useState("wrong-practice"); const [busy, setBusy] = useState(false); const [error, setError] = useState(); const close = (open: boolean) => { if (busy) return; if (!open) { setError(undefined); setReason("wrong-practice"); } onOpenChange(open); }; const confirm = async () => { if (!row?.orderId) return; setBusy(true); setError(undefined); try { await withdrawReferral(row.orderId, reason, scope); if (!stillOpen(scope)) return; setBusy(false); onWithdrawn(); close(false); } catch (failure) { setBusy(false); if (!stillOpen(scope)) return; setError(relayServerFailure(failure, "The referral could not be withdrawn. Nothing was changed.")); } }; const practice = row?.performerDisplay ?? "the receiving practice"; return ( Withdraw this referral? {row?.patientDisplay ?? "This patient"} ยท {practice}

This stops pending Akashic delivery and claim. If the receiving practice already has a copy, withdrawing here cannot erase it. Contact them directly.

{error && (

{error}

)}
); }; export default WithdrawReferralDialog;