diff --git a/package-lock.json b/package-lock.json index 70de9dc..e5aec89 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,7 @@ "name": "sticky-notes-app", "version": "0.0.0", "dependencies": { - "@hookform/resolvers": "^3.3.4", + "@hookform/resolvers": "^3.10.0", "axios": "^1.9.0", "lucide-react": "^0.344.0", "react": "^18.3.1", diff --git a/package.json b/package.json index cfcfc8e..79c37ed 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "preview": "vite preview" }, "dependencies": { - "@hookform/resolvers": "^3.3.4", + "@hookform/resolvers": "^3.10.0", "axios": "^1.9.0", "lucide-react": "^0.344.0", "react": "^18.3.1", diff --git a/server/notes.db b/server/notes.db index 9fb91df..a840e7e 100644 Binary files a/server/notes.db and b/server/notes.db differ diff --git a/src/App.jsx b/src/App.jsx index 0efe0cf..140ab2b 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -11,7 +11,7 @@ function App() {
} /> - } /> + } />
diff --git a/src/components/CreateNoteForm.jsx b/src/components/CreateNoteForm.jsx index ffb1252..0907bd7 100644 --- a/src/components/CreateNoteForm.jsx +++ b/src/components/CreateNoteForm.jsx @@ -1,25 +1,108 @@ // TODO: Import useForm, zodResolver, axios, useNavigate, useState, and noteSchema +import { useForm } from "react-hook-form"; +import axios from "axios" +import { useState } from "react"; +import { zodResolver } from "@hookform/resolvers/zod"; +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() + 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 + + // 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 + + const sendToTheServer = async (data) => { + setIsSubmitting(true); + try { + await axios.post("http://localhost:3001/api/notes", data, + { headers: { + "Content-Type": "application/json" + }} + ) + reset() + setTimeout(() => { + navigate("/ViewNotes") + }, 2000); + + } catch (err) { + console.log({"Error": err}) + } + }; + return ( - <> -

Create Note

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

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/pages/CreateNote.jsx b/src/pages/CreateNote.jsx index cc0d827..e31a28e 100644 --- a/src/pages/CreateNote.jsx +++ b/src/pages/CreateNote.jsx @@ -3,14 +3,14 @@ import CreateNoteForm from "../components/CreateNoteForm"; const CreateNote = () => { return (
-
+

Create a New Sticky Note

Jot down your thoughts, ideas, or reminders

-
+
diff --git a/src/schema/notes.js b/src/schema/notes.js index 4621c8c..5ef2143 100644 --- a/src/schema/notes.js +++ b/src/schema/notes.js @@ -1,8 +1,22 @@ import { z } from "zod"; - -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 - }); \ No newline at end of file + + + const noteSchema = z.object({ + title: z + .string() + .min(2, "Make sure the title is required") + .max(50, "Title cannot be longer then 50 characters"), + + content: z + .string() + .min(2, "Make sure the content is required") + .max(500, "content cannot be longer then 500 characters") + + + }); + + export default noteSchema; \ No newline at end of file