diff --git a/compliance-web/src/components/App/CaseFiles/Profile/CaseFileActions.tsx b/compliance-web/src/components/App/CaseFiles/Profile/CaseFileActions.tsx index 3ea414c..fe474c4 100644 --- a/compliance-web/src/components/App/CaseFiles/Profile/CaseFileActions.tsx +++ b/compliance-web/src/components/App/CaseFiles/Profile/CaseFileActions.tsx @@ -32,21 +32,25 @@ const CaseFileActions: React.FC = ({ fileNumber, ]); - const onUpdateStatusSuccess = useCallback(() => { + const closeAndRefresh = useCallback(() => { queryClient.invalidateQueries({ queryKey: ["case-file", fileNumber], }); - notify.success("Case File status updated!"); + queryClient.invalidateQueries({ + queryKey: ["continuation-reports", caseFileData?.id], + }); setClose(); - }, [queryClient, fileNumber, setClose]); + }, [caseFileData, fileNumber, queryClient, setClose]); + + const onUpdateStatusSuccess = useCallback(() => { + notify.success("Case File status updated!"); + closeAndRefresh(); + }, [closeAndRefresh]); const onLinkCaseFileSuccess = useCallback(() => { - queryClient.invalidateQueries({ - queryKey: ["case-file", fileNumber], - }); notify.success("Case file link is updated"); - setClose(); - }, [queryClient, fileNumber, setClose]); + closeAndRefresh(); + }, [closeAndRefresh]); const onDeleteSuccess = useCallback(() => { notify.success("Case File deleted!"); @@ -89,7 +93,10 @@ const CaseFileActions: React.FC = ({ { - unlinkCaseFile({id: caseFileData?.id ?? 0, linkId: caseFileId}) + unlinkCaseFile({ + id: caseFileData?.id ?? 0, + linkId: caseFileId, + }); }} linkedCaseFiles={caseFileData?.caseFileLinks ?? []} isEdit diff --git a/compliance-web/src/components/App/Complaints/Profile/ComplaintFileActions.tsx b/compliance-web/src/components/App/Complaints/Profile/ComplaintFileActions.tsx index 6a844b4..b5f45e3 100644 --- a/compliance-web/src/components/App/Complaints/Profile/ComplaintFileActions.tsx +++ b/compliance-web/src/components/App/Complaints/Profile/ComplaintFileActions.tsx @@ -9,7 +9,7 @@ import { useDeleteComplaint, useUpdateComplaintStatus, } from "@/hooks/useComplaints"; -import router from "@/router/router"; +import { useRouter } from "@tanstack/react-router"; interface ComplaintFileActionsProps { status: string; @@ -20,6 +20,7 @@ const ComplaintFileActions: React.FC = ({ status, fileNumber, }) => { + const router = useRouter(); const queryClient = useQueryClient(); const { setOpen, setClose } = useModal(); @@ -32,15 +33,18 @@ const ComplaintFileActions: React.FC = ({ queryClient.invalidateQueries({ queryKey: ["complaint", fileNumber], }); + queryClient.invalidateQueries({ + queryKey: ["continuation-reports", complaintData?.case_file_id], + }); notify.success("Complaint status updated"); setClose(); - }, [fileNumber, queryClient, setClose]); + }, [complaintData, fileNumber, queryClient, setClose]); const onDeleteSuccess = useCallback(() => { notify.success("Complaint deleted!"); setClose(); router.navigate({ to: "/ce-database/complaints" }); - }, [setClose]); + }, [router, setClose]); const { mutate: updateComplaintStatus } = useUpdateComplaintStatus( onUpdateStatusSuccess @@ -62,7 +66,7 @@ const ComplaintFileActions: React.FC = ({ onConfirm={() => { updateComplaintStatus({ id: complaintData?.id ?? 0, - caseFileStatus: { status: "CLOSED" }, + complaintStatus: { status: "CLOSED" }, }); }} /> @@ -84,7 +88,7 @@ const ComplaintFileActions: React.FC = ({ onConfirm={() => { updateComplaintStatus({ id: complaintData?.id ?? 0, - caseFileStatus: { status: "OPEN" }, + complaintStatus: { status: "OPEN" }, }); }} /> diff --git a/compliance-web/src/components/App/Inspections/Profile/InspectionFileActions.tsx b/compliance-web/src/components/App/Inspections/Profile/InspectionFileActions.tsx index 337cca4..ac00c58 100644 --- a/compliance-web/src/components/App/Inspections/Profile/InspectionFileActions.tsx +++ b/compliance-web/src/components/App/Inspections/Profile/InspectionFileActions.tsx @@ -4,8 +4,12 @@ import ConfirmationModal from "@/components/Shared/Popups/ConfirmationModal"; import { useModal } from "@/store/modalStore"; import { useQueryClient } from "@tanstack/react-query"; import { Inspection } from "@/models/Inspection"; -import { useCloseInspection } from "@/hooks/useInspections"; import { notify } from "@/store/snackbarStore"; +import { + useDeleteInspection, + useUpdateInspectionStatus, +} from "@/hooks/useInspections"; +import { useRouter } from "@tanstack/react-router"; interface InspectionFileActionsProps { status: string; @@ -16,6 +20,7 @@ const InspectionFileActions: React.FC = ({ status, fileNumber, }) => { + const router = useRouter(); const queryClient = useQueryClient(); const { setOpen, setClose } = useModal(); @@ -28,14 +33,25 @@ const InspectionFileActions: React.FC = ({ queryClient.invalidateQueries({ queryKey: ["inspection", fileNumber], }); + queryClient.invalidateQueries({ + queryKey: ["continuation-reports", inspectionData?.case_file_id], + }); notify.success("Inspection status updated"); setClose(); - }, [fileNumber, queryClient, setClose]); + }, [fileNumber, inspectionData, queryClient, setClose]); - const { mutate: closeInspection } = useCloseInspection( + const onDeleteSuccess = useCallback(() => { + notify.success("Inspection deleted!"); + setClose(); + router.navigate({ to: "/ce-database/inspections" }); + }, [router, setClose]); + + const { mutate: updateInspectionInspection } = useUpdateInspectionStatus( onUpdateStatusSuccess ); + const { mutate: deleteInspection } = useDeleteInspection(onDeleteSuccess); + const actionsList = [ { text: "Cancel Inspection", @@ -48,14 +64,16 @@ const InspectionFileActions: React.FC = ({ description="Are you sure you want to cancel this inspection?" confirmButtonText="Cancel Inspection" onConfirm={() => { - // TODO: Implement cancel inspection - setClose(); + updateInspectionInspection({ + id: inspectionData?.id ?? 0, + inspectionStatus: { status: "CANCELED" }, + }); }} /> ), }); }, - hidden: status?.toLowerCase() === "closed", + hidden: ["canceled", "closed"].includes(status?.toLowerCase()), }, { text: "Close as Note to File", @@ -68,34 +86,17 @@ const InspectionFileActions: React.FC = ({ description="Are you sure you want to close inspection as note to file?" confirmButtonText="Close Inspection" onConfirm={() => { - closeInspection({ id: inspectionData?.id ?? 0 }); + updateInspectionInspection({ + id: inspectionData?.id ?? 0, + inspectionStatus: { status: "CLOSED" }, + }); }} /> ), width: "420px", }); }, - hidden: status?.toLowerCase() === "closed", - }, - { - text: "Reopen Inspection", - onClick: () => { - // Handle reopening inspection - setOpen({ - content: ( - { - // TODO: Implement reopen inspection - setClose(); - }} - /> - ), - }); - }, - hidden: status?.toLowerCase() === "open", + hidden: ["canceled", "closed"].includes(status?.toLowerCase()), }, { text: "Delete Inspection", @@ -108,8 +109,7 @@ const InspectionFileActions: React.FC = ({ description="You are about to delete this inspection. Are you sure?" confirmButtonText="Delete" onConfirm={() => { - // TODO: Implement delete inspection - setClose(); + deleteInspection(inspectionData?.id ?? 0); }} /> ), diff --git a/compliance-web/src/hooks/useComplaints.tsx b/compliance-web/src/hooks/useComplaints.tsx index 541c111..503465e 100644 --- a/compliance-web/src/hooks/useComplaints.tsx +++ b/compliance-web/src/hooks/useComplaints.tsx @@ -1,4 +1,8 @@ -import { Complaint, ComplaintAPIData, ComplaintStatusAPIData } from "@/models/Complaint"; +import { + Complaint, + ComplaintAPIData, + ComplaintStatusAPIData, +} from "@/models/Complaint"; import { ComplaintSource } from "@/models/ComplaintSource"; import { Contact } from "@/models/Contact"; import { @@ -54,12 +58,16 @@ const updateComplaint = ({ const updateComplaintStatus = ({ id, - caseFileStatus, + complaintStatus, }: { id: number; - caseFileStatus: ComplaintStatusAPIData; + complaintStatus: ComplaintStatusAPIData; }) => { - return request({ url: `/complaints/${id}/status`, method: "patch", data: caseFileStatus }); + return request({ + url: `/complaints/${id}/status`, + method: "patch", + data: complaintStatus, + }); }; const deleteComplaint = (id: number) => { diff --git a/compliance-web/src/hooks/useInspections.tsx b/compliance-web/src/hooks/useInspections.tsx index a4a7dcb..6180be5 100644 --- a/compliance-web/src/hooks/useInspections.tsx +++ b/compliance-web/src/hooks/useInspections.tsx @@ -1,6 +1,10 @@ import { Attendance, InspectionAttendance } from "@/models/Attendance"; import { Initiation } from "@/models/Initiation"; -import { Inspection, InspectionAPIData } from "@/models/Inspection"; +import { + Inspection, + InspectionAPIData, + InspectionStatusAPIData, +} from "@/models/Inspection"; import { IRStatus } from "@/models/IRStatus"; import { IRType } from "@/models/IRType"; import { ProjectStatus } from "@/models/ProjectStatus"; @@ -59,13 +63,24 @@ const updateInspection = ({ }); }; -const closeInspection = ({ id }: { id: number }) => { +const updateInspectionStatus = ({ + id, + inspectionStatus, +}: { + id: number; + inspectionStatus: InspectionStatusAPIData; +}) => { return request({ - url: `/inspections/${id}/close`, + url: `/inspections/${id}/status`, method: "patch", + data: inspectionStatus, }); }; +const deleteInspection = (id: number) => { + return request({ url: `/inspections/${id}`, method: "delete" }); +}; + export const useIRTypesData = () => { return useQuery({ queryKey: ["ir-types"], @@ -138,6 +153,10 @@ export const useUpdateInspection = (onSuccess: OnSuccessType) => { return useMutation({ mutationFn: updateInspection, onSuccess }); }; -export const useCloseInspection = (onSuccess: OnSuccessType) => { - return useMutation({ mutationFn: closeInspection, onSuccess }); +export const useUpdateInspectionStatus = (onSuccess: OnSuccessType) => { + return useMutation({ mutationFn: updateInspectionStatus, onSuccess }); +}; + +export const useDeleteInspection = (onSuccess: OnSuccessType) => { + return useMutation({ mutationFn: deleteInspection, onSuccess }); }; diff --git a/compliance-web/src/models/Inspection.ts b/compliance-web/src/models/Inspection.ts index efa03f0..def4476 100644 --- a/compliance-web/src/models/Inspection.ts +++ b/compliance-web/src/models/Inspection.ts @@ -78,3 +78,7 @@ export interface InspectionAPIData { unapproved_project_type?: string; unapproved_project_sub_type?: string; } + +export interface InspectionStatusAPIData { + status: string; +}