diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 4cdd3c81..0259284a 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -5,14 +5,13 @@ import { ReactQueryDevtools } from '@tanstack/react-query-devtools' import { GlobalReportPoller } from './components/GlobalReportPoller' import { queryClient } from './utils/queryClient' import GlobalModal from './components/GlobalModal' -import { GoogleAnalytics } from './components/GoogleAnalytics' function App() { return ( <> - + {/* 전역 폴러 */} @@ -23,6 +22,3 @@ function App() { } export default App - - - diff --git a/frontend/src/api/user.ts b/frontend/src/api/user.ts index 11fcd9f3..f18c5592 100644 --- a/frontend/src/api/user.ts +++ b/frontend/src/api/user.ts @@ -33,3 +33,8 @@ export const fetchMyProfile = async () => { const { data } = await axiosInstance.get('/members') return data } + +export const withdrawMember = async () => { + const { data } = await axiosInstance.delete('/members/withdraw') + return data +} diff --git a/frontend/src/hooks/setting/useWithdrawSettings.ts b/frontend/src/hooks/setting/useWithdrawSettings.ts new file mode 100644 index 00000000..b1084c2d --- /dev/null +++ b/frontend/src/hooks/setting/useWithdrawSettings.ts @@ -0,0 +1,32 @@ +import { useQueryClient } from '@tanstack/react-query' +import { useNavigate } from 'react-router-dom' +import { useAuthStore } from '../../stores/authStore' +import { useWithdrawMember } from './userMutations' + +export function useWithdrawSettings(onSuccessAction?: () => void) { + const navigate = useNavigate() + const queryClient = useQueryClient() + const clearAuth = useAuthStore((state) => state.actions.clearAuth) + const { mutate: withdraw, isPending } = useWithdrawMember() + + const confirmWithdraw = () => { + if (isPending) return + + withdraw(undefined, { + onSuccess: () => { + queryClient.clear() + clearAuth() + onSuccessAction?.() + navigate('/') + }, + onError: () => { + alert('회원 탈퇴에 실패했습니다.') + }, + }) + } + + return { + confirmWithdraw, + isPending, + } +} diff --git a/frontend/src/hooks/setting/userMutations.ts b/frontend/src/hooks/setting/userMutations.ts index adeb0ecd..291c0e79 100644 --- a/frontend/src/hooks/setting/userMutations.ts +++ b/frontend/src/hooks/setting/userMutations.ts @@ -1,5 +1,5 @@ import { useMutation } from '@tanstack/react-query' -import { updateMemberAgree, updateMemberProfileImage, updateMemberSNS } from '../../api/user' +import { updateMemberAgree, updateMemberProfileImage, updateMemberSNS, withdrawMember } from '../../api/user' export const useUpdateMemberAgree = () => { return useMutation({ @@ -18,3 +18,9 @@ export const useUpdateMemberProfileImage = () => { mutationFn: updateMemberProfileImage, }) } + +export const useWithdrawMember = () => { + return useMutation({ + mutationFn: withdrawMember, + }) +} diff --git a/frontend/src/pages/setting/SettingPage.tsx b/frontend/src/pages/setting/SettingPage.tsx index 3f1673a5..f7026aef 100644 --- a/frontend/src/pages/setting/SettingPage.tsx +++ b/frontend/src/pages/setting/SettingPage.tsx @@ -3,22 +3,27 @@ import { Button } from './_components/SettingButton' import '../../styles/scrollbar.css' import CloseIcon from '../../assets/icons/delete_normal.svg?react' import LogoutIcon from '../../assets/icons/logout.svg?react' -import WithdrawlModal from './_components/WithdrawlModal' +import WithdrawModal from './_components/WithdrawModal' import { useLogout } from '../../hooks/useLogout' import ProfileSettings from './_containers/ProfileSettings' import ConsentSettings from './_containers/ConsentSettings' +import { useWithdrawSettings } from '../../hooks/setting/useWithdrawSettings' +import { useAuthStore } from '../../stores/authStore' type SettingPageProps = { onClose?: () => void } export default function SettingPage({ onClose }: SettingPageProps) { + const isLoggedIn = useAuthStore((state) => !!state.isAuth) const [activeTab, setActiveTab] = useState<'profile' | 'consent'>('profile') - const [showWithdrawlModal, setShowWithdrawlModal] = useState(false) + const [showWithdrawModal, setShowWithdrawModal] = useState(false) const logout = useLogout() const [loggingOut, setLoggingOut] = useState(false) + const { confirmWithdraw, isPending } = useWithdrawSettings(() => onClose?.()) + const handleClickLogout = async () => { if (loggingOut) return setLoggingOut(true) @@ -27,10 +32,6 @@ export default function SettingPage({ onClose }: SettingPageProps) { onClose?.() } - const handleWithdrawlConfirm = () => { - setShowWithdrawlModal(false) - } - return (
{activeTab === 'profile' && ( setShowWithdrawlModal(true)} + onOpenWithdraw={() => setShowWithdrawModal(true)} fetchEnabled={!loggingOut} /> )} @@ -88,8 +89,12 @@ export default function SettingPage({ onClose }: SettingPageProps) {
- {showWithdrawlModal && ( - setShowWithdrawlModal(false)} onConfirm={handleWithdrawlConfirm} /> + {isLoggedIn && showWithdrawModal && ( + {} : () => setShowWithdrawModal(false)} + onConfirm={confirmWithdraw} + isPending={isPending} + /> )} ) diff --git a/frontend/src/pages/setting/_components/WithdrawlModal.tsx b/frontend/src/pages/setting/_components/WithdrawModal.tsx similarity index 83% rename from frontend/src/pages/setting/_components/WithdrawlModal.tsx rename to frontend/src/pages/setting/_components/WithdrawModal.tsx index 53ad0dc3..1904a1b7 100644 --- a/frontend/src/pages/setting/_components/WithdrawlModal.tsx +++ b/frontend/src/pages/setting/_components/WithdrawModal.tsx @@ -1,12 +1,13 @@ import Modal from '../../../components/Modal' import { Button } from './SettingButton' -type WithdrawlModalProps = { +type WithdrawModalProps = { onClose: () => void onConfirm: () => void + isPending: boolean } -export default function WithdrawlModal({ onClose, onConfirm }: WithdrawlModalProps) { +export default function WithdrawModal({ onClose, onConfirm, isPending }: WithdrawModalProps) { return ( @@ -25,6 +27,7 @@ export default function WithdrawlModal({ onClose, onConfirm }: WithdrawlModalPro