-
Notifications
You must be signed in to change notification settings - Fork 0
/
drafts.tsx
executable file
·57 lines (50 loc) · 1.42 KB
/
drafts.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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/client';
import prisma from '../lib/prisma';
import ErrorMessage from '../components/ErrorMessage';
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: React.FC<Props> = (props) => {
const [session] = useSession();
if (!session) {
return <ErrorMessage></ErrorMessage>;
}
return (
<Layout pageTitle="Les Petits Chatons - Drafts">
{' '}
<div className="pt-24">
{props.drafts.map((post) => (
<div key={post.id} className="post">
<Post post={post} />
</div>
))}
</div>
</Layout>
);
};
export default Drafts;