mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Uses react-intersection-observer to load more comments when the last of the previously loaded comments is in the viewport.
58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import useCheckIfUserLikesBeerPost from '@/hooks/useCheckIfUserLikesBeerPost';
|
|
import sendLikeRequest from '@/requests/sendLikeRequest';
|
|
import { FC, useEffect, useState } from 'react';
|
|
import { FaThumbsUp, FaRegThumbsUp } from 'react-icons/fa';
|
|
|
|
import useGetLikeCount from '@/hooks/useGetLikeCount';
|
|
|
|
const BeerPostLikeButton: FC<{
|
|
beerPostId: string;
|
|
mutateCount: ReturnType<typeof useGetLikeCount>['mutate'];
|
|
}> = ({ beerPostId, mutateCount }) => {
|
|
const { isLiked, mutate: mutateLikeStatus } = useCheckIfUserLikesBeerPost(beerPostId);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
setLoading(false);
|
|
}, [isLiked]);
|
|
|
|
const handleLike = async () => {
|
|
try {
|
|
setLoading(true);
|
|
await sendLikeRequest(beerPostId);
|
|
await mutateCount();
|
|
await 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;
|