From 0df2686907245fede0237396b394f87931751fb9 Mon Sep 17 00:00:00 2001 From: Abhishek Hegde Date: Fri, 24 Nov 2023 01:04:59 +0530 Subject: [PATCH] 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 + +