|
| 1 | +import Button, { ButtonSize, ButtonVariant } from "@/components/button/button"; |
| 2 | +import Dialog from "@/components/dialog"; |
| 3 | +import Input, { InputSize, InputVariant } from "@/components/input/input"; |
| 4 | +import Profile from "@/components/profile/profile"; |
| 5 | +import { ProfileSize } from "@/components/profile/profile-size"; |
| 6 | +import Sheet, { SheetActionType } from "@/components/sheet"; |
| 7 | +import { useAuth } from "@/features/auth/components/auth-provider"; |
| 8 | +import { changeUserdata } from "@/features/user/apis/change-userdata"; |
| 9 | +import { getMe, GetMeResponse } from "@/features/user/apis/get-me"; |
| 10 | +import { useDialog } from "@/hooks/use-dialog"; |
| 11 | +import { useModal } from "@/hooks/use-modal"; |
| 12 | +import { AxiosError } from "axios"; |
| 13 | +import { useEffect, useRef, useState } from "react"; |
| 14 | +import styles from "./account-setting-modal.module.css"; |
| 15 | +import PasswordChangeModal from "./password-change-modal"; |
| 16 | + |
| 17 | +interface AccountSettingModalProps { |
| 18 | + modalKey: string; |
| 19 | +} |
| 20 | + |
| 21 | +export default function AccountSettingModal({ |
| 22 | + modalKey, |
| 23 | +}: AccountSettingModalProps) { |
| 24 | + const PASSWORD_CHANGE_MODAL_KEY = "PASSWORD_CHANGE_MODAL"; |
| 25 | + const { |
| 26 | + isShowModal: isShowPasswordChangeModal, |
| 27 | + openModal: openPasswordChangeModal, |
| 28 | + } = useModal({ key: PASSWORD_CHANGE_MODAL_KEY }); |
| 29 | + const [userData, setUserData] = useState<GetMeResponse | null>(null); |
| 30 | + const [nickname, setNickname] = useState(""); |
| 31 | + const [profileImage, setProfileImage] = useState(""); |
| 32 | + const { isLoadingToken } = useAuth(); |
| 33 | + const fileInputRef = useRef<HTMLInputElement | null>(null); |
| 34 | + const DIALOG_KEY = "DIALOG_CHAGNE_USERDATA"; |
| 35 | + const { isShowDialog, openDialog } = useDialog({ |
| 36 | + key: DIALOG_KEY, |
| 37 | + }); |
| 38 | + const [dialogMessage, setDialogMessage] = useState(""); |
| 39 | + const [selectedFile, setSelectedFile] = useState<File | null>(null); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + if (isLoadingToken) return; |
| 43 | + async function loadUserData() { |
| 44 | + try { |
| 45 | + setUserData(await getMe()); |
| 46 | + } catch (error: unknown) { |
| 47 | + console.error("Load User Data Failed:", error); |
| 48 | + } |
| 49 | + } |
| 50 | + loadUserData(); |
| 51 | + }, [isLoadingToken]); |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + if (userData) { |
| 55 | + setNickname(userData.nickname); |
| 56 | + setProfileImage(userData.profileImageUrl); |
| 57 | + } |
| 58 | + }, [userData]); |
| 59 | + |
| 60 | + const onNicknameChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 61 | + setNickname(e.target.value); |
| 62 | + }; |
| 63 | + |
| 64 | + const onFileChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 65 | + const file = e.target.files?.[0]; |
| 66 | + if (!file) return; |
| 67 | + const previewUrl = URL.createObjectURL(file); |
| 68 | + setProfileImage(previewUrl); |
| 69 | + setSelectedFile(file); |
| 70 | + }; |
| 71 | + |
| 72 | + const handleDeleteProfileImage = () => { |
| 73 | + setProfileImage(""); |
| 74 | + }; |
| 75 | + |
| 76 | + const handleSubmit = async () => { |
| 77 | + try { |
| 78 | + await changeUserdata(nickname, profileImage); |
| 79 | + setDialogMessage("프로필이 성공적으로 변경되었습니다."); |
| 80 | + } catch (err) { |
| 81 | + const error = err as AxiosError<{ message?: string }>; |
| 82 | + const message = error.response?.data?.message; |
| 83 | + setDialogMessage(message || "프로필 변경에 실패했습니다."); |
| 84 | + } finally { |
| 85 | + openDialog(true); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + return ( |
| 90 | + <Sheet |
| 91 | + sheetKey={modalKey} |
| 92 | + title="프로필 변경" |
| 93 | + actionType={SheetActionType.Modify} |
| 94 | + onAction={handleSubmit} |
| 95 | + > |
| 96 | + <div className={styles.body}> |
| 97 | + <section className={styles.profileImageSection}> |
| 98 | + <div className={styles.profileImage}> |
| 99 | + <Profile |
| 100 | + profileImageUrl={profileImage as string} |
| 101 | + name={userData?.nickname} |
| 102 | + size={ProfileSize.XLarge} |
| 103 | + /> |
| 104 | + </div> |
| 105 | + <div className={styles.profileImageButtons}> |
| 106 | + <Button |
| 107 | + variant={ButtonVariant.Secondary} |
| 108 | + size={ButtonSize.Small} |
| 109 | + onClick={(e) => { |
| 110 | + e.preventDefault(); |
| 111 | + fileInputRef.current?.click(); |
| 112 | + }} |
| 113 | + > |
| 114 | + 사진 변경 |
| 115 | + </Button> |
| 116 | + <Button |
| 117 | + variant={ButtonVariant.Delete} |
| 118 | + size={ButtonSize.Small} |
| 119 | + onClick={(e) => { |
| 120 | + e.preventDefault(); |
| 121 | + handleDeleteProfileImage(); |
| 122 | + }} |
| 123 | + > |
| 124 | + 사진 삭제 |
| 125 | + </Button> |
| 126 | + <input |
| 127 | + className={styles.invisibleFileInput} |
| 128 | + ref={fileInputRef} |
| 129 | + type="file" |
| 130 | + accept="image/*" |
| 131 | + onChange={onFileChange} |
| 132 | + /> |
| 133 | + </div> |
| 134 | + </section> |
| 135 | + <section className={styles.emailSection}> |
| 136 | + <p>이메일</p> |
| 137 | + <Input |
| 138 | + variant={InputVariant.Default} |
| 139 | + $size={InputSize.Auto} |
| 140 | + disabled={true} |
| 141 | + placeholder={userData?.email || ""} |
| 142 | + /> |
| 143 | + </section> |
| 144 | + <section className={styles.nicknameSection}> |
| 145 | + <p>닉네임</p> |
| 146 | + <Input |
| 147 | + variant={InputVariant.Default} |
| 148 | + $size={InputSize.Auto} |
| 149 | + value={nickname} |
| 150 | + onChange={onNicknameChange} |
| 151 | + placeholder={userData?.nickname || ""} |
| 152 | + /> |
| 153 | + </section> |
| 154 | + <section className={styles.passwordSection}> |
| 155 | + <p>비밀번호</p> |
| 156 | + <Button |
| 157 | + variant={ButtonVariant.Secondary} |
| 158 | + size={ButtonSize.Small} |
| 159 | + onClick={(e) => { |
| 160 | + e.preventDefault(); |
| 161 | + openPasswordChangeModal(true); |
| 162 | + }} |
| 163 | + > |
| 164 | + 비밀번호 변경 |
| 165 | + </Button> |
| 166 | + </section> |
| 167 | + </div> |
| 168 | + {isShowPasswordChangeModal && ( |
| 169 | + <PasswordChangeModal modalKey={PASSWORD_CHANGE_MODAL_KEY} /> |
| 170 | + )} |
| 171 | + |
| 172 | + {isShowDialog && ( |
| 173 | + <Dialog dialogKey={DIALOG_KEY} message={dialogMessage} /> |
| 174 | + )} |
| 175 | + </Sheet> |
| 176 | + ); |
| 177 | +} |
0 commit comments