Skip to content

Commit c19ecfc

Browse files
authored
hotfix: 리뷰, 펫 프로필 업데이트에 따라 상태 동기화 (#514)
* refactor: 불필요한 컴포넌트 분리 병합 * fix: 1분 잘못된 시간 수정 * fix: 리뷰 업데이트에 따라 상품 별점 동기화 * fix: 펫 프로필 업데이트에 따라 펫 프로필 정보 동기화
1 parent 834c5bf commit c19ecfc

File tree

6 files changed

+43
-42
lines changed

6 files changed

+43
-42
lines changed

frontend/src/components/PetProfile/PetListBottomSheet.tsx

+12-24
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,26 @@ const PetListBottomSheet = () => (
1212
<Loading>
1313
<Dialog.BackDrop />
1414
<Dialog.Content asChild>
15-
{({ openHandler }) => <PetListContainer toggleDialog={openHandler} />}
15+
<ContentLayout>
16+
<DialogHeader>어떤 아이 식품을 찾으세요?</DialogHeader>
17+
<DialogContent>
18+
<PetList />
19+
</DialogContent>
20+
<ButtonWrapper>
21+
<Dialog.Close asChild>
22+
<CloseButton type="button">닫기</CloseButton>
23+
</Dialog.Close>
24+
</ButtonWrapper>
25+
</ContentLayout>
1626
</Dialog.Content>
1727
</Loading>
1828
</Dialog.Portal>
1929
</Dialog>
2030
);
2131

22-
interface PetListContainerProps {
23-
toggleDialog: VoidFunction;
24-
}
25-
26-
const PetListContainer = (props: PetListContainerProps) => {
27-
const { toggleDialog } = props;
28-
29-
return (
30-
<Layout>
31-
<DialogHeader>어떤 아이 식품을 찾으세요?</DialogHeader>
32-
<DialogContent>
33-
<PetList />
34-
</DialogContent>
35-
<ButtonWrapper>
36-
<CloseButton type="button" onClick={toggleDialog}>
37-
닫기
38-
</CloseButton>
39-
</ButtonWrapper>
40-
</Layout>
41-
);
42-
};
43-
4432
export default PetListBottomSheet;
4533

46-
const Layout = styled.div`
34+
const ContentLayout = styled.div`
4735
position: fixed;
4836
z-index: 1001;
4937
bottom: 0;

frontend/src/constants/time.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const ONE_HOUR = 3600000;
22

3-
const ONE_MINUTE = 3600000;
3+
const ONE_MINUTE = 60000;
44

55
export { ONE_HOUR, ONE_MINUTE };

frontend/src/hooks/petProfile/usePetProfileEdition.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeEvent, useState } from 'react';
1+
import { ChangeEvent, useEffect, useState } from 'react';
22

