|
| 1 | +import { useState, FC, ChangeEvent } from 'react'; |
1 | 2 | import { useGetProfile } from '@/react-queries/userGetProfile';
|
| 3 | +import HistoryBackButton from '@/components/common/HistoryBackButton'; |
| 4 | +import ProfileIcon from '@/assets/svgs/ProfileIcon'; |
| 5 | +// import { useMutation } from '@tanstack/react-query'; |
| 6 | +import { updateUserProfile } from '@/apis/updateUserApi'; |
2 | 7 |
|
3 |
| -const ProfilePage = () => { |
| 8 | +interface Props { |
| 9 | + userProfile?: { |
| 10 | + imageUrl: string | null; |
| 11 | + }; |
| 12 | +} |
| 13 | + |
| 14 | +const ProfilePage: FC<Props> = ({ userProfile }) => { |
4 | 15 | const { data: user, error, isLoading, isError } = useGetProfile();
|
5 |
| - console.log(user); |
| 16 | + const [editMode, setEditMode] = useState(false); |
| 17 | + const [imageUrl, setImageUrl] = useState(userProfile ? userProfile?.imageUrl : null); |
| 18 | + const [nickname, setNickname] = useState(user ? user?.user_nickname : ''); |
| 19 | + const [selectedFile, setSelectedFile] = useState<File | null>(null); |
| 20 | + |
| 21 | + const handleImageChange = (event: ChangeEvent<HTMLInputElement>) => { |
| 22 | + const file = event.target.files ? event.target.files[0] : null; |
| 23 | + if (file) { |
| 24 | + const reader = new FileReader(); |
| 25 | + reader.onloadend = () => { |
| 26 | + setImageUrl(reader.result as string); |
| 27 | + }; |
| 28 | + reader.readAsDataURL(file); |
| 29 | + setSelectedFile(file); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + const toggleEditMode = () => { |
| 34 | + setEditMode(!editMode); |
| 35 | + }; |
| 36 | + |
| 37 | + const resetData = () => { |
| 38 | + if (user) { |
| 39 | + setNickname(user.user_nickname); |
| 40 | + setImageUrl(userProfile ? userProfile.imageUrl : null); |
| 41 | + setSelectedFile(null); |
| 42 | + } |
| 43 | + setEditMode(false); |
| 44 | + }; |
| 45 | + |
| 46 | + const updateProfile = async () => { |
| 47 | + if (!user) return; |
| 48 | + |
| 49 | + const param = { |
| 50 | + id: user.id, |
| 51 | + user_nickname: nickname, |
| 52 | + file: selectedFile, |
| 53 | + }; |
| 54 | + |
| 55 | + try { |
| 56 | + const updatedData = await updateUserProfile(param); |
| 57 | + console.log('프로필 업데이트 성공:', updatedData); |
| 58 | + setEditMode(false); |
| 59 | + } catch (error) { |
| 60 | + console.error('프로필 업데이트 실패:', error); |
| 61 | + } |
| 62 | + }; |
6 | 63 |
|
7 | 64 | if (isError) {
|
8 | 65 | // TODO: 추후 에러 처리
|
9 | 66 | console.error(error);
|
10 | 67 | }
|
11 | 68 |
|
12 | 69 | return (
|
13 |
| - <div> |
| 70 | + <div className="flex min-h-dvh w-screen flex-col overflow-x-hidden px-6"> |
14 | 71 | {isLoading && <span className="loading" />}
|
15 |
| - <h1>프로필</h1> |
16 |
| - <p>여기는 프로필 페이지입니다.</p> |
| 72 | + <nav className="navHeight flex w-full items-center justify-between px-5 py-5"> |
| 73 | + <HistoryBackButton /> |
| 74 | + <h1 className="text-xl font-semibold" hidden> |
| 75 | + 프로필 |
| 76 | + </h1> |
| 77 | + {/* 편집버튼 */} |
| 78 | + <button type="button" onClick={toggleEditMode} hidden={editMode}> |
| 79 | + <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 80 | + <path |
| 81 | + d="M16.8617 4.48667L18.5492 2.79917C19.2814 2.06694 20.4686 2.06694 21.2008 2.79917C21.9331 3.53141 21.9331 4.71859 21.2008 5.45083L6.83218 19.8195C6.30351 20.3481 5.65144 20.7368 4.93489 20.9502L2.25 21.75L3.04978 19.0651C3.26323 18.3486 3.65185 17.6965 4.18052 17.1678L16.8617 4.48667ZM16.8617 4.48667L19.5 7.12499" |
| 82 | + stroke="#0F172A" |
| 83 | + strokeWidth="1.5" |
| 84 | + strokeLinejoin="round" |
| 85 | + /> |
| 86 | + </svg> |
| 87 | + </button> |
| 88 | + </nav> |
| 89 | + <div className="container mx-auto flex max-w-sm flex-1 flex-col gap-4 pb-[50px] pt-4"> |
| 90 | + <div className="relative mx-auto my-5 flex h-80 w-80 items-center justify-center"> |
| 91 | + {/* <button type="button" hidden={!editMode} className="absolute right-2.5 top-0" onClick={() => handleImageButton()}> |
| 92 | + <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 93 | + <path d="M16.8617 4.48667L18.5492 2.79917C19.2814 2.06694 20.4686 2.06694 21.2008 2.79917C21.9331 3.53141 21.9331 4.71859 21.2008 5.45083L6.83218 19.8195C6.30351 20.3481 5.65144 20.7368 4.93489 20.9502L2.25 21.75L3.04978 19.0651C3.26323 18.3486 3.65185 17.6965 4.18052 17.1678L16.8617 4.48667ZM16.8617 4.48667L19.5 7.12499" stroke="#0F172A" strokeWidth="1.5" strokeLinejoin="round"/> |
| 94 | + </svg> |
| 95 | + </button> */} |
| 96 | + <ProfileIcon imageUrl={imageUrl} width="100%" height="100%" fill="#CCCFC4" /> |
| 97 | + {editMode && ( |
| 98 | + <label |
| 99 | + htmlFor="img" |
| 100 | + className="absolute inset-0 flex flex-col items-center justify-center gap-2.5 rounded-full bg-black bg-opacity-50 text-white" |
| 101 | + > |
| 102 | + <span>이미지를 드래그해서 넣어주세요!</span> |
| 103 | + <input |
| 104 | + type="file" |
| 105 | + id="img" |
| 106 | + className="file-input file-input-bordered file-input-success file-input-sm w-4/5 max-w-xs" |
| 107 | + accept="image/*" |
| 108 | + onChange={(e) => handleImageChange(e)} |
| 109 | + /> |
| 110 | + </label> |
| 111 | + )} |
| 112 | + </div> |
| 113 | + <div className="info-form mt-8 flex flex-col gap-1"> |
| 114 | + <div className="flex items-center justify-between"> |
| 115 | + <span>이름</span> |
| 116 | + <input type="text" value={user?.user_name} readOnly className="bg-transparent p-2" /> |
| 117 | + </div> |
| 118 | + <div className="flex items-center justify-between"> |
| 119 | + <span>닉네임</span> |
| 120 | + <input |
| 121 | + type="text" |
| 122 | + value={nickname as string} |
| 123 | + onChange={(e) => setNickname(e.target.value)} |
| 124 | + readOnly={!editMode} |
| 125 | + className={`p-2 ${!editMode ? 'bg-transparent' : ''}`} |
| 126 | + /> |
| 127 | + </div> |
| 128 | + {/* <div className="flex justify-between items-center"> |
| 129 | + <span>이메일</span> |
| 130 | + <input |
| 131 | + type="text" |
| 132 | + value={user?.user_name} |
| 133 | + placeholder="이메일" |
| 134 | + readOnly |
| 135 | + className="p-2 bg-transparent" |
| 136 | + /> |
| 137 | + </div> */} |
| 138 | + <div className="flex items-center justify-between"> |
| 139 | + <span>전화번호</span> |
| 140 | + <input type="text" value={user?.phone} readOnly className="bg-transparent p-2" /> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + |
| 144 | + {editMode && ( |
| 145 | + <div className="mt-20 flex gap-2"> |
| 146 | + <button className="btn btn-outline btn-primary flex-1" onClick={() => resetData()}> |
| 147 | + 취소 |
| 148 | + </button> |
| 149 | + <button type="submit" className="btn btn-outline btn-primary flex-1" onClick={() => updateProfile()}> |
| 150 | + 수정완료 |
| 151 | + </button> |
| 152 | + </div> |
| 153 | + )} |
| 154 | + </div> |
17 | 155 | </div>
|
18 | 156 | );
|
19 | 157 | };
|
|
0 commit comments