Update: add update brewery post

This commit is contained in:
Aaron William Po
2023-07-04 23:27:01 -04:00
parent 232435e3d5
commit ee47f99f8a
4 changed files with 161 additions and 2 deletions

View File

@@ -0,0 +1,55 @@
import FormPageLayout from '@/components/ui/forms/FormPageLayout';
import getBreweryPostById from '@/services/BreweryPost/getBreweryPostById';
import BreweryPostQueryResult from '@/services/BreweryPost/types/BreweryPostQueryResult';
import withPageAuthRequired from '@/util/withPageAuthRequired';
import { NextPage } from 'next';
import Head from 'next/head';
import { BiBeer } from 'react-icons/bi';
import { z } from 'zod';
interface EditPageProps {
breweryPost: z.infer<typeof BreweryPostQueryResult>;
}
const EditBreweryPostPage: NextPage<EditPageProps> = ({ breweryPost }) => {
const pageTitle = `Edit \u201c${breweryPost.name}\u201d`;
return (
<>
<Head>
<title>{pageTitle}</title>
<meta name="description" content={pageTitle} />
</Head>
<FormPageLayout
headingText={pageTitle}
headingIcon={BiBeer}
backLink={`/breweries/${breweryPost.id}`}
backLinkText={`Back to "${breweryPost.name}"`}
>
<></>
</FormPageLayout>
</>
);
};
export default EditBreweryPostPage;
export const getServerSideProps = withPageAuthRequired<EditPageProps>(
async (context, session) => {
const breweryPostId = context.params?.id as string;
const breweryPost = await getBreweryPostById(breweryPostId);
const { id: userId } = session;
if (!breweryPost) {
return { notFound: true };
}
const isBreweryPostOwner = breweryPost.postedBy.id === userId;
return isBreweryPostOwner
? { props: { breweryPost: JSON.parse(JSON.stringify(breweryPost)) } }
: { redirect: { destination: `/breweries/${breweryPostId}`, permanent: false } };
},
);