From a31a53ac8d196c0058519067641a6b48a45b2ec8 Mon Sep 17 00:00:00 2001 From: Matt Dyor Date: Wed, 27 Dec 2023 16:23:47 -0700 Subject: [PATCH] complete up to creating and showing a draft --- .vscode/launch.json | 21 +++++++++ components/Header.tsx | 52 ++++++++++----------- pages/api/auth/[...nextauth].ts | 19 ++++++++ pages/api/post/index.ts | 21 +++++++++ pages/create.tsx | 81 +++++++++++++++++++++++++++++++++ pages/drafts.tsx | 77 +++++++++++++++++++++++++++++++ prisma/schema.prisma | 60 ++++++++++++++++++++---- 7 files changed, 296 insertions(+), 35 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 pages/api/auth/[...nextauth].ts create mode 100644 pages/api/post/index.ts create mode 100644 pages/create.tsx create mode 100644 pages/drafts.tsx diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..713cdd0 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,21 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/pages/api/post/index.ts", + "outFiles": [ + "${workspaceFolder}/**/*.js" + ] + } + ] +} \ No newline at end of file diff --git a/components/Header.tsx b/components/Header.tsx index 0c1e054..b0fc004 100644 --- a/components/Header.tsx +++ b/components/Header.tsx @@ -1,19 +1,19 @@ -import React from 'react'; -import Link from 'next/link'; -import { useRouter } from 'next/router'; -import { signOut, useSession } from 'next-auth/react'; +import React from "react"; +import Link from "next/link"; +import { useRouter } from "next/router"; +import { signOut, useSession } from "next-auth/react"; const Header: React.FC = () => { const router = useRouter(); const isActive: (pathname: string) => boolean = (pathname) => router.pathname === pathname; - const { data: session, status } = useSession(); + const {data: session, status} = useSession(); let left = (
- - + + Feed @@ -24,11 +24,11 @@ const Header: React.FC = () => { a { text-decoration: none; - color: var(--geist-foreground); + color: #000; display: inline-block; } - .left a[data-active='true'] { + .left a[data-active="true"] { color: gray; } @@ -44,8 +44,8 @@ const Header: React.FC = () => { if (status === 'loading') { left = (
- - + + Feed @@ -56,11 +56,11 @@ const Header: React.FC = () => { a { text-decoration: none; - color: var(--geist-foreground); + color: #000; display: inline-block; } - .left a[data-active='true'] { + .left a[data-active="true"] { color: gray; } @@ -85,13 +85,13 @@ const Header: React.FC = () => { if (!session) { right = (
- - Log in + + Log in + + ); +}; + +export default Draft; \ No newline at end of file diff --git a/pages/drafts.tsx b/pages/drafts.tsx new file mode 100644 index 0000000..35643e1 --- /dev/null +++ b/pages/drafts.tsx @@ -0,0 +1,77 @@ +import React from 'react'; +import { GetServerSideProps } from 'next'; +import { useSession, getSession } from 'next-auth/react'; +import Layout from '../components/Layout'; +import Post, { PostProps } from '../components/Post'; +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: React.FC = (props) => { + const { data: session } = useSession(); + + if (!session) { + return ( + +

My Drafts

+
You need to be authenticated to view this page.
+
+ ); + } + + return ( + +
+

My Drafts

+
+ {props.drafts.map((post) => ( +
+ +
+ ))} +
+
+ +
+ ); +}; + +export default Drafts; \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ad75c07..c343a67 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -10,21 +10,63 @@ datasource db { directUrl = env("POSTGRES_URL_NON_POOLING") // uses a direct connection } +// schema.prisma + +// schema.prisma model Post { - id String @default(cuid()) @id + id String @id @default(cuid()) title String content String? published Boolean @default(false) - author User? @relation(fields: [authorId], references: [id]) + author User?@relation(fields:[authorId], references:[id]) authorId String? -} + } + +model Account { + id String @id @default(cuid()) + userId String @map("user_id") + type String + provider String + providerAccountId String @map("provider_account_id") + refresh_token String? + access_token String? + expires_at Int? + token_type String? + scope String? + id_token String? + session_state String? + oauth_token_secret String? + oauth_token String? + + user User @relation(fields:[userId], references:[id], onDelete: Cascade) + + @@unique([provider, providerAccountId]) + } + +model Session { + id String @id @default(cuid()) + sessionToken String @unique@map("session_token") + userId String @map("user_id") + expires DateTime + user User @relation(fields:[userId], references:[id], onDelete: Cascade) + } model User { - id String @default(cuid()) @id + id String @id @default(cuid()) name String? - email String? @unique - createdAt DateTime @default(now()) @map(name: "created_at") - updatedAt DateTime @updatedAt @map(name: "updated_at") + email String?@unique + emailVerified DateTime? + image String? posts Post[] - @@map(name: "users") -} \ No newline at end of file + accounts Account[] + sessions Session[] + } + +model VerificationToken { + id Int @id @default(autoincrement()) + identifier String + token String @unique + expires DateTime + + @@unique([identifier, token]) + }