diff --git a/components/Contact/ContactForm.jsx b/components/Contact/ContactForm.jsx index ae00054..a499b8e 100644 --- a/components/Contact/ContactForm.jsx +++ b/components/Contact/ContactForm.jsx @@ -35,13 +35,76 @@ function ContactForm() { const handleSubmit = (e) => { e.preventDefault(); + + // Frontend validation before sending + if (!formData.name.trim() || formData.name.trim().length < 2) { + toast.error("Name must be at least 2 characters long.", { + position: "top-center", + autoClose: 5000, + theme: "colored" + }); + return; + } + + if (!formData.email.trim()) { + toast.error("Email is required.", { + position: "top-center", + autoClose: 5000, + theme: "colored" + }); + return; + } + + if (emailError) { + toast.error("Please enter a valid email address.", { + position: "top-center", + autoClose: 5000, + theme: "colored" + }); + return; + } + + if (!formData.message.trim() || formData.message.trim().length < 10) { + toast.error("Message must be at least 10 characters long.", { + position: "top-center", + autoClose: 5000, + theme: "colored" + }); + return; + } + + if (formData.message.trim().length > 2000) { + toast.error("Message must be less than 2000 characters.", { + position: "top-center", + autoClose: 5000, + theme: "colored" + }); + return; + } + setIsSubmitting(true); + // Log the data being sent for debugging + console.log("📤 Sending form data:", { + name: formData.name.trim(), + email: formData.email.trim(), + message: formData.message.trim(), + lengths: { + name: formData.name.trim().length, + email: formData.email.trim().length, + message: formData.message.trim().length + } + }); + axios - .post("/api/v1/contact", formData) + .post("/api/v1/contact", { + name: formData.name.trim(), + email: formData.email.trim(), + message: formData.message.trim() + }) .then((response) => { - console.log("Response:", response.data); - toast.success("Your form has been submitted successfully!", { + console.log("✅ Response:", response.data); + toast.success(response.data.message || "Your form has been submitted successfully!", { position: "top-center", autoClose: 5000, hideProgressBar: false, @@ -56,25 +119,39 @@ function ContactForm() { }) .catch((error) => { console.error("Error:", error); - toast.error( - "There was an error submitting your form. Please try again.", - { - position: "top-center", - autoClose: 5000, - hideProgressBar: false, - closeOnClick: true, - pauseOnHover: true, - draggable: true, - progress: undefined, - theme: "colored" - } - ); + + // Get specific error message from API response + let errorMessage = "There was an error submitting your form. Please try again."; + + if (error.response && error.response.data) { + errorMessage = error.response.data.message || errorMessage; + console.log("API Error Details:", error.response.data); + } + + toast.error(errorMessage, { + position: "top-center", + autoClose: 7000, + hideProgressBar: false, + closeOnClick: true, + pauseOnHover: true, + draggable: true, + progress: undefined, + theme: "colored" + }); setIsSubmitting(false); }); }; - const isFormValid = - formData.name && formData.email && formData.message && !emailError; + const isFormValid = () => { + return ( + formData.name.trim().length >= 2 && + formData.name.trim().length <= 100 && + formData.email.trim() && + !emailError && + formData.message.trim().length >= 10 && + formData.message.trim().length <= 2000 + ); + }; return ( <> @@ -94,44 +171,68 @@ function ContactForm() { > Send Us Your Queries

- - - {emailError && ( -

{emailError}

- )} - +
+ 0 && formData.name.length < 2 ? 'border-red-500' : 'border-gray'} text-white font-dmSans bg-bg_black outline-none w-full my-8 mt-10`} + /> + {formData.name.length > 0 && formData.name.length < 2 && ( +

Name must be at least 2 characters

+ )} +
+ {formData.name.length}/100 +
+
+ +
+ + {emailError && ( +

{emailError}

+ )} +
+ +
+