|
1 | 1 | /** |
2 | 2 | * @file LoginButton.tsx |
3 | | - * @description 로그인 버튼 컴포넌트 |
| 3 | + * @description 로그인/프로필 버튼 컴포넌트 |
4 | 4 | * |
5 | | - * 헤더 우측에 표시되는 로그인 링크입니다. |
| 5 | + * 비로그인 상태: 로그인 버튼 (클릭 시 로그인 모달) |
| 6 | + * 로그인 상태: 사용자 이름 + 프로필 이미지 (클릭 시 로그아웃/회원탈퇴 드롭다운) |
6 | 7 | */ |
| 8 | +import { useState } from 'react'; |
| 9 | + |
| 10 | +import { apiClient } from '@/api/client'; |
7 | 11 | import LoginIcon from '@/assets/icons/icon-login.svg?react'; |
| 12 | +import LogoutIcon from '@/assets/icons/icon-logout.svg?react'; |
| 13 | +import { Dropdown } from '@/components/common/Dropdown'; |
| 14 | +import { Modal } from '@/components/common/Modal'; |
8 | 15 | import { useAuthStore } from '@/stores/authStore'; |
| 16 | +import { showToast } from '@/utils/toast'; |
9 | 17 |
|
10 | 18 | import { HeaderButton } from './HeaderButton'; |
11 | 19 |
|
12 | 20 | export function LoginButton() { |
| 21 | + const user = useAuthStore((s) => s.user); |
13 | 22 | const openLoginModal = useAuthStore((s) => s.openLoginModal); |
| 23 | + const logout = useAuthStore((s) => s.logout); |
| 24 | + |
| 25 | + const [isWithdrawModalOpen, setIsWithdrawModalOpen] = useState(false); |
| 26 | + const [isWithdrawing, setIsWithdrawing] = useState(false); |
| 27 | + |
| 28 | + if (!user) { |
| 29 | + return <HeaderButton text="로그인" icon={<LoginIcon />} onClick={openLoginModal} />; |
| 30 | + } |
| 31 | + |
| 32 | + const handleWithdraw = async () => { |
| 33 | + setIsWithdrawing(true); |
| 34 | + try { |
| 35 | + await apiClient.delete(`/users/${user.id}`); |
| 36 | + logout(); |
| 37 | + setIsWithdrawModalOpen(false); |
| 38 | + showToast.success('회원 탈퇴가 완료되었습니다.'); |
| 39 | + } catch { |
| 40 | + showToast.error('회원 탈퇴에 실패했습니다.', '잠시 후 다시 시도해주세요.'); |
| 41 | + } finally { |
| 42 | + setIsWithdrawing(false); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + return ( |
| 47 | + <> |
| 48 | + <Dropdown |
| 49 | + position="bottom" |
| 50 | + align="end" |
| 51 | + ariaLabel="사용자 메뉴" |
| 52 | + trigger={ |
| 53 | + <button |
| 54 | + type="button" |
| 55 | + className="flex cursor-pointer items-center gap-2 text-body-s-bold text-gray-800 transition-colors hover:text-gray-600" |
| 56 | + > |
| 57 | + {user.name ?? '사용자'} |
| 58 | + {user.profileImage ? ( |
| 59 | + <img |
| 60 | + src={user.profileImage} |
| 61 | + alt="프로필" |
| 62 | + className="size-6 rounded-full object-cover" |
| 63 | + /> |
| 64 | + ) : ( |
| 65 | + <div className="size-6 rounded-full bg-gray-200" /> |
| 66 | + )} |
| 67 | + </button> |
| 68 | + } |
| 69 | + items={[ |
| 70 | + { |
| 71 | + id: 'logout', |
| 72 | + label: ( |
| 73 | + <span className="flex items-center gap-1"> |
| 74 | + 로그아웃 |
| 75 | + <LogoutIcon className="size-6" /> |
| 76 | + </span> |
| 77 | + ), |
| 78 | + onClick: logout, |
| 79 | + variant: 'danger', |
| 80 | + }, |
| 81 | + { |
| 82 | + id: 'withdraw', |
| 83 | + label: '회원 탈퇴', |
| 84 | + onClick: () => setIsWithdrawModalOpen(true), |
| 85 | + variant: 'danger', |
| 86 | + }, |
| 87 | + ]} |
| 88 | + /> |
14 | 89 |
|
15 | | - return <HeaderButton text="로그인" icon={<LoginIcon />} onClick={openLoginModal} />; |
| 90 | + <Modal |
| 91 | + isOpen={isWithdrawModalOpen} |
| 92 | + onClose={() => setIsWithdrawModalOpen(false)} |
| 93 | + title="회원 탈퇴" |
| 94 | + size="sm" |
| 95 | + closeOnBackdropClick={!isWithdrawing} |
| 96 | + closeOnEscape={!isWithdrawing} |
| 97 | + > |
| 98 | + <p className="text-body-m"> |
| 99 | + 탈퇴하면 모든 데이터가 삭제되며 복구할 수 없습니다. |
| 100 | + <br /> |
| 101 | + 정말 탈퇴하시겠습니까? |
| 102 | + </p> |
| 103 | + <div className="mt-7 flex gap-3"> |
| 104 | + <button |
| 105 | + className="flex-1 rounded-md bg-gray-100 py-3 font-bold text-gray-600 transition-colors hover:bg-gray-200 disabled:opacity-50" |
| 106 | + type="button" |
| 107 | + onClick={() => setIsWithdrawModalOpen(false)} |
| 108 | + disabled={isWithdrawing} |
| 109 | + > |
| 110 | + 취소 |
| 111 | + </button> |
| 112 | + <button |
| 113 | + className="flex-1 rounded-md bg-error py-3 font-bold text-white transition-colors hover:bg-error/90 disabled:opacity-50" |
| 114 | + type="button" |
| 115 | + onClick={handleWithdraw} |
| 116 | + disabled={isWithdrawing} |
| 117 | + > |
| 118 | + {isWithdrawing ? '탈퇴 중...' : '탈퇴'} |
| 119 | + </button> |
| 120 | + </div> |
| 121 | + </Modal> |
| 122 | + </> |
| 123 | + ); |
16 | 124 | } |
0 commit comments