-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from FinalDoubleTen/FE-44--feat/ToursQA/ToursLike
인기여행지 좋아요 기능
- Loading branch information
Showing
6 changed files
with
67 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HTMLElement>) => { | ||
e.stopPropagation(); | ||
const token = localStorage.getItem('accessToken'); | ||
if (!token) { | ||
// 비로그인 알람창 처리 필요 | ||
navigate('/signin'); | ||
} else { | ||
if (liked === false) { | ||
likeMutate(id); | ||
} else { | ||
unlikeMutate(id); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<div | ||
onClick={onClickLikeButton} | ||
className="top-75 h-[24px] w-[24px] cursor-pointer"> | ||
<HeartIcon | ||
fill={liked ? '#FF2167' : '#D7D7D7'} | ||
color={liked ? '#ff2167' : '#ffffff'} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Like; |