diff --git a/server/notes.db b/server/notes.db index 9fb91df..4a2fd04 100644 Binary files a/server/notes.db and b/server/notes.db differ diff --git a/src/components/CreateNoteForm.jsx b/src/components/CreateNoteForm.jsx index ffb1252..b876f4e 100644 --- a/src/components/CreateNoteForm.jsx +++ b/src/components/CreateNoteForm.jsx @@ -1,25 +1,85 @@ -// TODO: Import useForm, zodResolver, axios, useNavigate, useState, and noteSchema - - +import { useForm } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; +import axios from "axios"; +import { useState } from "react"; +import { noteSchema } from "../schema/notes"; import { Save } from "lucide-react"; +import { useNavigate } from "react-router-dom"; -const CreateNoteForm = () => { - // TODO: Setup isSubmitting state with useState - // TODO: create navigate variable and set to useNavigate() - +const CreateNoteForm = ({ onSubmitSuccess }) => { + const [isSubmitting, setIsSubmitting] = useState(false); + const navigate = useNavigate() - // TODO: Set up the form with useForm from react-hook-form and zodResolver from @hookform/resolvers/zod + const { + register, + handleSubmit, + formState: { errors }, + } = useForm({ + resolver: zodResolver(noteSchema), + }); const sendToTheServer = async (data) => { - // TODO: Send the data to the server - // TODO: Use axios to create a new note in the server using the endpoint http://localhost:3001/api/notes + try { + setIsSubmitting(true); + await axios.post("http://localhost:3001/api/notes", data); + if (onSubmitSuccess) { + onSubmitSuccess(); // notify parent page + + } + setTimeout(() => { + navigate("/notes"); + }, 2000); + } catch (error) { + console.error("Error creating note:", error); + } finally { + setIsSubmitting(false); + } }; return ( - <> -