Skip to content

Commit

Permalink
회원 닉네임 수정, 회원 탈퇴 react-query를 이용한 커스텀훅 구현 (#192)
Browse files Browse the repository at this point in the history
* feat: (#187) 유저가 닉네임을 변경하는 커스텀 훅 구현

* feat: (#187) 유저 회원 탈퇴 기능 커스텀 훅 구현

* feat: (#187) 닉네임 변경 부분 낙관적 업데이트 적용

* refactor: (#187) 닉네임 변경에 실패했을 경우 console.error 코드 추가

* refactor: (#187) 회원 탈퇴를 cancel에서 withdrawal로 변경

* refactor: (#187) dev 브런치 머지 후 파일 경로 재설정
  • Loading branch information
Gilpop8663 authored and chsua committed Aug 11, 2023
1 parent b6c0525 commit 4b6b227
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 3 deletions.
4 changes: 2 additions & 2 deletions frontend/__test__/api/user.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
cancelMembership,
withdrawalMembership,
getUserInfo,
modifyNickname,
transformUserInfoResponse,
Expand Down Expand Up @@ -29,7 +29,7 @@ describe('서버와 통신하여 유저의 정보를 불러올 수 있어야 한
});

test('유저가 회원 탈퇴를 한다', async () => {
await cancelMembership();
await withdrawalMembership();

expect(MOCK_USER_INFO.nickname).toBe('cancel');
});
Expand Down
30 changes: 30 additions & 0 deletions frontend/__test__/hooks/query/useCancelMembership.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { ReactNode } from 'react';

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook, waitFor } from '@testing-library/react';

import { useWithdrawalMembership } from '@hooks/query/user/useWithdrawalMembership';

import { MOCK_USER_INFO } from '@mocks/mockData/user';

const queryClient = new QueryClient();

const wrapper = ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

describe('useWithdrawalMembership 훅이 회원 탈퇴를 하는지 확인한다', () => {
test('유저가 회원 탈퇴를 한다', async () => {
const { result } = renderHook(() => useWithdrawalMembership(), {
wrapper,
});

const { mutate } = result.current;

await waitFor(async () => {
mutate();

expect(MOCK_USER_INFO.nickname).toBe('cancel');
});
});
});
30 changes: 30 additions & 0 deletions frontend/__test__/hooks/query/useModifyUser.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { ReactNode } from 'react';

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook, waitFor } from '@testing-library/react';

import { useModifyUser } from '@hooks/query/user/useModifyUser';

import { MOCK_USER_INFO } from '@mocks/mockData/user';

const queryClient = new QueryClient();

const wrapper = ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);

describe('useModifyUser 훅이 닉네임을 변경하는 지 확인한다', () => {
test('유저가 닉네임을 변경한다', async () => {
const { result } = renderHook(() => useModifyUser(), {
wrapper,
});

const { mutate } = result.current;

await waitFor(async () => {
mutate('wood');

expect(MOCK_USER_INFO.nickname).toBe('wood');
});
});
});
2 changes: 1 addition & 1 deletion frontend/src/api/userInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ export const modifyNickname = async (nickname: string) => {
await patchFetch<ModifyNicknameRequest>(`${BASE_URL}/members/me/nickname`, { nickname });
};

export const cancelMembership = async () => {
export const withdrawalMembership = async () => {
await deleteFetch(`${BASE_URL}/members/me/delete`);
};
28 changes: 28 additions & 0 deletions frontend/src/hooks/query/user/useModifyUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';

import { modifyNickname } from '@api/userInfo';

import { QUERY_KEY } from '@constants/queryKey';

export const useModifyUser = () => {
const queryClient = useQueryClient();
const { mutate, isLoading } = useMutation({
mutationFn: (nickname: string) => modifyNickname(nickname),
onMutate: async newNickname => {
await queryClient.cancelQueries({ queryKey: [QUERY_KEY.USER_INFO] });
const previousNickname = queryClient.getQueryData([QUERY_KEY.USER_INFO]);
queryClient.setQueryData([QUERY_KEY.USER_INFO], newNickname);

return { previousNickname, newNickname };
},
onError: (error, __, context) => {
queryClient.setQueryData([QUERY_KEY.USER_INFO], context?.previousNickname);
window.console.error('닉네임 변경에 실패했습니다.');
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: [QUERY_KEY.USER_INFO] });
},
});

return { mutate, isLoading };
};
20 changes: 20 additions & 0 deletions frontend/src/hooks/query/user/useWithdrawalMembership.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';

import { withdrawalMembership } from '@api/userInfo';

import { QUERY_KEY } from '@constants/queryKey';

export const useWithdrawalMembership = () => {
const queryClient = useQueryClient();
const { mutate, isLoading } = useMutation({
mutationFn: () => withdrawalMembership(),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [QUERY_KEY.USER_INFO] });
},
onError: () => {
window.console.error('회원 탈퇴에 실패했습니다.');
},
});

return { mutate, isLoading };
};

0 comments on commit 4b6b227

Please sign in to comment.