From 04c0326eb557f067bfb1bed6d0d509f6926e457f Mon Sep 17 00:00:00 2001 From: sahramayo Date: Fri, 13 Jun 2025 23:32:13 -0500 Subject: [PATCH] update --- src/components/CreateNoteForm.jsx | 86 +++++++++++++++++++++++++++---- src/schema/notes.js | 15 ++++-- 2 files changed, 86 insertions(+), 15 deletions(-) diff --git a/src/components/CreateNoteForm.jsx b/src/components/CreateNoteForm.jsx index ffb1252..fd71ee5 100644 --- a/src/components/CreateNoteForm.jsx +++ b/src/components/CreateNoteForm.jsx @@ -1,24 +1,90 @@ -// TODO: Import useForm, zodResolver, axios, useNavigate, useState, and noteSchema - - +import React, { useState } from "react"; +import { useForm } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; +import axios from "axios"; +import { useNavigate } from "react-router-dom"; +import { noteSchema } from "../schema/notes"; // Adjust path if needed import { Save } from "lucide-react"; const CreateNoteForm = () => { - // TODO: Setup isSubmitting state with useState - // TODO: create navigate variable and set to useNavigate() + // ✅ Setup isSubmitting state + const [isSubmitting, setIsSubmitting] = useState(false); + // ✅ Setup navigate + const navigate = useNavigate(); - // TODO: Set up the form with useForm from react-hook-form and zodResolver from @hookform/resolvers/zod + // ✅ Set up useForm with zodResolver + const { + register, + handleSubmit, + formState: { errors }, + } = useForm({ + resolver: zodResolver(noteSchema), + }); + // ✅ Submit handler 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 { + await axios.post("http://localhost:3001/api/notes", data); + navigate("/notes"); + } catch (err) { + console.error("Error creating note:", err); + alert("Failed to create note."); + } finally { + setIsSubmitting(false); + } }; return ( <> -

Create Note

- {/* TODO: Setup the form with TailwindCSS, create a form with the following fields: title, content, and submit button */} +

Create Note

+ {/* ✅ Form with TailwindCSS */} +
+
+ + + {errors.title && ( +

+ {errors.title.message} +

+ )} +
+ +
+ +