From b73e51ad84fd769bfbdb4621f9bd9631a275c1ad Mon Sep 17 00:00:00 2001 From: naqib-axmed Date: Sun, 7 Sep 2025 22:19:37 +1300 Subject: [PATCH] assignment 13 or week 3 react --- server/notes.db | Bin 16384 -> 16384 bytes src/components/CreateNoteForm.jsx | 79 ++++++++++++++++++++++++++++-- src/schema/notes.js | 11 ++++- 3 files changed, 86 insertions(+), 4 deletions(-) diff --git a/server/notes.db b/server/notes.db index 9fb91df1bfcf0c2fe714e14a2690badbfe389499..3907916b01d0f6f8b212c14e715cc0d85c669575 100644 GIT binary patch literal 16384 zcmeI&@lM)690%|#7^@~y7iXFnN$wAqK~z#&D^PRE*lad31jlNkBn#=J3uIU()XeO^ z%id%Uvt%!@7w{SUe@8`7)NLknf5`XOl+wH3(YsH}AD7ItC>=zLcUoM|Z7ZtxbZjw~z+pI{4#=a0G5&J0)2nav`0uX=z1Rwwb2tWV= z5Ev_gYIIUHRq1onuX`uW`bo`i2VS>!(DB^BtJmBhJfC<`vGa~i3#Bc4kKU9<%Ox77 zCi+eDL2@TDsjjR@`+@tu<#mr*Vv1YqdPm2ee=vF)?O(vjKQG#JBsbx^hu*4andG~Y ztg5Q?bvpdb@Pgq-%!PxT&bKcRokWN&URDGj1DVn1fr z#Wpoo4YIsl(!kH-yD1{CX)h>GUoFq3%%SjW3Mj9 z)_sSr>E@c2ThlfiZNoBimX=MKhEct(oRG*eS>f}HxySB5j=?oeC!Mo&%`&x=kDf2tWV= z5P$##AOHafKmY;|fWSi*n3kfd6fRYW&xmnK$|+G!mG9x!e#isI*dPD_2tWV=5P$## zAOHafKmY>&MPMn)r1Mww*)=`wXxZK}j&)tUqR&>RhLvKC$H@AL$D(`j!OQ)yQqwKN aOlhW3oxjrZ(jtVkmZ>gYsT_IzAO8hFQkM7t delta 176 zcmZo@U~Fh$oFL7}IZ?)$k#l3hGJcj>4E(b;3o16%Rsf=sj8ui>{JfIXyb^`9{33;1gc2hIBU4=i6I~;V5CcOiV?!%LLp@Vt%P8#P ioBzo(3UIS { + + + // 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