-
Notifications
You must be signed in to change notification settings - Fork 3
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
refactor: 반려동물 정보 등록, 수정 관련 리팩터링 #360
Conversation
- PetProfileProvider > PetAdditionProvider로 변경 - PetProfileContext > PetAdditionContext로 변경
- usePetProfileAddition 생성
- usePetProfileStep - usePetProfileValidation - usePetProfileAddition
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.
전체적으로 반복되는 부분은 한 번만 코멘드 남겼어요!
고생하셨습니다 👍
placeholder="여기를 눌러 아이의 이름을 입력해주세요." | ||
maxLength={10} | ||
isValid | ||
onChange={onChangeName} | ||
design="underline" | ||
fontSize="1.3rem" | ||
/> | ||
<ErrorCaption> | ||
{isValidNameInput | ||
? '' | ||
: '아이의 이름은 1~10글자 사이의 한글, 영어, 숫자만 입력 가능합니다.'} | ||
</ErrorCaption> | ||
</div> | ||
<div> | ||
<InputLabel htmlFor="pet-age">나이 선택</InputLabel> | ||
<PetAgeSelect id="pet-age" onChange={onChangeAge} initialAge={pet.age} /> | ||
<ErrorCaption>{isValidAgeSelect ? '' : '나이를 선택해주세요!'} </ErrorCaption> |
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.
메세지들 상수로 분리하면 어떨까요!?
key={size} | ||
role="radio" | ||
text={size} | ||
onClick={() => onChangePetSize(size)} |
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.
핸들러 함수는 따로 분리하면 어떨까요??
|
||
export const PetProfileProvider = ({ children }: { children: ReactNode }) => { | ||
export const PetAdditionProvider = ({ children }: { children: ReactNode }) => { |
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.
PropsWithChildren
이란 타입도 있어요!
addPetProfileMutation | ||
.addPetProfile(petProfile) | ||
.then(async res => { | ||
const userInfo = JSON.parse(localStorage.getItem('userInfo')!); | ||
|
||
const userPetBreed = breedList?.find(breed => breed.name === petProfile.breed); | ||
const petProfileWithId = { | ||
...petProfile, | ||
id: 1, | ||
petSize: userPetBreed?.name === MIXED_BREED ? petProfile.petSize : PET_SIZES[0], | ||
} as PetProfile; | ||
|
||
updatePetProfileInHeader(petProfileWithId); | ||
|
||
localStorage.setItem('userInfo', JSON.stringify({ ...userInfo, hasPet: true })); | ||
|
||
alert('반려동물 정보 등록이 완료되었습니다.'); | ||
}) | ||
.catch(error => { | ||
alert('반려동물 정보 등록에 실패했습니다.'); | ||
}); |
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.
mutation
구조를 수정하고 onSuccess
, onError
라이프사이클을 활용해보면 어떨까요?
현재는 너무 명령적인 것 같아요!
localStorage.setItem('userInfo', JSON.stringify({ ...userInfo, hasPet: true }));
위 부분도 유저 정보 조회를 invalidate
시키는 것으로 대체할 수 있을 것 같아요!
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.
mutation구조를 수정하고 onSuccess , onError 라이프사이클을 활용하도록 수정했습니다!
그런데 아래 부분은 반영하지 못했습니다🥲
유저 정보 조회를 invalidate해도 auth만 재요청하고 auth/login은 재요청을 하지 않아서
const { mutate: loginZipgo, ...loginRestMutation } = useMutation({
mutationFn: loginZipgoAuth,
onSuccess({ accessToken, authResponse }) {
zipgoLocalStorage.setTokens({ accessToken });
zipgoLocalStorage.setUserInfo(authResponse);
queryClient.invalidateQueries([QUERY_KEY.authenticateUser]);
navigate(routerPath.home());
},
});
여기 onSuccess에 있는 로직이 실행되지 않기 때문에 로컬스토리지 내용을 갱신할 수가 없더라구요..?
뭔가 크게 로직을 수정하기에는 너무 오래 걸릴 것 같아서 일단 보류했습니다.
alert('반려동물 정보 등록에 실패했습니다.'); | ||
}); | ||
|
||
navigate(routerPath.home()); |
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.
useEasyNavigate
의 goHome
함수를 사용하면 좋을 것 같아요!
if (isValidName(petName)) setIsValidNameInput(true); | ||
if (!isValidName(petName)) setIsValidNameInput(false); |
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.
if (isValidName(petName)) setIsValidNameInput(true); | |
if (!isValidName(petName)) setIsValidNameInput(false); | |
setIsValidNameInput(isValidName(petName)); |
은 어떤가요!?
- onSuccess, onError 라이프 사이클 활용
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.
별론데!??
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.
lgtm
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.
고생하셨습니다!
📄 Summary
반려동물 정보 등록, 수정 기능 관련하여 리팩터링을 진행했습니다.
🙋🏻 More