import BeerCommentForm from '@/components/BeerById/BeerCommentForm'; import BeerInfoHeader from '@/components/BeerById/BeerInfoHeader'; import BeerRecommendations from '@/components/BeerById/BeerRecommendations'; import CommentCard from '@/components/BeerById/CommentCard'; import Layout from '@/components/ui/Layout'; import getAllBeerComments from '@/services/BeerComment/getAllBeerComments'; import { BeerCommentQueryResultArrayT } from '@/services/BeerComment/schema/BeerCommentQueryResult'; import getBeerPostById from '@/services/BeerPost/getBeerPostById'; import getBeerRecommendations from '@/services/BeerPost/getBeerRecommendations'; import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult'; import { BeerPost } from '@prisma/client'; import { NextPage, GetServerSideProps } from 'next'; import Head from 'next/head'; import Image from 'next/image'; import { useState, useEffect, useContext } from 'react'; import UserContext from '../contexts/userContext'; interface BeerPageProps { beerPost: BeerPostQueryResult; beerRecommendations: (BeerPost & { brewery: { id: string; name: string; }; beerImages: { id: string; alt: string; url: string; }[]; })[]; beerComments: BeerCommentQueryResultArrayT; } const BeerByIdPage: NextPage = ({ beerPost, beerRecommendations, beerComments, }) => { const { user } = useContext(UserContext); const [comments, setComments] = useState(beerComments); useEffect(() => { setComments(beerComments); }, [beerComments]); return ( {beerPost.name}
{beerPost.beerImages[0] && ( {beerPost.beerImages[0].alt} )}
{user ? ( ) : (
Log in to leave a comment.
)}
{comments.map((comment) => ( ))}
); }; export const getServerSideProps: GetServerSideProps = async (context) => { const beerPost = await getBeerPostById(context.params!.id! as string); if (!beerPost) { return { notFound: true }; } const { type, brewery, id } = beerPost; const beerComments = await getAllBeerComments( { id: beerPost.id }, { pageSize: 9, pageNum: 1 }, ); const beerRecommendations = await getBeerRecommendations({ type, brewery, id }); const props = { beerPost: JSON.parse(JSON.stringify(beerPost)), beerRecommendations: JSON.parse(JSON.stringify(beerRecommendations)), beerComments: JSON.parse(JSON.stringify(beerComments)), }; return { props }; }; export default BeerByIdPage;