refactor: useMutationWrapper 생성 및 적용

This commit is contained in:
2025-06-09 15:17:32 +09:00
parent 0df708f849
commit 33f6ce25f4
61 changed files with 236 additions and 211 deletions
+1
View File
@@ -1,4 +1,5 @@
export { getCookie, setCookie, removeCookie } from "./cookie";
export { useMutationWrapper } from "./use-mutation-wrapper";
export { useAlertStore } from "./use-alert-store";
export { useMenuStore } from "./use-menu-store";
export { useUserStore } from "./use-user-store";
@@ -0,0 +1,30 @@
import { AxiosError } from "axios";
import { useMutation, type UseMutationOptions } from "@tanstack/react-query";
import { useAlertStore } from "./use-alert-store";
export const useMutationWrapper = <
TData = unknown,
TError = unknown,
TVariables = void,
TContext = unknown,
>(
mutationOptions: UseMutationOptions<TData, TError, TVariables, TContext>,
) => {
const alert = useAlertStore((state) => state.setMessage);
return useMutation({
...mutationOptions,
onError: (error) => {
let errorMessage = "";
if (error instanceof AxiosError) {
if (error.response?.status) errorMessage += `status: ${error.status}\n`;
if (error.response?.data.message)
errorMessage += `${error.response?.data.message}\n`;
if (error.response?.data.errors[0].message)
errorMessage += `${error.response?.data.errors[0].message}\n`;
}
alert(`[Error] ${errorMessage}`);
},
});
};