import { NextPage } from 'next'; import Head from 'next/head'; import React from 'react'; import withPageAuthRequired from '@/util/withPageAuthRequired'; import BeerPostQueryResult from '@/services/posts/beer-post/schema/BeerPostQueryResult'; import EditBeerPostForm from '@/components/EditBeerPostForm'; import FormPageLayout from '@/components/ui/forms/FormPageLayout'; import { BiBeer } from 'react-icons/bi'; import { z } from 'zod'; import { getBeerPostById } from '@/services/posts/beer-post'; interface EditPageProps { beerPost: z.infer; } const EditBeerPostPage: NextPage = ({ beerPost }) => { const pageTitle = `Edit \u201c${beerPost.name}\u201d`; return ( <> {pageTitle} ); }; export default EditBeerPostPage; export const getServerSideProps = withPageAuthRequired( async (context, session) => { const beerPostId = context.params?.id as string; const beerPost = await getBeerPostById({ beerPostId }); const { id: userId } = session; if (!beerPost) { return { notFound: true }; } const isBeerPostOwner = beerPost.postedBy.id === userId; return isBeerPostOwner ? { props: { beerPost: JSON.parse(JSON.stringify(beerPost)) } } : { redirect: { destination: `/beers/${beerPostId}`, permanent: false } }; }, );