Skip to content

Commit

Permalink
refactor: 게더링 좋아요 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
joarthvr committed Dec 9, 2024
1 parent e7bdf7e commit 78aa4a9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/features/gathering/lib/hooks/useGatheringLike.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,28 @@ export const useGatheringLike = ({ gatheringId, onSuccess, onError }: UseGatheri
mutationFn: () => gatheringApi.toggleLike(gatheringId),

onMutate: async () => {
// 진행 중인 쿼리 취소
await queryClient.cancelQueries({
queryKey: ['/gatheringDetail', gatheringId],
});


const previousDetail = queryClient.getQueryData<GatheringDetailResponse>([
'/gatheringDetail',
gatheringId,
]);


if (previousDetail?.data) {
queryClient.setQueryData<GatheringDetailResponse>(['/gatheringDetail', gatheringId], {
...previousDetail,
data: {
...previousDetail.data,
isLiked: !previousDetail.data.isLiked,
},
});
}

return { previousDetail };
},

Expand All @@ -48,6 +61,7 @@ export const useGatheringLike = ({ gatheringId, onSuccess, onError }: UseGatheri
data: {
...currentDetail.data,
likeCounts: newLikeCounts,
isLiked: !!response.data,
},
});
}
Expand All @@ -56,6 +70,7 @@ export const useGatheringLike = ({ gatheringId, onSuccess, onError }: UseGatheri
},

onError: (error, _, context) => {

if (context?.previousDetail) {
queryClient.setQueryData(['/gatheringDetail', gatheringId], context.previousDetail);
}
Expand Down
1 change: 1 addition & 0 deletions src/features/gathering/model/dto/request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface GatheringDetailContent {
title: string;
content: string;
likeCounts: number;
isLiked: boolean;
status: '모집중' | '모집완료' | '기간만료';
}

Expand Down
6 changes: 5 additions & 1 deletion src/pages/GatheringDetailPage/GatheringDetailPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ export const GatheringDetailPage = () => {
</section>
<div className={styles.footer}>
<div className={styles.stats}>
<LikeBtn disabled={isPending} onLikeClick={toggleLike} />
<LikeBtn
disabled={isPending}
isLiked={gatheringDetail.isLiked}
onLikeClick={toggleLike}
/>
</div>
<GatheringDetailUserInfo username={gatheringDetail.username} />
<GatheringDetailBtnCon gatheringId={gatheringId} userId={gatheringDetail.userId} />
Expand Down

0 comments on commit 78aa4a9

Please sign in to comment.