mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Continue work on brewery page, implement like system
This commit is contained in:
43
src/components/BreweryIndex/BreweryCard.tsx
Normal file
43
src/components/BreweryIndex/BreweryCard.tsx
Normal file
@@ -0,0 +1,43 @@
|
||||
import UserContext from '@/contexts/userContext';
|
||||
import useGetBreweryPostLikeCount from '@/hooks/useGetBreweryPostLikeCount';
|
||||
import BreweryPostQueryResult from '@/services/BreweryPost/types/BreweryPostQueryResult';
|
||||
import { FC, useContext } from 'react';
|
||||
import { Link } from 'react-daisyui';
|
||||
import { z } from 'zod';
|
||||
import Image from 'next/image';
|
||||
import BreweryPostLikeButton from './BreweryPostLikeButton';
|
||||
|
||||
const BreweryCard: FC<{ brewery: z.infer<typeof BreweryPostQueryResult> }> = ({
|
||||
brewery,
|
||||
}) => {
|
||||
const { user } = useContext(UserContext);
|
||||
const { likeCount, mutate } = useGetBreweryPostLikeCount(brewery.id);
|
||||
return (
|
||||
<div className="card" key={brewery.id}>
|
||||
<figure className="card-image h-96">
|
||||
{brewery.breweryImages.length > 0 && (
|
||||
<Image
|
||||
src={brewery.breweryImages[0].path}
|
||||
alt={brewery.name}
|
||||
width="1029"
|
||||
height="110"
|
||||
/>
|
||||
)}
|
||||
</figure>
|
||||
<div className="card-body space-y-3">
|
||||
<div>
|
||||
<h2 className="text-3xl font-bold">
|
||||
<Link href={`/breweries/${brewery.id}`}>{brewery.name}</Link>
|
||||
</h2>
|
||||
<h3 className="text-xl font-semibold">{brewery.location}</h3>
|
||||
</div>
|
||||
liked by {likeCount} users
|
||||
{user && (
|
||||
<BreweryPostLikeButton breweryPostId={brewery.id} mutateCount={mutate} />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default BreweryCard;
|
||||
30
src/components/BreweryIndex/BreweryPostLikeButton.tsx
Normal file
30
src/components/BreweryIndex/BreweryPostLikeButton.tsx
Normal file
@@ -0,0 +1,30 @@
|
||||
import useCheckIfUserLikesBreweryPost from '@/hooks/useCheckIfUserLikesBreweryPost';
|
||||
import useGetBreweryPostLikeCount from '@/hooks/useGetBreweryPostLikeCount';
|
||||
import sendBreweryPostLikeRequest from '@/requests/sendBreweryPostLikeRequest';
|
||||
import { FC, useState } from 'react';
|
||||
import LikeButton from '../ui/LikeButton';
|
||||
|
||||
const BreweryPostLikeButton: FC<{
|
||||
breweryPostId: string;
|
||||
mutateCount: ReturnType<typeof useGetBreweryPostLikeCount>['mutate'];
|
||||
}> = ({ breweryPostId, mutateCount }) => {
|
||||
const { isLiked, mutate: mutateLikeStatus } =
|
||||
useCheckIfUserLikesBreweryPost(breweryPostId);
|
||||
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
const handleLike = async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
await sendBreweryPostLikeRequest(breweryPostId);
|
||||
await Promise.all([mutateCount(), mutateLikeStatus()]);
|
||||
setIsLoading(false);
|
||||
} catch (e) {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return <LikeButton isLiked={!!isLiked} handleLike={handleLike} loading={isLoading} />;
|
||||
};
|
||||
|
||||
export default BreweryPostLikeButton;
|
||||
Reference in New Issue
Block a user