fix: MH claim submit — wait for socket job:update before fetching PDF and claim number
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useState, useEffect, useMemo } from "react";
|
||||
import { useState, useEffect, useMemo, useRef } from "react";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import {
|
||||
Card,
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
clearTaskStatus,
|
||||
} from "@/redux/slices/seleniumTaskSlice";
|
||||
import { SeleniumTaskBanner } from "@/components/ui/selenium-task-banner";
|
||||
import { useJobStatus } from "@/hooks/use-job-status";
|
||||
import ClaimsRecentTable, {
|
||||
QK_CLAIMS_BASE,
|
||||
} from "@/components/claims/claims-recent-table";
|
||||
@@ -49,6 +50,14 @@ export default function ClaimsPage() {
|
||||
const { status, message, show } = useAppSelector(
|
||||
(state) => state.seleniumTasks.claimSubmit
|
||||
);
|
||||
|
||||
// Track pending selenium jobs so we can react to completion via socket
|
||||
const [pendingClaimJobId, setPendingClaimJobId] = useState<string | null>(null);
|
||||
const pendingClaimMeta = useRef<{
|
||||
patientId: number | null;
|
||||
groupKey: "INSURANCE_CLAIM" | "INSURANCE_CLAIM_PREAUTH";
|
||||
}>({ patientId: null, groupKey: "INSURANCE_CLAIM" });
|
||||
const { status: jobStatus, result: jobResult } = useJobStatus(pendingClaimJobId);
|
||||
const { toast } = useToast();
|
||||
const { user } = useAuth();
|
||||
|
||||
@@ -130,6 +139,24 @@ export default function ClaimsPage() {
|
||||
},
|
||||
});
|
||||
|
||||
// React to selenium job completing via socket
|
||||
useEffect(() => {
|
||||
if (!pendingClaimJobId) return;
|
||||
if (jobStatus === "completed" && jobResult) {
|
||||
setPendingClaimJobId(null);
|
||||
handleMHSeleniumPdfDownload(
|
||||
jobResult,
|
||||
pendingClaimMeta.current.patientId,
|
||||
pendingClaimMeta.current.groupKey
|
||||
);
|
||||
queryClient.invalidateQueries({ queryKey: ["claims-recent"] });
|
||||
} else if (jobStatus === "failed") {
|
||||
setPendingClaimJobId(null);
|
||||
dispatch(setTaskStatus({ key: "claimSubmit", status: "error", message: "Selenium job failed" }));
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [jobStatus, jobResult, pendingClaimJobId]);
|
||||
|
||||
// small helper: remove given query params from the current URL (silent, no reload)
|
||||
const clearUrlParams = (params: string[]) => {
|
||||
try {
|
||||
@@ -331,31 +358,23 @@ export default function ClaimsPage() {
|
||||
const result1 = await response.json();
|
||||
if (result1.error) throw new Error(result1.error);
|
||||
|
||||
if (result1.claimNumber) {
|
||||
await queryClient.invalidateQueries({ queryKey: ["claims-recent"] });
|
||||
}
|
||||
// Job is queued — wait for socket completion before downloading PDF
|
||||
pendingClaimMeta.current = { patientId: selectedPatientId, groupKey: "INSURANCE_CLAIM" };
|
||||
setPendingClaimJobId(result1.jobId);
|
||||
|
||||
dispatch(
|
||||
setTaskStatus({
|
||||
key: "claimSubmit",
|
||||
status: "pending",
|
||||
message: "Submitted to Selenium. Awaiting PDF...",
|
||||
message: "Submitted to Selenium. Awaiting result...",
|
||||
})
|
||||
);
|
||||
|
||||
toast({
|
||||
title: "Selenium service notified",
|
||||
description:
|
||||
"Your claim data was successfully sent to Selenium, Waitinig for its response.",
|
||||
description: "Claim submitted to Selenium. Waiting for confirmation...",
|
||||
variant: "default",
|
||||
});
|
||||
|
||||
const result2 = await handleMHSeleniumPdfDownload(
|
||||
result1,
|
||||
selectedPatientId,
|
||||
"INSURANCE_CLAIM"
|
||||
);
|
||||
return result2;
|
||||
} catch (error: any) {
|
||||
dispatch(
|
||||
setTaskStatus({
|
||||
@@ -475,27 +494,23 @@ export default function ClaimsPage() {
|
||||
const result1 = await response.json();
|
||||
if (result1.error) throw new Error(result1.error);
|
||||
|
||||
// Job is queued — wait for socket completion before downloading PDF
|
||||
pendingClaimMeta.current = { patientId: selectedPatientId, groupKey: "INSURANCE_CLAIM_PREAUTH" };
|
||||
setPendingClaimJobId(result1.jobId);
|
||||
|
||||
dispatch(
|
||||
setTaskStatus({
|
||||
key: "claimSubmit",
|
||||
status: "pending",
|
||||
message: "Submitted to Selenium. Awaiting PDF...",
|
||||
message: "Submitted to Selenium. Awaiting result...",
|
||||
})
|
||||
);
|
||||
|
||||
toast({
|
||||
title: "Selenium service notified",
|
||||
description:
|
||||
"Your claim pre auth data was successfully sent to Selenium, Waitinig for its response.",
|
||||
description: "Pre-auth submitted to Selenium. Waiting for confirmation...",
|
||||
variant: "default",
|
||||
});
|
||||
|
||||
const result2 = await handleMHSeleniumPdfDownload(
|
||||
result1,
|
||||
selectedPatientId,
|
||||
"INSURANCE_CLAIM_PREAUTH"
|
||||
);
|
||||
return result2;
|
||||
} catch (error: any) {
|
||||
dispatch(
|
||||
setTaskStatus({
|
||||
|
||||
Reference in New Issue
Block a user