diff --git a/src/@types/detail.types.ts b/src/@types/detail.types.ts index 8c65e79c..bfb4bca8 100644 --- a/src/@types/detail.types.ts +++ b/src/@types/detail.types.ts @@ -9,3 +9,8 @@ interface tourDetail { tel: string; originalThumbnailUrl: string; } + +interface LikeProps { + liked: boolean; + id: number; +} diff --git a/src/components/DetailSectionTop/DetailToursInfo.tsx b/src/components/DetailSectionTop/DetailToursInfo.tsx index c4a0edf9..8af2a5ee 100644 --- a/src/components/DetailSectionTop/DetailToursInfo.tsx +++ b/src/components/DetailSectionTop/DetailToursInfo.tsx @@ -1,7 +1,4 @@ -import { HeartIcon } from '@components/common/icons/Icons'; -import { postLikedTours, deleteLikedTours } from '@api/tours'; -import { useMutation, useQueryClient } from '@tanstack/react-query'; -import { useNavigate } from 'react-router-dom'; +import Like from '@components/common/like/Like'; interface DetailToursInfoProps { infoData: tourDetail; @@ -10,39 +7,6 @@ interface DetailToursInfoProps { export default function DetailToursInfo({ infoData }: DetailToursInfoProps) { const { title, liked, originalThumbnailUrl, id } = infoData; - const navigate = useNavigate(); - const queryClient = useQueryClient(); - - const { mutate: likeMutate } = useMutation({ - mutationFn: (id: number) => postLikedTours({ id }), - onSuccess: () => { - queryClient.invalidateQueries({ queryKey: ['details'] }); - }, - onError: () => console.log('error'), - }); - - const { mutate: unlikeMutate } = useMutation({ - mutationFn: (id: number) => deleteLikedTours({ id }), - onSuccess: () => { - queryClient.invalidateQueries({ queryKey: ['details'] }); - }, - onError: () => console.log('error'), - }); - - const onClickLikeButton = () => { - const token = localStorage.getItem('accessToken'); - if (!token) { - // 비로그인 알람창 처리 필요 - navigate('/signin'); - } else { - if (liked === false) { - likeMutate(id); - } else { - unlikeMutate(id); - } - } - }; - return ( <>
@@ -56,13 +20,7 @@ export default function DetailToursInfo({ infoData }: DetailToursInfoProps) {

{title}

-
- -
+
); diff --git a/src/components/Tours/ToursCategory.tsx b/src/components/Tours/ToursCategory.tsx index d4e3a3fd..8578ddda 100644 --- a/src/components/Tours/ToursCategory.tsx +++ b/src/components/Tours/ToursCategory.tsx @@ -30,7 +30,7 @@ const ToursCategory = ({ }, [isLoading]); if (error) { - console.log('error - 예외 처리'); + console.error('error'); } const handleSelectRegion = (name: string) => { diff --git a/src/components/Tours/ToursItem.tsx b/src/components/Tours/ToursItem.tsx index df049fb2..41c1abd6 100644 --- a/src/components/Tours/ToursItem.tsx +++ b/src/components/Tours/ToursItem.tsx @@ -1,5 +1,6 @@ import { TourType } from '@/@types/tours.types'; import { HeartIcon, StarIcon } from '@components/common/icons/Icons'; +import Like from '@components/common/like/Like'; import { useNavigate } from 'react-router-dom'; const ToursItem = ({ tour }: { tour: TourType }) => { @@ -26,11 +27,7 @@ const ToursItem = ({ tour }: { tour: TourType }) => { alt="여행지 이미지" />
- +

diff --git a/src/components/common/icons/Icons.tsx b/src/components/common/icons/Icons.tsx index 83536fc9..73b09c65 100644 --- a/src/components/common/icons/Icons.tsx +++ b/src/components/common/icons/Icons.tsx @@ -769,8 +769,8 @@ export const KakaoIcon = () => { xmlns="http://www.w3.org/2000/svg"> diff --git a/src/components/common/like/Like.tsx b/src/components/common/like/Like.tsx new file mode 100644 index 00000000..e47b1730 --- /dev/null +++ b/src/components/common/like/Like.tsx @@ -0,0 +1,55 @@ +import { useNavigate } from 'react-router-dom'; +import { HeartIcon } from '../icons/Icons'; +import { useMutation, useQueryClient } from '@tanstack/react-query'; +import { deleteLikedTours, postLikedTours } from '@api/tours'; + +const Like = ({ liked, id }: LikeProps) => { + const navigate = useNavigate(); + const queryClient = useQueryClient(); + + const { mutate: likeMutate } = useMutation({ + mutationFn: (id: number) => postLikedTours({ id }), + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ['details'] }); + queryClient.invalidateQueries({ queryKey: ['tours'] }); + }, + onError: () => console.log('error'), + }); + + const { mutate: unlikeMutate } = useMutation({ + mutationFn: (id: number) => deleteLikedTours({ id }), + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ['details'] }); + queryClient.invalidateQueries({ queryKey: ['tours'] }); + }, + onError: () => console.log('error'), + }); + + const onClickLikeButton = (e: React.MouseEvent) => { + e.stopPropagation(); + const token = localStorage.getItem('accessToken'); + if (!token) { + // 비로그인 알람창 처리 필요 + navigate('/signin'); + } else { + if (liked === false) { + likeMutate(id); + } else { + unlikeMutate(id); + } + } + }; + + return ( +

+ +
+ ); +}; + +export default Like;