Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inspection actions #174

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,25 @@ const CaseFileActions: React.FC<CaseFileActionsProps> = ({
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!");
Expand Down Expand Up @@ -89,7 +93,10 @@ const CaseFileActions: React.FC<CaseFileActionsProps> = ({
<LinkCaseFileModal
fileNumber={fileNumber}
onSubmit={(caseFileId) => {
unlinkCaseFile({id: caseFileData?.id ?? 0, linkId: caseFileId})
unlinkCaseFile({
id: caseFileData?.id ?? 0,
linkId: caseFileId,
});
}}
linkedCaseFiles={caseFileData?.caseFileLinks ?? []}
isEdit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +20,7 @@ const ComplaintFileActions: React.FC<ComplaintFileActionsProps> = ({
status,
fileNumber,
}) => {
const router = useRouter();
const queryClient = useQueryClient();
const { setOpen, setClose } = useModal();

Expand All @@ -32,15 +33,18 @@ const ComplaintFileActions: React.FC<ComplaintFileActionsProps> = ({
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
Expand All @@ -62,7 +66,7 @@ const ComplaintFileActions: React.FC<ComplaintFileActionsProps> = ({
onConfirm={() => {
updateComplaintStatus({
id: complaintData?.id ?? 0,
caseFileStatus: { status: "CLOSED" },
complaintStatus: { status: "CLOSED" },
});
}}
/>
Expand All @@ -84,7 +88,7 @@ const ComplaintFileActions: React.FC<ComplaintFileActionsProps> = ({
onConfirm={() => {
updateComplaintStatus({
id: complaintData?.id ?? 0,
caseFileStatus: { status: "OPEN" },
complaintStatus: { status: "OPEN" },
});
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +20,7 @@ const InspectionFileActions: React.FC<InspectionFileActionsProps> = ({
status,
fileNumber,
}) => {
const router = useRouter();
const queryClient = useQueryClient();
const { setOpen, setClose } = useModal();

Expand All @@ -28,14 +33,25 @@ const InspectionFileActions: React.FC<InspectionFileActionsProps> = ({
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",
Expand All @@ -48,14 +64,16 @@ const InspectionFileActions: React.FC<InspectionFileActionsProps> = ({
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",
Expand All @@ -68,34 +86,17 @@ const InspectionFileActions: React.FC<InspectionFileActionsProps> = ({
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: (
<ConfirmationModal
title="Reopen Inspection?"
description="Are you sure you want to reopen this inspection?"
confirmButtonText="Reopen Inspection"
onConfirm={() => {
// TODO: Implement reopen inspection
setClose();
}}
/>
),
});
},
hidden: status?.toLowerCase() === "open",
hidden: ["canceled", "closed"].includes(status?.toLowerCase()),
},
{
text: "Delete Inspection",
Expand All @@ -108,8 +109,7 @@ const InspectionFileActions: React.FC<InspectionFileActionsProps> = ({
description="You are about to delete this inspection. Are you sure?"
confirmButtonText="Delete"
onConfirm={() => {
// TODO: Implement delete inspection
setClose();
deleteInspection(inspectionData?.id ?? 0);
}}
/>
),
Expand Down
16 changes: 12 additions & 4 deletions compliance-web/src/hooks/useComplaints.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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) => {
Expand Down
29 changes: 24 additions & 5 deletions compliance-web/src/hooks/useInspections.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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 });
};
4 changes: 4 additions & 0 deletions compliance-web/src/models/Inspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ export interface InspectionAPIData {
unapproved_project_type?: string;
unapproved_project_sub_type?: string;
}

export interface InspectionStatusAPIData {
status: string;
}