Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 1x 20x 20x 20x 20x 1x 20x 20x 20x 20x 20x | import { showMessage } from "@common/Toast";
import { customInstance } from "@services/api";
import { useMutation, useQueryClient } from "react-query";
import { DISPUTES_QUERY_KEY } from "./useDisputesTable";
import { MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS } from "@features/Merchants/MerchantSidePanel/constants";
export const useUpdateDisputeAssignee = (refetchDisputePreview?: boolean) => {
const { updateData } = useUpdateDisputesList();
const updateAssignee = useMutation(({ disputeID, userID, isDelete }: any) => {
return customInstance({
url: `/disputes/${disputeID}/assignee/${userID}`,
method: isDelete ? "DELETE" : "PUT",
});
});
const handleChangeAssignee = (
oldAssignee = 0,
newAssignee: string | null,
disputeID: string,
onSuccess?: () => void,
) => {
const isDelete = newAssignee === null;
const successCharMsg =
oldAssignee && newAssignee
? "changed"
: newAssignee
? "added"
: "removed";
updateAssignee.mutate(
{ userID: isDelete ? oldAssignee : newAssignee, disputeID, isDelete },
{
onError: (error: unknown) => {
const axiosError = error as any;
const errorMessage = axiosError?.response?.data?.message;
showMessage("Error", errorMessage || "Unable to change assignee");
},
onSuccess: (data) => {
if (onSuccess) onSuccess();
showMessage("Success", `Assignee has been ${successCharMsg}`);
updateData(
{
accID: data?.accID,
avatarURL: data?.avatarURL,
email: data?.email,
firstName: data?.firstName,
lastName: data?.lastName,
},
refetchDisputePreview,
);
},
},
);
};
return { handleChangeAssignee };
};
type TKeyValuePairs = {
[key: string]: any;
};
const useUpdateDisputesList = (queryKey?: string) => {
const queryClient = useQueryClient();
const previousData = queryClient.getQueriesData(queryKey as string);
const updateData = (
keyValuePairs: TKeyValuePairs,
refetchDisputePreview?: boolean,
) => {
const updateHandler = (oldData: any) => {
if (!oldData?.data || !Array.isArray(oldData.data)) return oldData;
const updatedData = oldData.data.map((merchant: any) => {
// Iterate over each key-value pair and update the merchant
const updatedMerchant = Object.entries(keyValuePairs).reduce(
(acc, [key, value]) => {
acc[key] = value;
return acc;
},
{ ...merchant },
);
return updatedMerchant;
});
return {
...oldData,
data: updatedData,
};
};
const updateMerchantPreview = (oldData: any) => {
if (!oldData?.merchant) return oldData;
const updatedMerchant = Object.entries(keyValuePairs).reduce(
(acc, [key, value]) => {
acc[key] = value;
return acc;
},
{ ...oldData.merchant },
);
return {
...oldData,
merchant: updatedMerchant,
};
};
queryClient.setQueriesData(DISPUTES_QUERY_KEY, updateHandler);
if (refetchDisputePreview) {
queryClient.setQueriesData(
MERCHANT_SIDE_PANEL_PREVIEW_API_KEYS.GET,
updateMerchantPreview,
);
}
};
const revertChange = () => {
previousData.forEach(([queryKey, queryData]) =>
queryClient.setQueriesData(queryKey as string, queryData),
);
};
return { updateData, revertChange };
};
|