From 0c84f4f020dbd5d2ad93e92627250629b760d47a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=ED=98=84=EC=A1=B0?= Date: Wed, 18 Sep 2024 16:56:08 +0900 Subject: [PATCH] =?UTF-8?q?Refactor:=20=ED=9B=85=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/hooks/useAuthHooks.ts | 28 +++-------- frontend/src/hooks/useProjectHooks.ts | 59 ++++++++++++++++++++++ frontend/src/hooks/useWorkspaceHooks.ts | 66 +++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 20 deletions(-) diff --git a/frontend/src/hooks/useAuthHooks.ts b/frontend/src/hooks/useAuthHooks.ts index cf5d40f..1d18390 100644 --- a/frontend/src/hooks/useAuthHooks.ts +++ b/frontend/src/hooks/useAuthHooks.ts @@ -1,9 +1,8 @@ import { useMutation, useQueryClient } from '@tanstack/react-query'; -import { AxiosError } from 'axios'; import useAuthStore from '@/stores/useAuthStore'; -import { reissueToken, getProfile } from '@/api/authApi'; -import { CustomError } from '@/types'; -import { useState, useEffect } from 'react'; +import { reissueToken } from '@/api/authApi'; +import { useEffect } from 'react'; + import useProfileQuery from '@/queries/useProfileQuery'; export const useReissueToken = () => { @@ -32,20 +31,9 @@ export const useProfile = () => { }; export const useFetchProfile = () => { - const { profile, setProfile } = useAuthStore(); - const [isFetched, setIsFetched] = useState(false); - - useEffect(() => { - if (!profile && !isFetched) { - getProfile() - .then((data) => { - setProfile(data); - setIsFetched(true); - }) - .catch((error: AxiosError) => { - alert('프로필을 가져오는 중 오류가 발생했습니다. 다시 시도해주세요.'); - console.error('프로필 가져오기 실패:', error?.response?.data?.message || '알 수 없는 오류'); - }); - } - }, [profile, setProfile, isFetched]); + const { setProfile } = useAuthStore(); + const query = useProfileQuery(); + if (query.data) { + setProfile(query.data); + } }; diff --git a/frontend/src/hooks/useProjectHooks.ts b/frontend/src/hooks/useProjectHooks.ts index 77118a0..84ed741 100644 --- a/frontend/src/hooks/useProjectHooks.ts +++ b/frontend/src/hooks/useProjectHooks.ts @@ -112,3 +112,62 @@ // }, // }); // }; +import { useMutation, useQueryClient } from '@tanstack/react-query'; +import { createProject, updateProject, deleteProject, addProjectMember, removeProjectMember } from '@/api/projectApi'; +import { ProjectResponse, ProjectRequest, ProjectMemberRequest } from '@/types'; + +export const useCreateProject = () => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: ({ workspaceId, memberId, data }) => createProject(workspaceId, memberId, data), + onSuccess: (_, variables) => { + queryClient.invalidateQueries({ queryKey: ['projects', variables.workspaceId] }); + }, + }); +}; + +export const useUpdateProject = () => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: ({ projectId, memberId, data }) => updateProject(projectId, memberId, data), + onSuccess: (data) => { + queryClient.invalidateQueries({ queryKey: ['project', data.id] }); + }, + }); +}; + +export const useDeleteProject = () => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: ({ projectId, memberId }) => deleteProject(projectId, memberId), + onSuccess: (_, variables) => { + queryClient.invalidateQueries({ queryKey: ['projects', variables.projectId] }); + }, + }); +}; + +export const useAddProjectMember = () => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: ({ projectId, memberId, data }) => + addProjectMember(projectId, memberId, data.memberId, data.privilegeType), + onSuccess: (_, variables) => { + queryClient.invalidateQueries({ queryKey: ['project', variables.projectId] }); + }, + }); +}; + +export const useRemoveProjectMember = () => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: ({ projectId, memberId, targetMemberId }) => removeProjectMember(projectId, memberId, targetMemberId), + onSuccess: (_, variables) => { + queryClient.invalidateQueries({ queryKey: ['project', variables.projectId] }); + }, + }); +}; diff --git a/frontend/src/hooks/useWorkspaceHooks.ts b/frontend/src/hooks/useWorkspaceHooks.ts index 03e33f7..992031c 100644 --- a/frontend/src/hooks/useWorkspaceHooks.ts +++ b/frontend/src/hooks/useWorkspaceHooks.ts @@ -87,3 +87,69 @@ // }, // }); // }; + +import { useMutation, useQueryClient } from '@tanstack/react-query'; +import { + createWorkspace, + updateWorkspace, + deleteWorkspace, + addWorkspaceMember, + removeWorkspaceMember, +} from '@/api/workspaceApi'; +import { WorkspaceResponse, WorkspaceRequest } from '@/types'; + +export const useCreateWorkspace = () => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: ({ memberId, data }) => createWorkspace(memberId, data), + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ['workspaceList'] }); + }, + }); +}; + +export const useUpdateWorkspace = () => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: ({ workspaceId, memberId, data }) => updateWorkspace(workspaceId, memberId, data), + onSuccess: (_, variables) => { + queryClient.invalidateQueries({ queryKey: ['workspace', variables.workspaceId] }); + }, + }); +}; + +export const useDeleteWorkspace = () => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: ({ workspaceId, memberId }) => deleteWorkspace(workspaceId, memberId), + onSuccess: (_, variables) => { + queryClient.invalidateQueries({ queryKey: ['workspace', variables.workspaceId] }); + }, + }); +}; + +export const useAddWorkspaceMember = () => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: ({ workspaceId, memberId, newMemberId }) => addWorkspaceMember(workspaceId, memberId, newMemberId), + onSuccess: (_, variables) => { + queryClient.invalidateQueries({ queryKey: ['workspace', variables.workspaceId] }); + }, + }); +}; + +export const useRemoveWorkspaceMember = () => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: ({ workspaceId, memberId, targetMemberId }) => + removeWorkspaceMember(workspaceId, memberId, targetMemberId), + onSuccess: (_, variables) => { + queryClient.invalidateQueries({ queryKey: ['workspace', variables.workspaceId] }); + }, + }); +};