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 | 67x 67x 20x 20x 20x 20x 20x 20x | import { showMessage } from "@common/Toast";
import { useQueryClient } from "react-query";
type TUpdateHandler = (oldData: any) => any;
const defaultErrorHandler = (
message = "Whoops.. an error occured. Please try again",
) => showMessage("Error", message);
export const useUpdateCachedQuery = (queryKey: string) => {
const queryClient = useQueryClient();
const previousData = queryClient.getQueriesData(queryKey);
const updateData = (updateHandler: TUpdateHandler) => {
queryClient.setQueriesData(queryKey, updateHandler);
};
const revertChange = () => {
previousData.forEach(([queryKey, queryData]) =>
queryClient.setQueriesData(queryKey, queryData),
);
};
const updateDataWithExceptions = async (
updateHandler: TUpdateHandler,
execute: () => void,
onError = defaultErrorHandler,
) => {
try {
await queryClient.cancelQueries(queryKey);
updateData(updateHandler);
execute();
} catch (error: any) {
const message = error?.message || error?.response?.message;
revertChange();
onError(message);
}
};
return { updateData, revertChange, updateDataWithExceptions };
};
|