diff --git a/src/components/CreateNoteForm.jsx b/src/components/CreateNoteForm.jsx index ffb1252..b4a466c 100644 --- a/src/components/CreateNoteForm.jsx +++ b/src/components/CreateNoteForm.jsx @@ -1,25 +1,91 @@ // TODO: Import useForm, zodResolver, axios, useNavigate, useState, and noteSchema - - +import { useForm } from "react-hook-form"; +import { z } from "zod"; +import { zodResolver } from "@hookform/resolvers/zod"; +import axios from "axios"; +import { useNavigate } from "react-router-dom"; +import { useState } from "react"; import { Save } from "lucide-react"; +// Zod validation schema +const noteSchema = z.object({ + title: z.string().min(1, "Title is required"), + content: z.string().min(1, "Content is required"), +}); + const CreateNoteForm = () => { // TODO: Setup isSubmitting state with useState - // TODO: create navigate variable and set to useNavigate() + const [isSubmitting, setIsSubmitting] = useState(false); + // TODO: create navigate variable and set to useNavigate() + 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 + setIsSubmitting(true); + try { + const response = await axios.post("http://localhost:3001/api/notes", data); + console.log("response", response.data); + navigate("/view-all"); + } catch (error) { + console.error("Error creating note:", error); + } finally { + setIsSubmitting(false); + } }; return ( - <> -
+ +
+ + {/* TODO: Setup the form with TailwindCSS, create a form with the following fields: title, content, and submit button */} + +