From ccd83bfd9421e4d769068fd2f770465b8d1972de Mon Sep 17 00:00:00 2001 From: Abdulkadir Hassan Elmi Date: Sun, 28 Sep 2025 18:52:34 +0100 Subject: [PATCH 1/2] waan dhameeyey --- server/notes.db | Bin 16384 -> 16384 bytes src/components/CreateNoteForm.jsx | 133 ++++++++++++++++++++++++++++-- src/schema/notes.js | 13 ++- 3 files changed, 137 insertions(+), 9 deletions(-) diff --git a/server/notes.db b/server/notes.db index 9fb91df1bfcf0c2fe714e14a2690badbfe389499..057125adca8256b96f0642ab24a0a22d96ea3cff 100644 GIT binary patch delta 587 zcmZo@U~Fh$oFL68JW0AG!pX)Us%UEqGCv$-AlNpb>Uv~jfF_yfS(+K38v_?l zW(PVp8s^w=kUNtUl0a?-J2oXh58_ySZZ!Zp7wA??h$Zz1PrxOT*w`54gMqdrCg+sq z0!;>6l&6qgs*sYHkyxpam6%eVn3J7anGWt&d28Ig@f4r?wflJJn? Z#3BjzAUnDwvdh>Ik|vX{%L_8H000~Xleho? delta 63 zcmZo@U~Fh$oFL7}IZ?)$k#l3h5`H#D{#gwCvo;GVOyJ) { - // 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 */} - +
+

Create a New Note

+ + {submitSuccess && ( +
+ Note created successfully! +
+ )} + + {submitError && ( +
+ {submitError} +
+ )} + +
+
+ + + {errors.title && ( +

{errors.title.message}

+ )} +
+ +
+ +