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 + +