260303_01_ENH: TanStack Query 적용

This commit is contained in:
2026-03-03 16:07:41 +09:00
parent 4b22120f45
commit d8e894b136
29 changed files with 1631 additions and 1143 deletions

54
src/query/auth.ts Normal file
View File

@@ -0,0 +1,54 @@
"use client";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { nextApi } from "@/lib/http-client";
import { useAuth } from "@/hooks/useAuth";
import { useSelectedProject } from "@/hooks/useSelectedProject";
import { useStorage } from "@/hooks/useStorage";
import { StorageKeys } from "@/store/storage/storage-keys";
import type { LoginUser } from "@/types/auth";
export const useLoginMutation = () => {
const { setFromLoginResponse } = useAuth();
const { set: setSeat } = useStorage(StorageKeys.SeatNumber);
const { set: setLoginType } = useStorage(StorageKeys.LoginType);
return useMutation({
mutationFn: async (v: { userId: string; userPw: string; seatNum: string; loginType: string }) => {
return nextApi.post<LoginUser>("/login", { userId: v.userId, userPw: v.userPw }, { autoLogoutOnAuthError: false });
},
onSuccess: (data, v) => {
try {
setSeat(v.seatNum);
setLoginType(v.loginType);
} catch {}
setFromLoginResponse(data, v.seatNum);
},
});
}
export const useLogoutMutation = () => {
const qc = useQueryClient();
const { auth, logout } = useAuth();
const { clearSelectedProject } = useSelectedProject();
const { get: getLoginType } = useStorage(StorageKeys.LoginType);
return useMutation({
mutationFn: async () => {
const userId = auth.user?.userId ?? "";
try {
if (userId){
await nextApi.post(`/logout/${userId}`, null, { autoLogoutOnAuthError: false });
}
} catch (e) {
console.error(e);
}
return { loginType: getLoginType() };
},
onSettled: () => {
clearSelectedProject();
logout();
qc.clear(); // 다음 사용자/세션에 서버상태 남지 않게
},
});
}