From 20a4f4696505af716287f75cac529f2d75739efb Mon Sep 17 00:00:00 2001 From: selvi Date: Wed, 30 Jul 2025 22:08:53 +0530 Subject: [PATCH] contact form updated --- components/contact/ContactForm.jsx | 237 +++++++++++++------ components/homes/home-1/Blogs.jsx | 4 +- components/homes/home-1/project.jsx | 2 +- components/turnkey-solutions/page.jsx | 2 +- package-lock.json | 316 ++++++++++++++++++++++++++ package.json | 2 + public/assets/css/main.css | 4 +- public/assets/scss/_contact.scss | 2 +- public/assets/scss/_section.scss | 4 +- utlis/constant.utils.js | 6 +- 10 files changed, 499 insertions(+), 80 deletions(-) diff --git a/components/contact/ContactForm.jsx b/components/contact/ContactForm.jsx index 0444442..d1f574b 100644 --- a/components/contact/ContactForm.jsx +++ b/components/contact/ContactForm.jsx @@ -1,44 +1,92 @@ "use client"; -import emailjs from "@emailjs/browser"; -import React, { useRef } from "react"; +import React, { useRef, useState } from "react"; import { toast } from "react-toastify"; +import ReCAPTCHA from "react-google-recaptcha"; +import axios from "axios"; import "react-toastify/dist/ReactToastify.css"; import AnimatedText from "../common/AnimatedText"; export default function ContactForm() { const form = useRef(); - const sandMail = (e) => { - e.preventDefault(); - emailjs - .sendForm("service_noj8796", "template_fs3xchn", form.current, { - publicKey: "iG4SCmR-YtJagQ4gV", - }) - .then((res) => { - if (res.status == 200) { - toast.success("Message Sent successfully!", { - position: "bottom-right", - autoClose: 5000, - hideProgressBar: false, - closeOnClick: true, - pauseOnHover: true, - draggable: true, - progress: undefined, - }); - form.current.reset(); - } else { - toast.error("Ops Message not Sent!", { - position: "bottom-right", - autoClose: 5000, - hideProgressBar: false, - closeOnClick: true, - pauseOnHover: true, - draggable: true, - progress: undefined, - }); - } - }); + const [formData, setFormData] = useState({ + firstName: "", + email: "", + number: "", + message: "", + product: "", + }); + + const [formErrors, setFormErrors] = useState({}); + const [captchaToken, setCaptchaToken] = useState(null); + + const handleChange = (e) => { + const { name, value } = e.target; + setFormData((prev) => ({ + ...prev, + [name]: value, + })); }; + + const handleCaptchaChange = (token) => { + setCaptchaToken(token); + }; + + const handleSubmit = async (e) => { + e.preventDefault(); + + const errors = {}; + if (!formData.firstName.trim()) errors.firstName = "First name is required."; + if (!formData.email.trim()) errors.email = "Email is required."; + if (!formData.number.trim()) errors.number = "Phone number is required."; + if (!formData.product.trim()) errors.product = "Please select a product."; + if (!formData.message.trim()) errors.message = "Message is required."; + if (!captchaToken) errors.captcha = "Please verify the CAPTCHA."; + + setFormErrors(errors); + if (Object.keys(errors).length > 0) return; + + const emailData = { + name: formData.firstName, + email: formData.email, + phone: formData.number, + service: formData.product, + message: `Message: ${formData.message}`, + to: "lead@metatroncube.in", + senderName: "Metatroncube Landing", + recaptchaToken: captchaToken, + }; + + try { + const res = await axios.post( + "https://mailserver.metatronnest.com/send", + emailData, + { headers: { "Content-Type": "application/json" } } + ); + + toast.success(res?.data?.message || "Message sent successfully!", { + position: "bottom-right", + autoClose: 5000, + }); + + setFormData({ + firstName: "", + email: "", + number: "", + message: "", + product: "", + }); + setCaptchaToken(null); + setFormErrors({}); + form.current.reset(); + } catch (err) { + toast.error("Failed to send message. Please try again later.", { + position: "bottom-right", + autoClose: 5000, + }); + } + }; + return (
@@ -48,78 +96,131 @@ export default function ContactForm() {
-
+
+ {formErrors.firstName && ( +
{formErrors.firstName}
+ )}
+
-
- -
-
-
-
-
- -
- -
-
-
-
-
-
+ {formErrors.email && ( +
{formErrors.email}
+ )}
+ +
+
+ +
+ +
+ {formErrors.number && ( +
{formErrors.number}
+ )} +
+
+ +
+
+ +
+ +
+ {formErrors.product && ( +
{formErrors.product}
+ )} +
+
+