|
1 | | -import { Link } from 'react-router-dom'; |
| 1 | +import { useState, useEffect, useContext, useCallback } from 'react'; |
| 2 | +import { useNavigate } from 'react-router-dom'; |
| 3 | +import { AuthContext } from '@/context/AuthContext'; |
| 4 | +import RegisterLayout from '@/components/layout/RegisterLayout'; |
| 5 | +import Footer from '@/components/layout/Footer'; |
| 6 | +import Table from '@/components/common/table/Table'; |
| 7 | +import Button from '@/components/common/Button'; |
| 8 | +import Modal from '@/components/common/Modal'; |
| 9 | +import { getUser } from '@/api/userApi'; |
| 10 | +import { getUserApplications } from '@/api/applicationApi'; |
| 11 | +import formatPhone from '@/utils/formatPhone'; |
| 12 | +import phone from '@/assets/icons/phone.svg'; |
| 13 | +import location from '@/assets/icons/location-red.svg'; |
| 14 | + |
2 | 15 | export default function Profile() { |
| 16 | + const navigate = useNavigate(); |
| 17 | + const { isLoggedIn } = useContext(AuthContext); |
| 18 | + const [profileInfo, setProfileInfo] = useState({ |
| 19 | + name: '', |
| 20 | + phone: '', |
| 21 | + address: '', |
| 22 | + bio: '', |
| 23 | + }); |
| 24 | + const [hasInfo, setHasInfo] = useState(false); |
| 25 | + const [hasApplications, setHasApplications] = useState(false); |
| 26 | + const [modal, setModal] = useState({ |
| 27 | + isOpen: false, |
| 28 | + message: '', |
| 29 | + }); |
| 30 | + const [buttonSize, setButtonSize] = useState<'large' | 'medium'>('medium'); |
| 31 | + const [isLoading, setIsLoading] = useState(true); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + const handleResize = () => { |
| 35 | + if (window.innerWidth >= 768) { |
| 36 | + setButtonSize('large'); |
| 37 | + } else { |
| 38 | + setButtonSize('medium'); |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + handleResize(); |
| 43 | + window.addEventListener('resize', handleResize); |
| 44 | + |
| 45 | + return () => window.removeEventListener('resize', handleResize); |
| 46 | + }, []); |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + const userId = localStorage.getItem('userId'); |
| 50 | + |
| 51 | + if (!userId) { |
| 52 | + setModal({ |
| 53 | + isOpen: true, |
| 54 | + message: '로그인이 필요합니다.', |
| 55 | + }); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + const fetchUserInfo = async () => { |
| 60 | + try { |
| 61 | + const userInfo = await getUser(userId); |
| 62 | + setProfileInfo({ |
| 63 | + name: userInfo.item.name ?? '', |
| 64 | + phone: userInfo.item.phone ?? '', |
| 65 | + address: userInfo.item.address ?? '', |
| 66 | + bio: userInfo.item.bio ?? '', |
| 67 | + }); |
| 68 | + setHasInfo(!!userInfo.item.name); |
| 69 | + const applications = await getUserApplications(userId); |
| 70 | + setHasApplications(applications.count > 0); |
| 71 | + } catch (error) { |
| 72 | + setModal({ |
| 73 | + isOpen: true, |
| 74 | + message: (error as Error).message, |
| 75 | + }); |
| 76 | + } finally { |
| 77 | + setIsLoading(false); |
| 78 | + } |
| 79 | + }; |
| 80 | + fetchUserInfo(); |
| 81 | + }, [isLoggedIn]); |
| 82 | + |
| 83 | + const handleModalConfirm = useCallback(() => { |
| 84 | + if (modal.message.includes('로그인')) { |
| 85 | + setModal({ isOpen: false, message: '' }); |
| 86 | + navigate('/login'); |
| 87 | + } else { |
| 88 | + setModal({ isOpen: false, message: '' }); |
| 89 | + } |
| 90 | + }, [modal.message, navigate]); |
| 91 | + |
3 | 92 | return ( |
4 | 93 | <> |
5 | | - <Link to="/profile/edit">등록하기</Link> |
| 94 | + {isLoading ? ( |
| 95 | + <div className="flex min-h-[calc(100vh-102px)] items-center justify-center md:min-h-[calc(100vh-70px)]"> |
| 96 | + <div className="size-100 animate-spin rounded-full border-8 border-gray-200 border-t-primary" /> |
| 97 | + </div> |
| 98 | + ) : ( |
| 99 | + <div className="flex min-h-[calc(100vh-102px)] flex-col justify-between md:min-h-[calc(100vh-70px)]"> |
| 100 | + {!hasInfo ? ( |
| 101 | + <div className="mx-12 flex flex-col gap-16 py-40 md:mx-32 md:gap-24 md:py-60 lg:mx-auto lg:w-964"> |
| 102 | + <h1 className="text-h3 font-bold md:text-h1">내 프로필</h1> |
| 103 | + <RegisterLayout type="profile" /> |
| 104 | + </div> |
| 105 | + ) : ( |
| 106 | + <div className="mx-12 flex flex-col gap-16 py-40 md:mx-32 md:gap-24 md:py-60 lg:mx-auto lg:w-964 lg:flex-row lg:justify-between lg:gap-180"> |
| 107 | + <h1 className="text-h3 font-bold md:text-h1">내 프로필</h1> |
| 108 | + <div className="flex justify-between rounded-[12px] bg-red-10 p-20 md:p-32 lg:flex-1"> |
| 109 | + <div className="flex flex-col gap-8 md:gap-12"> |
| 110 | + <div className="flex flex-col gap-8"> |
| 111 | + <div className="text-body2/17 font-bold text-primary md:text-body1/20"> |
| 112 | + 이름 |
| 113 | + </div> |
| 114 | + <h2 className="text-h2/29 font-bold md:text-h1/34"> |
| 115 | + {profileInfo.name} |
| 116 | + </h2> |
| 117 | + </div> |
| 118 | + <div className="flex items-center gap-6 text-body2/22 text-gray-50 md:text-body1/26"> |
| 119 | + <img src={phone} className="size-16 md:size-20" /> |
| 120 | + {formatPhone(profileInfo.phone)} |
| 121 | + </div> |
| 122 | + <div className="flex items-center gap-6 text-body2/22 text-gray-50 md:text-body1/26"> |
| 123 | + <img src={location} className="size-16 md:size-20" /> |
| 124 | + 선호 지역: {profileInfo.address} |
| 125 | + </div> |
| 126 | + {profileInfo.bio && ( |
| 127 | + <p className="mt-12 text-body2/22 md:mt-16 md:text-body1/26"> |
| 128 | + {profileInfo.bio} |
| 129 | + </p> |
| 130 | + )} |
| 131 | + </div> |
| 132 | + <Button |
| 133 | + size={buttonSize} |
| 134 | + solid={false} |
| 135 | + onClick={() => navigate('/profile/edit')} |
| 136 | + className="w-108 shrink-0 md:w-169" |
| 137 | + > |
| 138 | + 편집하기 |
| 139 | + </Button> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + )} |
| 143 | + |
| 144 | + {hasInfo && ( |
| 145 | + <div className="flex-1 bg-gray-5"> |
| 146 | + <div className="mx-12 flex flex-col gap-16 pt-40 pb-80 md:mx-32 md:gap-24 md:pt-60 md:pb-120 lg:mx-auto lg:w-964"> |
| 147 | + <h1 className="text-h3 font-bold md:text-h1">신청 내역</h1> |
| 148 | + {!hasApplications ? ( |
| 149 | + <RegisterLayout type="application" /> |
| 150 | + ) : ( |
| 151 | + <Table |
| 152 | + mode="user" |
| 153 | + userId={localStorage.getItem('userId') ?? ''} |
| 154 | + /> |
| 155 | + )} |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + )} |
| 159 | + <Footer /> |
| 160 | + {modal.isOpen && ( |
| 161 | + <Modal |
| 162 | + onClose={handleModalConfirm} |
| 163 | + onButtonClick={handleModalConfirm} |
| 164 | + > |
| 165 | + {modal.message} |
| 166 | + </Modal> |
| 167 | + )} |
| 168 | + </div> |
| 169 | + )} |
6 | 170 | </> |
7 | 171 | ); |
8 | 172 | } |
0 commit comments