-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] - 프로필 이미지 수정 기능 및 여행기 등록 시 여행 장소마다 국가 코드 주도록 구현 #535
Changes from 1 commit
004644c
cef7429
7912136
0aa2cfc
dde854e
932e318
aa16247
2eb4464
537c3e8
98076f6
fbc78f4
9321263
3f702c4
ae9be47
01b6786
c7561fd
b889315
cac5077
7e6ca5e
f318d32
c810327
0ec73d3
3f18f98
211c7d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,78 @@ | ||
import React, { useEffect, useRef, useState } from "react"; | ||
|
||
import { usePostUploadImages } from "@queries/usePostUploadImages"; | ||
import usePutProfile from "@queries/usePutProfile"; | ||
import { useUserProfile } from "@queries/useUserProfile"; | ||
|
||
import useToggle from "@hooks/useToggle"; | ||
|
||
import { FORM_VALIDATIONS_MAP } from "@constants/formValidation"; | ||
import useProfileEdit from "./useProfileEdit"; | ||
import useProfileImage from "./useProfileImage"; | ||
import useProfileInitialization from "./useProfileInitialization"; | ||
import useProfileNickname from "./useProfileNickname"; | ||
|
||
const useMyPage = () => { | ||
const { data, status, error } = useUserProfile(); | ||
|
||
const onError = (error: Error) => { | ||
alert(error.message); | ||
setNickname(data?.nickname ?? ""); | ||
}; | ||
|
||
const { mutate: mutateModifyProfile } = usePutProfile(onError); | ||
|
||
const profileImageFileInputRef = useRef<HTMLInputElement>(null); | ||
|
||
const [profileImageUrl, setProfileImageUrl] = useState(data?.profileImageUrl ?? ""); | ||
const [nickname, setNickname] = useState(data?.nickname ?? ""); | ||
|
||
const [isModifying, setIsModifying] = useState(false); | ||
const [isProfileImageLoading, setIsProfileImageLoading] = useState(false); | ||
|
||
const [isModalOpen, handleOpenModal, handleCloseModal] = useToggle(); | ||
|
||
const handleClickEditModalOpenButton = () => handleOpenModal(); | ||
const handleClickEditModalCloseButton = () => handleCloseModal(); | ||
const [isEditModalOpen, handleOpenEditModal, handleCloseEditModal] = useToggle(); | ||
|
||
const handleClickProfileEditButton = () => setIsModifying(true); | ||
const handleClickProfileImageEditButton = () => profileImageFileInputRef.current?.click(); | ||
|
||
const { mutateAsync: mutateAddImage } = usePostUploadImages(); | ||
|
||
const handleChangeProfileImage = async (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setIsProfileImageLoading(true); | ||
handleCloseModal(); | ||
|
||
const files = Array.from(e.target.files as FileList); | ||
const profileImage = await mutateAddImage(files); | ||
|
||
setProfileImageUrl(profileImage[0]); | ||
}; | ||
|
||
const handleLoadProfileImage = () => { | ||
setIsProfileImageLoading(false); | ||
}; | ||
|
||
const handleClickProfileImageDeleteButton = () => { | ||
setProfileImageUrl(""); | ||
|
||
handleCloseModal(); | ||
}; | ||
|
||
const handleClickProfileEditConfirmButton = () => { | ||
const trimmedNickname = nickname.trim(); | ||
const newNickname = trimmedNickname || data?.nickname || ""; | ||
|
||
setNickname(newNickname); | ||
mutateModifyProfile({ nickname: newNickname, profileImageUrl: profileImageUrl }); | ||
|
||
setIsModifying(false); | ||
}; | ||
|
||
const handleClickProfileEditCancelButton = () => { | ||
setNickname(data?.nickname ?? ""); | ||
setProfileImageUrl(data?.profileImageUrl ?? ""); | ||
|
||
setIsModifying(false); | ||
}; | ||
|
||
const handleChangeNickname = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setNickname( | ||
e.target.value.slice( | ||
FORM_VALIDATIONS_MAP.title.minLength, | ||
FORM_VALIDATIONS_MAP.title.maxLength, | ||
), | ||
); | ||
}; | ||
const { data, status, error } = useUserProfile(); | ||
const { nickname: userNickname, profileImageUrl: userProfileImageUrl } = data ?? {}; | ||
|
||
useEffect(() => { | ||
if (data?.nickname) setNickname(data.nickname); | ||
if (data?.profileImageUrl) setProfileImageUrl(data.profileImageUrl); | ||
}, [data?.nickname, data?.profileImageUrl]); | ||
const { | ||
profileImageFileInputRef, | ||
profileImageUrl, | ||
isProfileImageLoading, | ||
handleClickProfileImageEditButton, | ||
handleChangeProfileImage, | ||
handleLoadProfileImage, | ||
handleClickProfileImageDeleteButton, | ||
updateProfileImageUrl, | ||
} = useProfileImage({ userProfileImageUrl, handleCloseEditModal }); | ||
|
||
const { nickname, handleChangeNickname, updateNickname } = useProfileNickname(userNickname); | ||
|
||
const { | ||
isModifying, | ||
handleClickProfileEditButton, | ||
handleClickProfileEditConfirmButton, | ||
handleClickProfileEditCancelButton, | ||
} = useProfileEdit({ | ||
userNickname, | ||
nickname, | ||
updateNickname, | ||
userProfileImageUrl, | ||
profileImageUrl, | ||
updateProfileImageUrl, | ||
}); | ||
|
||
useProfileInitialization({ | ||
userNickname, | ||
updateNickname, | ||
userProfileImageUrl, | ||
updateProfileImageUrl, | ||
}); | ||
|
||
return { | ||
states: { profileImageUrl, nickname, isModifying, isProfileImageLoading, isModalOpen }, | ||
handlers: { | ||
handleClickEditModalOpenButton, | ||
handleClickEditModalCloseButton, | ||
handleClickProfileEditButton, | ||
editModal: { | ||
isEditModalOpen, | ||
handleOpenEditModal, | ||
handleCloseEditModal, | ||
}, | ||
profileImage: { | ||
profileImageFileInputRef, | ||
profileImageUrl, | ||
isProfileImageLoading, | ||
handleClickProfileImageEditButton, | ||
handleChangeProfileImage, | ||
handleLoadProfileImage, | ||
handleClickProfileImageDeleteButton, | ||
}, | ||
profileNickname: { | ||
nickname, | ||
handleChangeNickname, | ||
}, | ||
profileEdit: { | ||
isModifying, | ||
handleClickProfileEditButton, | ||
handleClickProfileEditConfirmButton, | ||
handleClickProfileEditCancelButton, | ||
handleChangeNickname, | ||
}, | ||
userProfile: { data, status, error }, | ||
profileImageFileInputRef, | ||
}; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 시간이 충분하다면 이 훅을 조금 더 리팩터링 해보는 것도 좋을거 같단 생각이 들었어요,,, (너무 많은 책임들이 쏠려 있어서 그런지 코드 해석에 시간이 걸렸어요) 제 생각에는
정도로만 나눠도 괜찮을거 같다는 생각이 들었어요~! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 우선 states, handler 이런식으로 반환하던 것을 editModal, profileImage, profileNickname, ProfileEdit, userProfile 로 반환하고 해당 순서로 정리해 뒀습니다. 또한 해당 부분을 각각 훅으로 분리해서 훨씬 가독성이 좋아졌네요 좋은 피드백 감사합니다 :) |
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useState } from "react"; | ||
|
||
import usePutProfile from "@queries/usePutProfile"; | ||
|
||
interface UseProfileEdit { | ||
userNickname: string | undefined; | ||
nickname: string; | ||
updateNickname: (newNickname: string) => void; | ||
userProfileImageUrl: string | undefined; | ||
profileImageUrl: string; | ||
updateProfileImageUrl: (newProfileImageUrl: string) => void; | ||
} | ||
|
||
const useProfileEdit = ({ | ||
userNickname, | ||
nickname, | ||
updateNickname, | ||
userProfileImageUrl, | ||
profileImageUrl, | ||
updateProfileImageUrl, | ||
}: UseProfileEdit) => { | ||
const onError = (error: Error) => { | ||
alert(error.message); | ||
updateNickname(userNickname ?? ""); | ||
updateProfileImageUrl(userProfileImageUrl ?? ""); | ||
}; | ||
|
||
const { mutate: mutateModifyProfile } = usePutProfile(onError); | ||
|
||
const [isModifying, setIsModifying] = useState(false); | ||
|
||
const handleClickProfileEditButton = () => setIsModifying(true); | ||
|
||
const handleClickProfileEditConfirmButton = () => { | ||
const trimmedNickname = nickname.trim(); | ||
const newNickname = trimmedNickname || userNickname || ""; | ||
|
||
updateNickname(newNickname); | ||
mutateModifyProfile({ nickname: newNickname, profileImageUrl }); | ||
|
||
setIsModifying(false); | ||
}; | ||
|
||
const handleClickProfileEditCancelButton = () => { | ||
updateNickname(userNickname ?? ""); | ||
updateProfileImageUrl(userProfileImageUrl ?? ""); | ||
|
||
setIsModifying(false); | ||
}; | ||
return { | ||
isModifying, | ||
handleClickProfileEditButton, | ||
handleClickProfileEditConfirmButton, | ||
handleClickProfileEditCancelButton, | ||
}; | ||
}; | ||
|
||
export default useProfileEdit; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
확실히 이전보다 코드가 더 깔끔해졌네요 :) 감사합니다!