From c8d2c139c7792652598d0218f6adf59ad70a2b11 Mon Sep 17 00:00:00 2001 From: Potenz Date: Sun, 18 Jan 2026 23:52:06 +0530 Subject: [PATCH] fix - autorun selenium from aptmnt page - deleted this --- .../src/pages/insurance-status-page.tsx | 112 +++++------------- 1 file changed, 32 insertions(+), 80 deletions(-) diff --git a/apps/Frontend/src/pages/insurance-status-page.tsx b/apps/Frontend/src/pages/insurance-status-page.tsx index 92951ec..bdb8ae4 100644 --- a/apps/Frontend/src/pages/insurance-status-page.tsx +++ b/apps/Frontend/src/pages/insurance-status-page.tsx @@ -34,7 +34,7 @@ export default function InsuranceStatusPage() { const { toast } = useToast(); const dispatch = useAppDispatch(); const { status, message, show } = useAppSelector( - (state) => state.seleniumEligibilityCheckTask + (state) => state.seleniumEligibilityCheckTask, ); const [selectedPatient, setSelectedPatient] = useState(null); const [location] = useLocation(); @@ -56,12 +56,6 @@ export default function InsuranceStatusPage() { string | null >(null); - // 1) state to remember we should auto-run once patient arrives - const [pendingAutoAction, setPendingAutoAction] = useState<{ - appointmentId: number; - action: "eligibility" | "claim"; - } | null>(null); - // Populate fields from selected patient useEffect(() => { if (selectedPatient) { @@ -129,12 +123,12 @@ export default function InsuranceStatusPage() { setTaskStatus({ status: "pending", message: "Sending Data to Selenium...", - }) + }), ); const response = await apiRequest( "POST", "/api/insurance-status/eligibility-check", - { data: JSON.stringify(data) } + { data: JSON.stringify(data) }, ); const result = await response.json(); if (result.error) throw new Error(result.error); @@ -144,7 +138,7 @@ export default function InsuranceStatusPage() { status: "success", message: "Patient status is updated, and its eligibility pdf is uploaded at Document Page.", - }) + }), ); toast({ @@ -161,7 +155,7 @@ export default function InsuranceStatusPage() { setPreviewPdfId(Number(result.pdfFileId)); // optional fallback name while header is parsed setPreviewFallbackFilename( - result.pdfFilename ?? `eligibility_${memberId}.pdf` + result.pdfFilename ?? `eligibility_${memberId}.pdf`, ); setPreviewOpen(true); } @@ -170,7 +164,7 @@ export default function InsuranceStatusPage() { setTaskStatus({ status: "error", message: error.message || "Selenium submission failed", - }) + }), ); toast({ title: "Selenium service error", @@ -194,12 +188,12 @@ export default function InsuranceStatusPage() { setTaskStatus({ status: "pending", message: "Sending Data to Selenium...", - }) + }), ); const response = await apiRequest( "POST", "/api/insurance-status/claim-status-check", - { data: JSON.stringify(data) } + { data: JSON.stringify(data) }, ); const result = await response.json(); if (result.error) throw new Error(result.error); @@ -209,7 +203,7 @@ export default function InsuranceStatusPage() { status: "success", message: "Claim status is updated, and its pdf is uploaded at Document Page.", - }) + }), ); toast({ @@ -226,7 +220,7 @@ export default function InsuranceStatusPage() { setPreviewPdfId(Number(result.pdfFileId)); // optional fallback name while header is parsed setPreviewFallbackFilename( - result.pdfFilename ?? `eligibility_${memberId}.pdf` + result.pdfFilename ?? `eligibility_${memberId}.pdf`, ); setPreviewOpen(true); } @@ -235,7 +229,7 @@ export default function InsuranceStatusPage() { setTaskStatus({ status: "error", message: error.message || "Selenium submission failed", - }) + }), ); toast({ title: "Selenium service error", @@ -369,7 +363,6 @@ export default function InsuranceStatusPage() { // set selectedPatient as before setSelectedPatient(patient as Patient); - setPendingAutoAction({ appointmentId: id, action: action as any }); clearUrlParams(["appointmentId", "action"]); } } catch (err: any) { @@ -390,82 +383,41 @@ export default function InsuranceStatusPage() { }; }, [location]); - // ---------- same case1: runs when selectedPatient AND form fields are ready ---------- + // handling case-1, when redirect happens from appointment page: useEffect(() => { - if (!pendingAutoAction) return; - if (!selectedPatient) return; // wait until fetch effect set it + const params = new URLSearchParams(window.location.search); + const appointmentId = params.get("appointmentId"); + if (!appointmentId) return; - if ( - selectedPatient && - memberId === "" && - firstName === "" && - dateOfBirth === null - ) { - // form hasn't been populated yet; do nothing and wait for the next re-render - return; - } + const id = Number(appointmentId); + if (Number.isNaN(id) || id <= 0) return; let cancelled = false; - let inFlight = false; - // helper: determine final values using both selectedPatient and current form state - const finalMemberId = - (selectedPatient?.insuranceId - ? String(selectedPatient.insuranceId).trim() - : "") || (memberId ? memberId.trim() : ""); - - const finalFirstName = - (selectedPatient?.firstName - ? String(selectedPatient.firstName).trim() - : "") || (firstName ? firstName.trim() : ""); - - // DOB: try component state first (user may have typed), else patient fallback - const parsedDobFromPatient = - selectedPatient?.dateOfBirth != null - ? typeof selectedPatient.dateOfBirth === "string" - ? parseLocalDate(selectedPatient.dateOfBirth) - : selectedPatient.dateOfBirth - : null; - const finalDob = dateOfBirth ?? parsedDobFromPatient ?? null; - - const missing: string[] = []; - if (!finalMemberId) missing.push("Member ID"); - if (!finalFirstName) missing.push("First Name"); - if (!finalDob) missing.push("Date of Birth"); - - if (missing.length > 0) { - toast({ - title: "Missing Fields", - description: `Cannot auto-run. Missing: ${missing.join(", ")}.`, - variant: "destructive", - }); - return; - } - - // If ready, call the requested handler once. Clear pendingAutoAction afterwards. (async () => { - if (cancelled) return; - if (inFlight) return; - inFlight = true; - try { - if (pendingAutoAction.action === "eligibility") { - await handleMHEligibilityButton(); - } else { - await handleMHStatusButton(); + const res = await apiRequest("GET", `/api/appointments/${id}/patient`); + if (!res.ok) return; + + const data = await res.json(); + const patient = data?.patient ?? data; + + if (!cancelled && patient) { + // ✅ ONLY prefill patient + setSelectedPatient(patient as Patient); + + // ✅ clean URL (no auto selenium) + clearUrlParams(["appointmentId", "action"]); } } catch (err) { - console.error("Auto MH action failed:", err); - } finally { - inFlight = false; - if (!cancelled) setPendingAutoAction(null); // clear so it doesn't run again + console.error("Failed to fetch patient from appointment", err); } })(); return () => { cancelled = true; }; - }, [pendingAutoAction, selectedPatient, memberId, firstName, dateOfBirth]); + }, [location]); return (
@@ -589,7 +541,7 @@ export default function InsuranceStatusPage() { onPdfReady={(pdfId, fallbackFilename) => { setPreviewPdfId(pdfId); setPreviewFallbackFilename( - fallbackFilename ?? `eligibility_ddma_${memberId}.pdf` + fallbackFilename ?? `eligibility_ddma_${memberId}.pdf`, ); setPreviewOpen(true); }}