From 747f92a2543e4cf699ebd8d0ff00a3dbc265a1f9 Mon Sep 17 00:00:00 2001 From: alqazimi Date: Sun, 28 Sep 2025 18:33:34 +0100 Subject: [PATCH 1/3] finisheed alqazimi --- README.md | 2 +- server/notes.db | Bin 16384 -> 16384 bytes src/components/CreateNoteForm.jsx | 132 ++++++++++++++++++++++++++++-- src/schema/notes.js | 16 ++++ 4 files changed, 142 insertions(+), 8 deletions(-) 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 9fb91df1bfcf0c2fe714e14a2690badbfe389499..4d1cf6dc88c13b222e91e5956cd68e05ca39b533 100644 GIT binary patch delta 684 zcmZ`%ze~eF6uxK)Qkz~&C@r*7Q5+ktcTJic91adTiy&sQHorg{k+xu`#Jz*!WH(30 z(*L2e|AY8fbnzlsFF|?S8@_k%d-r|s;#i7f>9H==o*G9|ZGQO*D@6)&%4!47U`bgO zm&z)M`NO9Yv4RZL3S(DccQZ89R)^R)67-6NV_62(1IGH}aCG~fb_czFuO~7gL`jEY ztm$S~d|(O(030U%PR8S`zl0LBM-kSLk@bk6AKbPVv!+|BDtUMca!?2G1HM5WPWj=n zD3_WrYHTt|lqhbl0GEJ@MR5b$e-zi==Cib-L5ff$$#+SqmbvVm^w3{PTS@Eld$T6x Fz5t~@pHKh* delta 67 zcmZo@U~Fh$oFL7}IZ?)$k#l3hLVivL1_nm{Sq%KMHVZ0D;NLt;K2?C5g$peDn1TN> TQ1m80iwv_Z { - // 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}

+ )} +
+ +
+ +