"use client";
import { useState, useEffect } from "react";
import ReCAPTCHA from "react-google-recaptcha";
import axios from "axios";
import Link from "next/link";
const HomeContact = () => {
const [formData, setFormData] = useState({
name: "",
email: "",
phone: "",
web: "",
message: "",
});
const [formErrors, setFormErrors] = useState({});
const [captchaToken, setCaptchaToken] = useState(null);
const [alert, setAlert] = useState({ show: false, type: "", message: "" });
const handleChange = (e) => {
const { name, value } = e.target;
setFormData((prev) => ({ ...prev, [name]: value }));
};
const handleCaptchaChange = (token) => {
console.log("β
Captcha token:", token);
setCaptchaToken(token);
};
const handleSubmit = async (e) => {
e.preventDefault();
const errors = {};
if (!formData.name.trim()) errors.name = "Name is required.";
if (!formData.email.trim()) errors.email = "Email is required.";
if (!formData.phone.trim()) errors.phone = "Phone is required.";
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.name,
email: formData.email,
phone: formData.phone,
subject: formData.web || "Home Contact Form",
message: `Website: ${formData.web}
Message: ${formData.message}`,
to: "info@metatroncubesolutions.com",
senderName: "Metatroncube Home Page",
recaptchaToken: captchaToken,
};
console.log("π Submitting home form data:", emailData);
try {
const res = await axios.post("https://mailserver.metatronnest.com/send", emailData, {
headers: { "Content-Type": "application/json" },
});
setAlert({
show: true,
type: "success",
message: res?.data?.message || "Message sent successfully!",
});
setFormData({
name: "",
email: "",
phone: "",
web: "",
message: "",
});
setCaptchaToken(null);
setFormErrors({});
} catch (error) {
console.error("β Error sending email:", error);
setAlert({
show: true,
type: "danger",
message: "Failed to send message. Please try again later.",
});
}
};
useEffect(() => {
if (alert.show) {
const timer = setTimeout(() => {
setAlert((prev) => ({ ...prev, show: false }));
}, 5000);
return () => clearTimeout(timer);
}
}, [alert.show]);
return (
Weβre here to simplify your digital journey. Share your project ideas, challenges, or questions, and our experts will guide you with the right solutions.
Expect a personalized reply within one business day.
Get a free consultation to align your goals with our web, app, and marketing solutions.
Your data is safe with us. All details remain confidential and secure.