import { GetServerSideProps, NextPage } from 'next'; import BeerPostQueryResult from '@/services/BeerPost/types/BeerPostQueryResult'; import getBeerPostById from '@/services/BeerPost/getBeerPostById'; import Layout from '@/components/ui/Layout'; import Head from 'next/head'; import Image from 'next/image'; import BeerInfoHeader from '@/components/BeerById/BeerInfoHeader'; import CommentCard from '@/components/BeerById/CommentCard'; import { useState } from 'react'; import { BeerPost } from '@prisma/client'; import BeerCommentQueryResult from '@/services/BeerPost/types/BeerCommentQueryResult'; import BeerCommentForm from '../../components/BeerById/BeerCommentForm'; import BeerRecommendations from '../../components/BeerById/BeerRecommendations'; import getBeerRecommendations from '../../services/BeerPost/getBeerRecommendations'; import getAllBeerComments from '../../services/BeerPost/getAllBeerComments'; interface BeerPageProps { beerPost: BeerPostQueryResult; beerRecommendations: (BeerPost & { brewery: { id: string; name: string; }; beerImages: { id: string; alt: string; url: string; }[]; })[]; beerComments: BeerCommentQueryResult[]; } const BeerByIdPage: NextPage = ({ beerPost, beerRecommendations, beerComments, }) => { const [comments, setComments] = useState(beerComments); return ( {beerPost.name}
{beerPost.beerImages[0] && ( {beerPost.beerImages[0].alt} )}
{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 }, { pageSize: 3, pageNum: 1 }); const beerRecommendations = await getBeerRecommendations({ type, brewery }); 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;