feat(servicedate automated from direct appointment page)

This commit is contained in:
2025-10-02 00:12:14 +05:30
parent 29ddb4ab92
commit 2ff78db35f
2 changed files with 98 additions and 15 deletions

View File

@@ -73,6 +73,7 @@ interface ClaimFormData {
interface ClaimFormProps { interface ClaimFormProps {
patientId: number; patientId: number;
appointmentId?: number;
onSubmit: (data: ClaimFormData) => Promise<Claim>; onSubmit: (data: ClaimFormData) => Promise<Claim>;
onHandleAppointmentSubmit: ( onHandleAppointmentSubmit: (
appointmentData: InsertAppointment | UpdateAppointment appointmentData: InsertAppointment | UpdateAppointment
@@ -84,6 +85,7 @@ interface ClaimFormProps {
export function ClaimForm({ export function ClaimForm({
patientId, patientId,
appointmentId,
onHandleAppointmentSubmit, onHandleAppointmentSubmit,
onHandleUpdatePatient, onHandleUpdatePatient,
onHandleForMHSelenium, onHandleForMHSelenium,
@@ -147,6 +149,82 @@ export function ClaimForm({
number | null number | null
>(null); >(null);
//incase when appointmentId is given - directly from appoinmentpage - claimpage - to here.
// then, update the service date as per the appointment date.
useEffect(() => {
if (!appointmentId) return;
if (!Number.isFinite(appointmentId) || appointmentId <= 0) return;
let cancelled = false;
(async () => {
try {
const res = await apiRequest(
"GET",
`/api/appointments/${appointmentId}`
);
if (!res.ok) {
let body: any = null;
try {
body = await res.json();
} catch {}
if (!cancelled) {
toast({
title: "Failed to load appointment",
description:
body?.message ??
body?.error ??
`Could not fetch appointment ${appointmentId}.`,
variant: "destructive",
});
}
return;
}
const appointment = await res.json();
// appointment.date is expected to be either "YYYY-MM-DD" or an ISO string.
const rawDate = appointment?.date ?? appointment?.day ?? "";
if (!rawDate) return;
// Use your parseLocalDate to create a local-midnight Date (avoid TZ shifts)
let dateVal: Date;
try {
dateVal = parseLocalDate(String(rawDate));
} catch (e) {
// Fallback - try constructing Date and then normalize
const maybe = new Date(rawDate);
if (isNaN(maybe.getTime())) {
console.error("Could not parse appointment date:", rawDate);
return;
}
dateVal = new Date(
maybe.getFullYear(),
maybe.getMonth(),
maybe.getDate()
);
}
if (!cancelled) {
setServiceDateValue(dateVal);
setServiceDate(formatLocalDate(dateVal));
}
} catch (err: any) {
if (!cancelled) {
console.error("Error fetching appointment:", err);
toast({
title: "Error",
description: err?.message ?? "Failed to fetch Appointment.",
variant: "destructive",
});
}
}
})();
return () => {
cancelled = true;
};
}, [appointmentId]);
// Update service date when calendar date changes // Update service date when calendar date changes
const onServiceDateChange = (date: Date | undefined) => { const onServiceDateChange = (date: Date | undefined) => {
if (date) { if (date) {

View File

@@ -10,7 +10,6 @@ import {
import { ClaimForm } from "@/components/claims/claim-form"; import { ClaimForm } from "@/components/claims/claim-form";
import { useToast } from "@/hooks/use-toast"; import { useToast } from "@/hooks/use-toast";
import { useAuth } from "@/hooks/use-auth"; import { useAuth } from "@/hooks/use-auth";
import { parse } from "date-fns";
import { apiRequest, queryClient } from "@/lib/queryClient"; import { apiRequest, queryClient } from "@/lib/queryClient";
import { useLocation } from "wouter"; import { useLocation } from "wouter";
import { useAppDispatch, useAppSelector } from "@/redux/hooks"; import { useAppDispatch, useAppSelector } from "@/redux/hooks";
@@ -19,7 +18,6 @@ import {
clearTaskStatus, clearTaskStatus,
} from "@/redux/slices/seleniumClaimSubmitTaskSlice"; } from "@/redux/slices/seleniumClaimSubmitTaskSlice";
import { SeleniumTaskBanner } from "@/components/ui/selenium-task-banner"; import { SeleniumTaskBanner } from "@/components/ui/selenium-task-banner";
import { formatLocalDate } from "@/utils/dateUtils";
import ClaimsRecentTable, { import ClaimsRecentTable, {
QK_CLAIMS_BASE, QK_CLAIMS_BASE,
} from "@/components/claims/claims-recent-table"; } from "@/components/claims/claims-recent-table";
@@ -27,27 +25,26 @@ import ClaimsOfPatientModal from "@/components/claims/claims-of-patient-table";
import { import {
Claim, Claim,
InsertAppointment, InsertAppointment,
InsertPatient,
UpdateAppointment, UpdateAppointment,
UpdatePatient, UpdatePatient,
} from "@repo/db/types"; } from "@repo/db/types";
import ClaimDocumentsUploadMultiple from "@/components/claims/claim-document-upload-modal"; import ClaimDocumentsUploadMultiple from "@/components/claims/claim-document-upload-modal";
export default function ClaimsPage() { export default function ClaimsPage() {
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
const [isClaimFormOpen, setIsClaimFormOpen] = useState(false); const [isClaimFormOpen, setIsClaimFormOpen] = useState(false);
const [selectedPatientId, setSelectedPatientId] = useState<number | null>( const [selectedPatientId, setSelectedPatientId] = useState<number | null>(
null null
); );
// for redirect from appointment page directly, then passing to claimform
const [selectedAppointmentId, setSelectedAppointmentId] = useState<
number | null
>(null);
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const { status, message, show } = useAppSelector( const { status, message, show } = useAppSelector(
(state) => state.seleniumClaimSubmitTask (state) => state.seleniumClaimSubmitTask
); );
const { toast } = useToast(); const { toast } = useToast();
const { user } = useAuth(); const { user } = useAuth();
const toggleMobileMenu = () => {
setIsMobileMenuOpen(!isMobileMenuOpen);
};
// Update patient mutation // Update patient mutation
const updatePatientMutation = useMutation({ const updatePatientMutation = useMutation({
@@ -151,8 +148,13 @@ export default function ClaimsPage() {
return { newPatient: params.get("newPatient") }; return { newPatient: params.get("newPatient") };
}, [location]); }, [location]);
const handleNewClaim = (patientId: number) => { const handleNewClaim = (patientId: number, appointmentId?: number) => {
setSelectedPatientId(patientId); setSelectedPatientId(patientId);
// case: when redirect happerns from appointment page.
if (appointmentId) {
setSelectedAppointmentId(appointmentId);
}
setIsClaimFormOpen(true); setIsClaimFormOpen(true);
}; };
@@ -186,7 +188,7 @@ export default function ClaimsPage() {
try { try {
const res = await apiRequest( const res = await apiRequest(
"GET", "GET",
`/api/appointments/${appointmentId}/patient` `/api/appointments/${appointmentId}`
); );
if (!res.ok) { if (!res.ok) {
let body: any = null; let body: any = null;
@@ -195,21 +197,22 @@ export default function ClaimsPage() {
} catch {} } catch {}
if (!cancelled) { if (!cancelled) {
toast({ toast({
title: "Failed to load patient", title: "Failed to load appointment",
description: description:
body?.message ?? body?.message ??
body?.error ?? body?.error ??
`Could not fetch patient for appointment ${appointmentId}.`, `Could not fetch appointment ${appointmentId}.`,
variant: "destructive", variant: "destructive",
}); });
} }
return; return;
} }
const data = await res.json(); const appointment = await res.json();
const patient = data?.patient ?? data; const patientId = appointment?.patientId;
if (!cancelled && patient && patient.id) {
handleNewClaim(patient.id); if (!cancelled && patientId) {
handleNewClaim(patientId, appointmentId);
clearUrlParams(["appointmentId"]); clearUrlParams(["appointmentId"]);
} }
} catch (err: any) { } catch (err: any) {
@@ -404,6 +407,7 @@ export default function ClaimsPage() {
// 6. close claim // 6. close claim
const closeClaim = () => { const closeClaim = () => {
setSelectedPatientId(null); setSelectedPatientId(null);
setSelectedAppointmentId(null);
setIsClaimFormOpen(false); setIsClaimFormOpen(false);
clearUrlParams(["newPatient", "appointmentId"]); clearUrlParams(["newPatient", "appointmentId"]);
@@ -458,6 +462,7 @@ export default function ClaimsPage() {
{isClaimFormOpen && selectedPatientId !== null && ( {isClaimFormOpen && selectedPatientId !== null && (
<ClaimForm <ClaimForm
patientId={selectedPatientId} patientId={selectedPatientId}
appointmentId={selectedAppointmentId ?? undefined}
onClose={closeClaim} onClose={closeClaim}
onSubmit={handleClaimSubmit} onSubmit={handleClaimSubmit}
onHandleAppointmentSubmit={handleAppointmentSubmit} onHandleAppointmentSubmit={handleAppointmentSubmit}