Files
the-biergarten-app/components/BeerById/BeerPostLikeButton.tsx
Aaron William Po a4362a531c Add custom hooks for time distance and retrieving like count
Documentation added to all custom hooks
2023-04-03 23:32:32 -04:00

53 lines
1.3 KiB
TypeScript

import useCheckIfUserLikesBeerPost from '@/hooks/useCheckIfUserLikesBeerPost';
import sendLikeRequest from '@/requests/sendLikeRequest';
import { FC, useState } from 'react';
import { FaThumbsUp, FaRegThumbsUp } from 'react-icons/fa';
import { KeyedMutator } from 'swr';
const BeerPostLikeButton: FC<{
beerPostId: string;
mutateCount: KeyedMutator<number>;
}> = ({ beerPostId, mutateCount }) => {
const { isLiked, mutate: mutateLikeStatus } = useCheckIfUserLikesBeerPost(beerPostId);
const [loading, setLoading] = useState(false);
const handleLike = async () => {
try {
setLoading(true);
await sendLikeRequest(beerPostId);
mutateCount();
mutateLikeStatus();
setLoading(false);
} catch (e) {
setLoading(false);
}
};
return (
<button
type="button"
className={`btn gap-2 rounded-2xl ${
!isLiked ? 'btn-ghost outline' : 'btn-primary'
}`}
onClick={() => {
handleLike();
}}
disabled={loading}
>
{isLiked ? (
<>
<FaThumbsUp className="text-2xl" />
Liked
</>
) : (
<>
<FaRegThumbsUp className="text-2xl" />
Like
</>
)}
</button>
);
};
export default BeerPostLikeButton;