Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 회원탈퇴 후 유저정보가 남아있는 버그 수정 #208

Merged
merged 6 commits into from
Jun 22, 2024
23 changes: 5 additions & 18 deletions src/home/apis/postWithdraw.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import { isAxiosError } from 'axios';

import { authClient } from '@/apis';
import { PostWithdrawResponse, AuthErrorData } from '@/home/types/Auth.type';
import { api } from '@/service/TokenService';

export const postWithdraw = async (): Promise<PostWithdrawResponse> => {
try {
await authClient.post(
'/auth/withdraw',
{},
{
headers: api.headers,
}
);
api.logout();
return { success: true };
} catch (error) {
if (isAxiosError<AuthErrorData>(error)) return { success: false, error };
return Promise.reject(error);
}
export const postWithdraw = async (data: object) => {
const res = await authClient.post('/auth/withdraw', data, {
headers: api.headers,
});
return res.data;
};
110 changes: 58 additions & 52 deletions src/home/pages/Withdraw/Withdraw.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useState } from 'react';

import { BoxButton, CheckBox } from '@yourssu/design-system-react';
import { ErrorBoundary } from 'react-error-boundary';
import { useNavigate } from 'react-router-dom';

import Logo from '@/assets/soomsil_logo.svg';
import { postWithdraw } from '@/home/apis/postWithdraw';
import { Fallback } from '@/components/Fallback/Fallback';
import { WithdrawSuccess } from '@/home/components/WithdrawSuccess/WithdrawSuccess';
import { usePostWithdraw } from '@/hooks/usePostWithdraw';

import {
StyledButtonText,
Expand All @@ -22,6 +24,7 @@ export const Withdraw = () => {
const navigate = useNavigate();
const [agreed, setAgreed] = useState(false);
const [draw, setDraw] = useState(false);
const withdrawMutation = usePostWithdraw();

const handleGoToHome = () => {
navigate('/');
Expand All @@ -31,60 +34,63 @@ export const Withdraw = () => {
setAgreed((prevAgreed) => !prevAgreed);
};

const handleWithdrawAgree = async () => {
try {
const { success, error } = await postWithdraw();
if (success) {
setDraw((prevDraw) => !prevDraw);
} else if (error) {
alert(`탈퇴 처리에 실패했습니다: ${error.message}`);
const handleWithdrawAgree = () => {
withdrawMutation.mutate(
{},
{
onSuccess: () => {
setDraw((prevDraw) => !prevDraw);
},
}
} catch (error) {
if (error instanceof Error) {
alert(`탈퇴 처리 중 오류가 발생했습니다: ${error.message}`);
} else {
alert(`알 수 없는 오류가 발생했습니다`);
}
}
);
};

return (
<StyledWrapper>
<img onClick={handleGoToHome} src={Logo} alt="Logo-image" width={180} height={38} />
<StyledWithdrawContainer>
{draw ? (
<WithdrawSuccess onConfirm={handleGoToHome} />
) : (
<>
<StyledTitleText>계정 탈퇴</StyledTitleText>
<StyledSubTitleText>계정 탈퇴 전 꼭 확인하세요.</StyledSubTitleText>
<StyledText>
<StyledListItem>탈퇴하는 즉시 데이터가 제거되며, 복구가 불가능합니다.</StyledListItem>
<StyledListItem>
닉네임, 이메일 등 사용자를 특정할 수 있는 모든 계정 정보가 지워집니다.
</StyledListItem>
<StyledListItem>
등록된 글이나 댓글의 삭제를 원한다면 탈퇴 이전에 삭제하시기 바랍니다.
</StyledListItem>
</StyledText>
<StyledLeft>
<CheckBox size="medium" isSelected={agreed} onClick={handleCheckAgree} type="button">
<StyledButtonText>안내사항을 확인하였으며, 이에 동의합니다.</StyledButtonText>
</CheckBox>
</StyledLeft>
<BoxButton
style={{ width: '100%' }}
rounding={8}
size="large"
variant="filled"
onClick={handleWithdrawAgree}
disabled={!agreed}
>
탈퇴하기
</BoxButton>
</>
)}
</StyledWithdrawContainer>
</StyledWrapper>
<ErrorBoundary FallbackComponent={Fallback}>
<StyledWrapper>
<img onClick={handleGoToHome} src={Logo} alt="Logo-image" width={180} height={38} />
<StyledWithdrawContainer>
{draw ? (
<WithdrawSuccess onConfirm={handleGoToHome} />
) : (
<>
<StyledTitleText>계정 탈퇴</StyledTitleText>
<StyledSubTitleText>계정 탈퇴 전 꼭 확인하세요.</StyledSubTitleText>
<StyledText>
<StyledListItem>
탈퇴하는 즉시 데이터가 제거되며, 복구가 불가능합니다.
</StyledListItem>
<StyledListItem>
닉네임, 이메일 등 사용자를 특정할 수 있는 모든 계정 정보가 지워집니다.
</StyledListItem>
<StyledListItem>
등록된 글이나 댓글의 삭제를 원한다면 탈퇴 이전에 삭제하시기 바랍니다.
</StyledListItem>
</StyledText>
<StyledLeft>
<CheckBox
size="medium"
isSelected={agreed}
onClick={handleCheckAgree}
type="button"
>
<StyledButtonText>안내사항을 확인하였으며, 이에 동의합니다.</StyledButtonText>
</CheckBox>
</StyledLeft>
<BoxButton
style={{ width: '100%' }}
rounding={8}
size="large"
variant="filled"
onClick={handleWithdrawAgree}
disabled={!agreed || withdrawMutation.isPending}
>
탈퇴하기
</BoxButton>
</>
)}
</StyledWithdrawContainer>
</StyledWrapper>
</ErrorBoundary>
);
};
17 changes: 17 additions & 0 deletions src/hooks/usePostWithdraw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useMutation } from '@tanstack/react-query';

import { postWithdraw } from '@/home/apis/postWithdraw';

import { useResetUserInfo } from './useResetUserInfo';

export const usePostWithdraw = () => {
const resetUserInfo = useResetUserInfo();

return useMutation({
mutationFn: postWithdraw,
onSuccess: () => {
resetUserInfo();
},
throwOnError: true,
});
};
13 changes: 13 additions & 0 deletions src/hooks/useResetUserInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useSetRecoilState } from 'recoil';

import { LogInState } from '@/home/recoil/LogInState';
import { api } from '@/service/TokenService';

export const useResetUserInfo = () => {
const resetLoginState = useSetRecoilState(LogInState);

return () => {
resetLoginState(false);
api.logout();
};
};