diff --git a/server/notes.db b/server/notes.db index 9fb91df..3907916 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..4846c26 100644 --- a/src/components/CreateNoteForm.jsx +++ b/src/components/CreateNoteForm.jsx @@ -1,25 +1,98 @@ // TODO: Import useForm, zodResolver, axios, useNavigate, useState, and noteSchema +import {useForm} from "react-hook-form" +import axios from "axios" +import {zodResolver} from "@hookform/resolvers/zod" +import { useState } from "react"; +import {noteSchema} from "../schema/notes" +import {useNavigate} from "react-router-dom" + + + + import { Save } from "lucide-react"; const CreateNoteForm = () => { + + + // TODO: Setup isSubmitting state with useState + + 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}, + reset} = useForm ({ + resolver: zodResolver(noteSchema), + + defaultValues: {title: "", content: ""} + }) + const sendToTheServer = async (data) => { // TODO: Send the data to the server + setIsSubmitting(true) + try { + await axios.post("http://localhost:3001/api/notes", data, + + {headers: { + "content-type": "application/json" + }} + ) + + reset() + setTimeout (() => { + navigate("/notes") + }, 3000) + + + }catch(err) { + console.log(err) + + }finally { + setIsSubmitting(false) + } + + // TODO: Use axios to create a new note in the server using the endpoint http://localhost:3001/api/notes + + }; return ( - <> -

Create Note

+
+

Create a New Note

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

{errors.title.message}

} + + + + + {errors.content &&

{errors.content.message}

} + + + + +
+ +
+ ); }; diff --git a/src/schema/notes.js b/src/schema/notes.js index 4621c8c..72ecbe7 100644 --- a/src/schema/notes.js +++ b/src/schema/notes.js @@ -4,5 +4,14 @@ export const noteSchema = z.object({ //TODO: create the title and content schema, // Make sure the title is required and the content is required // Make sure the title is max 50 characters and the content is max 500 characters + title: z + .string().min(10, "Title must be at least 10 characters") + .max(50, "Title must be less than 50 characters"), + content: z + .string() + .min(50, "Content must be at least 50 characters") + .max(500, "Content must be less than 500 characters") - }); \ No newline at end of file + }); + + export default noteSchema \ No newline at end of file