import React from "react"; import { GetServerSideProps } from "next"; import ReactMarkdown from "react-markdown"; import Layout from "../../components/Layout"; import Router from "next/router"; import { PostProps } from "../../components/Post"; import prisma from '../../lib/prisma' import { useSession } from "next-auth/react"; export const getServerSideProps: GetServerSideProps = async ({ params }) => { const post = await prisma.post.findUnique({ where: { id: Number(params?.id) || -1, }, include: { author: { select: { name: true, email: true }, }, }, }); return { props: post, }; }; async function publishPost(id: number): Promise { await fetch(`/api/publish/${id}`, { method: "PUT", }); await Router.push("/") } async function deletePost(id: number): Promise { await fetch(`/api/post/${id}`, { method: "DELETE", }); await Router.push("/") } const Post = (props : PostProps) => { const { data: session, status } = useSession(); if (status === 'loading') { return
Authenticating ...
; } const userHasValidSession = Boolean(session); const postBelongsToUser = session?.user?.email === props.author?.email; let title = props.title; if (!props.published) { title = `${title} (Draft)`; } return (

{title}

By {props?.author?.name || "Unknown author"}

{!props.published && userHasValidSession && postBelongsToUser && ( )} {userHasValidSession && postBelongsToUser && ( )}
); }; export default Post;