Skip to content

Commit 5e97b40

Browse files
authored
[Comp-792-2] Invalidate cached queries on frontend (#714)
1 parent ba90890 commit 5e97b40

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

compliance-web/src/components/App/Staff/StaffModal.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { Position } from "@/models/Position";
1414
import { StaffAPIData, StaffFormData, StaffUser } from "@/models/Staff";
1515
import { yupResolver } from "@hookform/resolvers/yup";
1616
import { DialogContent } from "@mui/material";
17-
import { useQueryClient } from "@tanstack/react-query";
1817
import { useEffect, useMemo } from "react";
1918
import { FormProvider, useForm } from "react-hook-form";
2019
import * as yup from "yup";
@@ -49,7 +48,6 @@ const initFormData: StaffFormData = {
4948
};
5049

5150
const StaffModal: React.FC<StaffModalProps> = ({ onSubmit, staff }) => {
52-
const queryClient = useQueryClient();
5351

5452
const { data: usersList } = useAuthUsersData();
5553
const { data: positionsList } = usePositionsData();
@@ -117,9 +115,6 @@ const StaffModal: React.FC<StaffModalProps> = ({ onSubmit, staff }) => {
117115
} else {
118116
addStaff(staffData);
119117
}
120-
queryClient.invalidateQueries({
121-
queryKey: ["staff-users"],
122-
});
123118
};
124119

125120
return (

compliance-web/src/hooks/useStaff.tsx

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Permission } from "@/models/Permission";
33
import { Position } from "@/models/Position";
44
import { StaffAPIData, StaffUser } from "@/models/Staff";
55
import { OnSuccessType, request, requestAuthAPI } from "@/utils/axiosUtils";
6-
import { useMutation, useQuery } from "@tanstack/react-query";
6+
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
77
import { useStaticQuery } from "@/hooks/useCustomQueries";
88
import { STAFF_USER_POSITION } from "@/utils/constants";
99

@@ -80,14 +80,65 @@ export const usePermissionsData = () => {
8080
});
8181
};
8282

83-
export const useAddStaff = (onSuccess: OnSuccessType) => {
84-
return useMutation({ mutationFn: addStaff, onSuccess });
85-
};
83+
export const useAddStaff = (onSuccess?: OnSuccessType) => {
84+
const queryClient = useQueryClient();
85+
86+
return useMutation({
87+
mutationFn: addStaff,
88+
onSuccess: async (data) => {
89+
// Invalidate all staff-users queries
90+
await queryClient.invalidateQueries({
91+
queryKey: ["staff-users"],
92+
refetchType: 'active'
93+
});
8694

87-
export const useUpdateStaff = (onSuccess: OnSuccessType) => {
88-
return useMutation({ mutationFn: updateStaff, onSuccess });
95+
// Call the provided callback if it exists
96+
if (onSuccess) {
97+
onSuccess(data);
98+
}
99+
},
100+
});
89101
};
90102

91-
export const useDeleteStaff = (onSuccess: OnSuccessType) => {
92-
return useMutation({ mutationFn: deleteStaff, onSuccess });
103+
export const useUpdateStaff = (onSuccess?: OnSuccessType) => {
104+
const queryClient = useQueryClient();
105+
106+
return useMutation({
107+
mutationFn: updateStaff,
108+
onSuccess: async (data) => {
109+
// Invalidate all staff-users queries
110+
await queryClient.invalidateQueries({
111+
queryKey: ["staff-users"],
112+
refetchType: 'active'
113+
});
114+
115+
// Call the provided callback if it exists
116+
if (onSuccess) {
117+
onSuccess(data);
118+
}
119+
},
120+
});
93121
};
122+
123+
export const useDeleteStaff = (onSuccess?: OnSuccessType) => {
124+
const queryClient = useQueryClient();
125+
126+
return useMutation({
127+
mutationFn: deleteStaff,
128+
onSuccess: async (data, ) => {
129+
// Invalidate all staff-users queries
130+
await queryClient.invalidateQueries({
131+
queryKey: ["staff-users"],
132+
refetchType: 'active'
133+
});
134+
135+
// Invalidate validation
136+
queryClient.invalidateQueries({ queryKey: ["staff-user-validation"] });
137+
138+
// Call the provided callback if it exists
139+
if (onSuccess) {
140+
onSuccess(data,);
141+
}
142+
},
143+
});
144+
};

compliance-web/src/routes/_authenticated/admin/staff.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { useModal } from "@/store/modalStore";
99
import { notify } from "@/store/snackbarStore";
1010
import { DeleteOutlineRounded, EditOutlined } from "@mui/icons-material";
1111
import { Box, Chip, IconButton } from "@mui/material";
12-
import { useQueryClient } from "@tanstack/react-query";
1312
import { createFileRoute } from "@tanstack/react-router";
1413
import { BCDesignTokens } from "epic.theme";
1514
import { MRT_ColumnDef } from "material-react-table";
@@ -35,7 +34,6 @@ function AuthorizedStaffComponent() {
3534
const STAFF_MODAL_WIDTH = "520px";
3635

3736
export function Staff() {
38-
const queryClient = useQueryClient();
3937
const { setOpen, setClose } = useModal();
4038
const { data: staffUsersList, isLoading } = useStaffUsersData({
4139
isActive: false,
@@ -77,7 +75,6 @@ export function Staff() {
7775
}, [staffUsersList]);
7876

7977
const handleOnSubmit = (submitMsg: string) => {
80-
queryClient.invalidateQueries({ queryKey: ["staff-users"] });
8178
setClose();
8279
notify.success(submitMsg);
8380
};
@@ -99,8 +96,6 @@ export function Staff() {
9996
/** Staff Deletion START */
10097

10198
const onDeleteSuccess = () => {
102-
queryClient.invalidateQueries({ queryKey: ["staff-users"] });
103-
queryClient.invalidateQueries({ queryKey: ["staff-user-validation"] });
10499
setClose();
105100
notify.success("Staff deleted successfully!");
106101
};

0 commit comments

Comments
 (0)