-
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import React, { useEffect, useRef, useState } from "react"; | ||
|
||
import { usePostUploadImages } from "@queries/usePostUploadImages"; | ||
import usePutProfile from "@queries/usePutProfile"; | ||
import { useUserProfile } from "@queries/useUserProfile"; | ||
|
||
import { FORM_VALIDATIONS_MAP } from "@constants/formValidation"; | ||
|
||
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, setIsModalOpen] = useState(false); | ||
|
||
const handleClickEditModalOpenButton = () => setIsModalOpen(true); | ||
const handleClickEditModalCloseButton = () => setIsModalOpen(false); | ||
|
||
const handleClickProfileEditButton = () => setIsModifying(true); | ||
const handleClickProfileImageEditButton = () => profileImageFileInputRef.current?.click(); | ||
|
||
const { mutateAsync: mutateAddImage } = usePostUploadImages(); | ||
|
||
const handleChangeProfileImage = async (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setIsProfileImageLoading(true); | ||
setIsModalOpen(false); | ||
|
||
const files = Array.from(e.target.files as FileList); | ||
const profileImage = await mutateAddImage(files); | ||
|
||
setProfileImageUrl(profileImage[0]); | ||
}; | ||
|
||
const handleLoadProfileImage = () => { | ||
setIsProfileImageLoading(false); | ||
}; | ||
|
||
const handleClickProfileImageDeleteButton = () => { | ||
setProfileImageUrl(""); | ||
|
||
setIsModalOpen(false); | ||
}; | ||
|
||
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, | ||
), | ||
); | ||
}; | ||
|
||
useEffect(() => { | ||
if (data?.nickname) setNickname(data.nickname); | ||
if (data?.profileImageUrl) setProfileImageUrl(data.profileImageUrl); | ||
}, [data?.nickname, data?.profileImageUrl]); | ||
|
||
return { | ||
states: { profileImageUrl, nickname, isModifying, isProfileImageLoading, isModalOpen }, | ||
handlers: { | ||
handleClickEditModalOpenButton, | ||
handleClickEditModalCloseButton, | ||
handleClickProfileEditButton, | ||
handleClickProfileImageEditButton, | ||
handleChangeProfileImage, | ||
handleLoadProfileImage, | ||
handleClickProfileImageDeleteButton, | ||
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 로 반환하고 해당 순서로 정리해 뒀습니다. 또한 해당 부분을 각각 훅으로 분리해서 훨씬 가독성이 좋아졌네요 좋은 피드백 감사합니다 :) |
||
|
||
export default useMyPage; |
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.
사용자 프로필은 여행기 썸네일, 여행기 장소와 달리 크기가 작지만,
usePostUploadImages
에서 똑같이 max width가 900대로 resize될텐데요usePostUploadImages
에서 max width, max height를 props로 받게해서 사용자 이미지는 레티나 디스플레이 대응해서 아바타 프로필 컴포넌트 최대 widht의 2배로 값 넣어주면 더 좋은 최적화가 될 것 같습니다!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.
해당 부분 위와 마찬가지로 수정했습니다 고마워요 :)