From 670b5b01f2901bd41f168927d93f862711845ae9 Mon Sep 17 00:00:00 2001 From: saynis Date: Mon, 1 Sep 2025 17:51:28 +0300 Subject: [PATCH] waan soo diray macalin --- server/notes.db | Bin 16384 -> 16384 bytes src/components/CreateNoteForm.jsx | 90 +++++++++++++++++++++++++++++- src/schema/notes.js | 2 + 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/server/notes.db b/server/notes.db index 9fb91df1bfcf0c2fe714e14a2690badbfe389499..2f04ec79b7e80195abf83eb7ac4e040a82f5be11 100644 GIT binary patch delta 290 zcmZo@U~Fh$oFL7}H&Mo!k#A$d5`HcweqRRuS^U2I!kYyJ-tpD@v#~Mo+uGU|r=%BW zBxa>#q!njmC1zW&1BHR&#l=PjMy9$3mbwOpA%-SaCgxVg#(I{<7E!pwy;<29WYukL z^YTkl6-vtUOENNx6<{1xO~#g1^2X+R#)c+nrU50O;+tp5rwZ`1b1^V5F!3K_;D5}2 kjDO{3L511;b~4PijHu=z9LJ6+i7=BDBsuwlyaXc)0Haz&E&u=k delta 64 zcmZo@U~Fh$oFL7}IZ?)$k#l3h5`H#D{#gwCvo;GVOyJ)p4tB^X%%BNq?- diff --git a/src/components/CreateNoteForm.jsx b/src/components/CreateNoteForm.jsx index ffb1252..e0832b6 100644 --- a/src/components/CreateNoteForm.jsx +++ b/src/components/CreateNoteForm.jsx @@ -1,24 +1,110 @@ // TODO: Import useForm, zodResolver, axios, useNavigate, useState, and noteSchema - +import { useForm } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; +import axios from "axios"; +import { useNavigate } from "react-router-dom"; import { Save } from "lucide-react"; +import { noteSchema } from "../schema/notes"; +import { useState } from "react"; const CreateNoteForm = () => { // TODO: Setup isSubmitting state with useState // TODO: create navigate variable and set to useNavigate() + const [isSubmitting, setIsSubmitting] = useState(false); + 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 + try { + setIsSubmitting(true); + await axios.post("http://localhost:3001/api/notes", data, + { + headers: { + "Content-Type": "application/json", + }, + } + ); + navigate("/notes"); // redirect to notes list page + } catch (error) { + console.error("Error saving note:", 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 */} +
+
+

+ Create a New Note +

+ +
+ {/* Title */} +
+ + + {errors.title && ( +

{errors.title.message}

+ )} +
+ + {/* Content */} +
+ + + {errors.content && ( +

{errors.content.message}

+ )} +
+ + {/* Submit Button */} + +
+
+
); }; diff --git a/src/schema/notes.js b/src/schema/notes.js index 4621c8c..b88eb83 100644 --- a/src/schema/notes.js +++ b/src/schema/notes.js @@ -4,5 +4,7 @@ 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(2, "Title is required").max(50, "Title is too long"), + content: z.string().min(2, "Content is required").max(500, "Content is too long"), }); \ No newline at end of file