From 9064f8d23d3ffa971e514889c19548d6d373a103 Mon Sep 17 00:00:00 2001 From: Abhishek Hegde Date: Fri, 24 Nov 2023 00:33:13 +0530 Subject: [PATCH 1/2] added api routes , added zod validation for titel which should contain atleast 1 string --- src/app/api/notes/route.ts | 37 +++++++++++++++++++++++++++++++++++++ src/lib/validation/note.ts | 8 ++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/app/api/notes/route.ts create mode 100644 src/lib/validation/note.ts diff --git a/src/app/api/notes/route.ts b/src/app/api/notes/route.ts new file mode 100644 index 0000000..06cbee3 --- /dev/null +++ b/src/app/api/notes/route.ts @@ -0,0 +1,37 @@ +import prisma from "@/lib/db/prisma"; +import { createNoteSchema } from "@/lib/validation/note"; +import { auth } from "@clerk/nextjs"; + +export async function POST(req: Request) { + try { + const body = await req.json(); + + const parseResult = createNoteSchema.safeParse(body); + + if (!parseResult.success) { + console.error(parseResult.error); + return Response.json({ error: "Invalid input" }, { status: 400 }); + } + + const { title, content } = parseResult.data; + + const { userId } = auth(); + + if (!userId) { + return Response.json({ error: "Unauthorized" }, { status: 401 }); + } + + const note = await prisma.note.create({ + data: { + title, + content, + userId, + }, + }); + + return Response.json({ note }, { status: 201 }); + } catch (error) { + console.error(error); + return Response.json({ error: "Internal server error" }, { status: 500 }); + } +} diff --git a/src/lib/validation/note.ts b/src/lib/validation/note.ts new file mode 100644 index 0000000..482b7e4 --- /dev/null +++ b/src/lib/validation/note.ts @@ -0,0 +1,8 @@ +import { z } from "zod"; + +export const createNoteSchema = z.object({ + title: z.string().min(1, { message: "Title is required" }), + content: z.string().optional(), +}); + +export type CreateNoteSchema = z.infer; From 0df2686907245fede0237396b394f87931751fb9 Mon Sep 17 00:00:00 2001 From: Abhishek Hegde Date: Fri, 24 Nov 2023 01:04:59 +0530 Subject: [PATCH 2/2] notes/ page.tsx error at 1.28 added shad cn react - hook - form with zod add note dialauge added --- src/components/AddNoteDialog.tsx | 103 +++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/components/AddNoteDialog.tsx diff --git a/src/components/AddNoteDialog.tsx b/src/components/AddNoteDialog.tsx new file mode 100644 index 0000000..cf3a4ad --- /dev/null +++ b/src/components/AddNoteDialog.tsx @@ -0,0 +1,103 @@ +import { CreateNoteSchema, createNoteSchema } from "@/lib/validation/note"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useRouter } from "next/navigation"; +import { useForm } from "react-hook-form"; +import { + Dialog, + DialogContent, + DialogFooter, + DialogHeader, + DialogTitle, +} from "./ui/dialog"; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "./ui/form"; +import { Input } from "./ui/input"; +import LoadingButton from "./ui/loading-button"; +import { Textarea } from "./ui/textarea"; + +interface AddNoteDialogProps { + open: boolean; + setOpen: (open: boolean) => void; +} + +export default function AddNoteDialog({ open, setOpen }: AddNoteDialogProps) { + const router = useRouter(); + + const form = useForm({ + resolver: zodResolver(createNoteSchema), + defaultValues: { + title: "", + content: "", + }, + }); + + async function onSubmit(input: CreateNoteSchema) { + try { + const response = await fetch("/api/notes", { + method: "POST", + body: JSON.stringify(input), + }); + if (!response.ok) throw Error("Status code: " + response.status); + form.reset(); + router.refresh(); + setOpen(false); + } catch (error) { + console.error(error); + alert("Something went wrong. Please try again."); + } + } + + return ( + + + + Add Note + +
+ + ( + + Note title + + + + + + )} + /> + ( + + Note content + +