Skip to content
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

Merged
merged 14 commits into from
Sep 11, 2023

Conversation

HyeryongChoi
Copy link
Collaborator

📄 Summary

반려동물 정보 등록, 수정 기능 관련하여 리팩터링을 진행했습니다.

  • PetProfileProvider -> PetAdditionProvider 함수명 변경
  • PetProfileOutletContextProps -> PetAdditionOutletContextProps 타입명 변경
  • 반려동물 등록 전체적인 구조 변경
  • 반려동물 수정 페이지 -> PetProfileEditionForm 컴포넌트 분리
  • usePetProfileEdition 훅 추가

🙋🏻 More

- PetProfileProvider > PetAdditionProvider로 변경
- PetProfileContext > PetAdditionContext로 변경
- usePetProfileStep
- usePetProfileValidation
- usePetProfileAddition
Copy link
Collaborator

@n0eyes n0eyes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전체적으로 반복되는 부분은 한 번만 코멘드 남겼어요!
고생하셨습니다 👍

Comment on lines 51 to 67
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>
Copy link
Collaborator

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)}
Copy link
Collaborator

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 }) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PropsWithChildren이란 타입도 있어요!

Comment on lines 93 to 113
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('반려동물 정보 등록에 실패했습니다.');
});
Copy link
Collaborator

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시키는 것으로 대체할 수 있을 것 같아요!

Copy link
Collaborator Author

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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useEasyNavigategoHome 함수를 사용하면 좋을 것 같아요!

Comment on lines 41 to 42
if (isValidName(petName)) setIsValidNameInput(true);
if (!isValidName(petName)) setIsValidNameInput(false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (isValidName(petName)) setIsValidNameInput(true);
if (!isValidName(petName)) setIsValidNameInput(false);
setIsValidNameInput(isValidName(petName));

은 어떤가요!?

Copy link
Collaborator

@wonyongChoi05 wonyongChoi05 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

별론데!??

Copy link
Member

@ksone02 ksone02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Collaborator

@n0eyes n0eyes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다!

@HyeryongChoi HyeryongChoi merged commit 2533c9e into develop Sep 11, 2023
1 check passed
@wonyongChoi05 wonyongChoi05 deleted the refactor/#359 branch September 12, 2023 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FE] 반려동물 정보 등록, 수정 관련 리팩터링
4 participants