You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.6 KiB
79 lines
1.6 KiB
import React from "react";
|
|
import { GetServerSideProps } from "next";
|
|
import Layout from "../components/Layout";
|
|
import Post, { PostProps } from "../components/Post";
|
|
import { useSession, getSession } from "next-auth/react";
|
|
import prisma from '../lib/prisma'
|
|
|
|
|
|
export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
|
|
const session = await getSession({ req });
|
|
if (!session) {
|
|
res.statusCode = 403;
|
|
return { props: { drafts: [] } };
|
|
}
|
|
|
|
const drafts = await prisma.post.findMany({
|
|
where: {
|
|
author: { email: session.user.email },
|
|
published: false,
|
|
},
|
|
include: {
|
|
author: {
|
|
select: { name: true },
|
|
},
|
|
},
|
|
});
|
|
return {
|
|
props: { drafts },
|
|
};
|
|
};
|
|
|
|
type Props = {
|
|
drafts: PostProps[];
|
|
};
|
|
|
|
const Drafts = (props : Props) => {
|
|
const {data: session}= useSession();
|
|
|
|
if (!session) {
|
|
return (
|
|
<Layout>
|
|
<h1>My Drafts</h1>
|
|
<div>You need to be authenticated to view this page.</div>
|
|
</Layout>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Layout>
|
|
<div className="page">
|
|
<h1>My Drafts</h1>
|
|
<main>
|
|
{props.drafts.map((post) => (
|
|
<div key={post.id} className="post">
|
|
<Post post={post} />
|
|
</div>
|
|
))}
|
|
</main>
|
|
</div>
|
|
<style jsx>{`
|
|
.post {
|
|
background: white;
|
|
transition: box-shadow 0.1s ease-in;
|
|
}
|
|
|
|
.post:hover {
|
|
box-shadow: 1px 1px 3px #aaa;
|
|
}
|
|
|
|
.post + .post {
|
|
margin-top: 2rem;
|
|
}
|
|
`}</style>
|
|
</Layout>
|
|
);
|
|
};
|
|
|
|
export default Drafts;
|