diff --git a/README.md b/README.md index 71843a6..cfa2ed5 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ npm install npm run dev ``` -6. Create a new branch: git checkout -b ``. Implement the project on your newly created `` branch, committing changes regularly. +6. Create a new branch: . Implement the project on your newly created `` branch, committing changes regularly. 7. Push commits: git push origin ``. ### Task 2: MVP diff --git a/server/notes.db b/server/notes.db index 9fb91df..58e9716 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..9bf8fdf 100644 --- a/src/components/CreateNoteForm.jsx +++ b/src/components/CreateNoteForm.jsx @@ -1,25 +1,143 @@ // 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"; import { Save } from "lucide-react"; -const CreateNoteForm = () => { - // TODO: Setup isSubmitting state with useState - // TODO: create navigate variable and set to useNavigate() +// TODO: Setup isSubmitting state with useState +// TODO: create navigate variable and set to useNavigate() +const CreateNoteForm = ({ onSubmitSuccess }) => { + const [isSubmitting, setIsSubmitting] = useState(false); + const [submitSuccess, setSubmitSuccess] = useState(false); + const [submitError, setSubmitError] = useState(""); // TODO: Set up the form with useForm from react-hook-form and zodResolver from @hookform/resolvers/zod + const { + register, + handleSubmit, + formState: { errors, isValid, isDirty }, + reset, + } = useForm({ + mode: "onChange", + resolver: zodResolver(noteSchema), + defaultValues: { + title: "", + content: "", + }, + }); + 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); + setSubmitSuccess(false); + setSubmitError(""); + + try { + await axios.post("http://localhost:3001/api/notes", data, { + headers: { "Content-Type": "application/json" }, + }); + setSubmitSuccess(true); + reset(); + onSubmitSuccess?.(); + setTimeout(() => { + setSubmitSuccess(false); + }, 5000); + } catch (error) { + setSubmitError( + "There was an error submitting your form. Please try again." + ); + console.error("Form submission error:", error); + } finally { + setIsSubmitting(false); + } }; return ( - <> -

Create Note

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

Create a New Note

+ + {submitSuccess && ( +
+ Note created successfully! +
+ )} + + {submitError && ( +
+ {submitError} +
+ )} + +
+
+ + + {errors.title && ( +

{errors.title.message}

+ )} +
+ +
+ +