33
import {
44
AGE_GROUP,
@@ -33,6 +33,10 @@ export const usePetProfileEdition = () => {
3333
const [isValidWeightInput, setIsValidWeightInput] = useState(true);
3434
const isValidForm = isValidNameInput && isValidAgeSelect && isValidWeightInput;
3535

36+
useEffect(() => {
37+
setPet(petItem);
38+
}, [petItem]);
39+
3640
const onChangeName = (e: ChangeEvent<HTMLInputElement>) => {
3741
const petName = e.target.value;
3842

frontend/src/hooks/query/food.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { useInfiniteQuery, useQuery } from '@tanstack/react-query';
33
import { getFoodDetail, getFoodList, getFoodListFilterMeta } from '@/apis/food';
44
import { Parameter } from '@/types/common/utility';
55

6-
const QUERY_KEY = {
6+
export const FOOD_QUERY_KEY = {
77
foodList: 'foodList',
8-
foodDetail: 'foodDetail',
8+
foodDetail: (petFoodId: string) => ['foodDetail', petFoodId],
99
petFoods: 'petFoods',
1010
foodListFilterMeta: 'foodListFilterMeta',
1111
};
@@ -14,7 +14,7 @@ const SIZE_PER_PAGE = 20;
1414

1515
export const useFoodListInfiniteQuery = (payload: Parameter<typeof getFoodList>) => {
1616
const { data, ...restQuery } = useInfiniteQuery({
17-
queryKey: [QUERY_KEY.petFoods, Object.values(payload).join()],
17+
queryKey: [FOOD_QUERY_KEY.petFoods, location.search],
1818
queryFn: ({ pageParam = { ...payload, size: String(SIZE_PER_PAGE) } }) =>
1919
getFoodList(pageParam),
2020
suspense: false,
@@ -38,7 +38,7 @@ export const useFoodListInfiniteQuery = (payload: Parameter<typeof getFoodList>)
3838

3939
export const useFoodDetailQuery = (payload: Parameter<typeof getFoodDetail>) => {
4040
const { data, ...restQuery } = useQuery({
41-
queryKey: [QUERY_KEY.foodDetail, payload.petFoodId],
41+
queryKey: FOOD_QUERY_KEY.foodDetail(payload.petFoodId),
4242
queryFn: () => getFoodDetail(payload),
4343
});
4444

@@ -50,7 +50,7 @@ export const useFoodDetailQuery = (payload: Parameter<typeof getFoodDetail>) =>
5050

5151
export const useFoodListFilterMetaQuery = () => {
5252
const { data, ...restQuery } = useQuery({
53-
queryKey: [QUERY_KEY.foodListFilterMeta],
53+
queryKey: [FOOD_QUERY_KEY.foodListFilterMeta],
5454
queryFn: getFoodListFilterMeta,
5555
});
5656
return {

frontend/src/hooks/query/petProfile.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ import {
1313
} from '@/types/petProfile/remote';
1414
import { zipgoLocalStorage } from '@/utils/localStorage';
1515

16-
const QUERY_KEY = { breedList: 'breedList', petItem: 'petItem', petList: 'petList' };
16+
const PET_PROFILE_QUERY_KEY = {
17+
breedList: 'breedList',
18+
petItem: (petId: number) => ['petItem', petId],
19+
petList: 'petList',
20+
};
1721

1822
export const useBreedListQuery = () => {
1923
const { data, ...restQuery } = useQuery({
20-
queryKey: [QUERY_KEY.breedList],
24+
queryKey: [PET_PROFILE_QUERY_KEY.breedList],
2125
queryFn: () => getBreeds(),
2226
});
2327

@@ -29,22 +33,19 @@ export const useBreedListQuery = () => {
2933

3034
export const usePetItemQuery = (payload: Parameter<typeof getPet>) => {
3135
const { data, ...restQuery } = useQuery({
32-
queryKey: [QUERY_KEY.petItem],
36+
queryKey: PET_PROFILE_QUERY_KEY.petItem(payload.petId),
3337
queryFn: () => getPet(payload),
3438
});
35-
const queryClient = useQueryClient();
36-
const resetPetItemQuery = () => queryClient.removeQueries([QUERY_KEY.petItem]);
3739

3840
return {
3941
petItem: data,
4042
...restQuery,
41-
resetPetItemQuery,
4243
};
4344
};
4445

4546
export const usePetListQuery = () => {
4647
const { data, ...restQuery } = useQuery({
47-
queryKey: [QUERY_KEY.petList],
48+
queryKey: [PET_PROFILE_QUERY_KEY.petList],
4849
queryFn: () => getPets(),
4950
});
5051

@@ -86,7 +87,7 @@ export const useAddPetMutation = () => {
8687
};
8788

8889
export const useEditPetMutation = () => {
89-
const { resetPetItemQuery } = usePetItemQuery({ petId: 0 });
90+
const queryClient = useQueryClient();
9091
const { petProfile: petProfileInHeader, updatePetProfile: updatePetProfileInHeader } =
9192
usePetProfile();
9293

@@ -100,7 +101,8 @@ export const useEditPetMutation = () => {
100101
onSuccess: (putPetRes, newPetProfile, context) => {
101102
if (newPetProfile.id === petProfileInHeader?.id) updatePetProfileInHeader(newPetProfile);
102103

103-
resetPetItemQuery();
104+
queryClient.invalidateQueries([PET_PROFILE_QUERY_KEY.petList]);
105+
queryClient.invalidateQueries(PET_PROFILE_QUERY_KEY.petItem(newPetProfile.id));
104106

105107
alert('반려동물 정보 수정이 완료되었습니다.');
106108
},
@@ -113,8 +115,9 @@ export const useEditPetMutation = () => {
113115
};
114116

115117
export const useRemovePetMutation = () => {
118+
const queryClient = useQueryClient();
116119
const { refetch: refetchPetList } = usePetListQuery();
117-
const { resetPetItemQuery } = usePetItemQuery({ petId: 0 });
120+
118121
const {
119122
petProfile: petProfileInHeader,
120123
updatePetProfile: updatePetProfileInHeader,
@@ -141,7 +144,8 @@ export const useRemovePetMutation = () => {
141144
if (!newestPet) resetPetProfileInHeader();
142145
}
143146

144-
resetPetItemQuery();
147+
queryClient.invalidateQueries([PET_PROFILE_QUERY_KEY.petList]);
148+
queryClient.invalidateQueries(PET_PROFILE_QUERY_KEY.petItem(deletePetReq.petId));
145149

146150
alert('반려동물 정보를 삭제했습니다.');
147151
},

frontend/src/hooks/query/review.ts

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { routerPath } from '@/router/routes';
1919
import { Parameter } from '@/types/common/utility';
2020
import { GetReviewsRes } from '@/types/review/remote';
2121

22+
import { FOOD_QUERY_KEY } from './food';
23+
2224
const QUERY_KEY = {
2325
reviewItem: 'reviewItem',
2426
reviewList: (petFoodId: string) => ['reviewList', petFoodId, location.search],
@@ -61,6 +63,7 @@ export const useAddReviewMutation = () => {
6163
onSuccess: (_, { petFoodId }) => {
6264
queryClient.invalidateQueries(QUERY_KEY.reviewList(petFoodId));
6365
queryClient.invalidateQueries([QUERY_KEY.reviewSummary]);
66+
queryClient.invalidateQueries(FOOD_QUERY_KEY.foodDetail(petFoodId));
6467

6568
alert('리뷰 작성이 완료되었습니다.');
6669

@@ -80,6 +83,7 @@ export const useEditReviewMutation = () => {
8083
onSuccess: (_, { petFoodId }) => {
8184
queryClient.invalidateQueries(QUERY_KEY.reviewList(petFoodId));
8285
queryClient.invalidateQueries([QUERY_KEY.reviewSummary]);
86+
queryClient.invalidateQueries(FOOD_QUERY_KEY.foodDetail(petFoodId));
8387

8488
alert('리뷰 수정이 완료되었습니다.');
8589

@@ -98,6 +102,7 @@ export const useRemoveReviewMutation = () => {
98102
onSuccess: (_, { petFoodId }) => {
99103
queryClient.invalidateQueries(QUERY_KEY.reviewList(petFoodId));
100104
queryClient.invalidateQueries([QUERY_KEY.reviewSummary]);
105+
queryClient.invalidateQueries(FOOD_QUERY_KEY.foodDetail(petFoodId));
101106
},
102107
});
103108

0 commit comments

Comments
 (0)