import React, { useRef, useState } from "react"; import ReCAPTCHA from "react-google-recaptcha"; const captchaKey = import.meta.env.VITE_CAPTCHA_KEY; const captchaSecret = import.meta.env.VITE_CAPTCHA_SECRET; const serverlUrl = import.meta.env.VITE_SERVER_URL; export default function ContactForm() { const captchaRef = useRef(); const [formValue, setFormValue] = useState({ firstName: "", lastName: "", email: "", company: "", phone: "", textarea: "", }); const [formError, setFormError] = useState({}); const [submit, setSubmit] = useState(false); const [loading, setLoading] = useState(false); const [captchaError, setCaptchaError] = useState(false); const handleSubmit = async (e) => { e.preventDefault(); setLoading(true); setFormError(validateForm(formValue)); if (Object.keys(validateForm(formValue)).length > 0) { setLoading(false); return null; } else { let captchaToken = captchaRef.current?.getValue(); if (captchaToken.length === 0) { setCaptchaError("ReCaptcha is mandatory"); setLoading(false); return; } window.scrollTo(0, 0); const verify = await verifyCaptcha(captchaToken); captchaRef.current?.reset(); if (verify) { setSubmit(true); setFormValue({ firstname: "", lastname: "", email: "", message: "", }); setCaptchaError(""); setLoading(false); } else { setSubmit(false); setFormValue({ firstname: "", lastname: "", email: "", message: "", }); setCaptchaError(""); setLoading(false); } } }; const handleValidation = (e) => { const { name, value } = e.target; setFormValue({ ...formValue, [name]: value }); }; const verifyCaptcha = async (captchaToken) => { try { const response = await fetch(serverlUrl, { method: "POST", body: JSON.stringify({ secret: captchaSecret, captchaToken, }), headers: { "Content-Type": "application/json", }, }); if (response.status === 200) { return true; } else { return false; } } catch (error) { console.error("Could not verify captcha!", error.message); return false; } }; const validateForm = (value) => { let errors = {}; const emailRegex = /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/; const numberRegex = /\d/; if (!value.firstName) { errors.firstName = "Please enter first name!"; } else if (value.firstName && value.firstName.length < 3) { errors.firstName = "First name is too short"; } if (!value.lastName) { errors.lastName = "Please enter last name!"; } else if (value.lastName && value.lastName.length < 3) { errors.lastName = "Last name is too short"; } if (!value.email) { errors.email = "Please enter email!"; } else if (!emailRegex.test(value.email)) { errors.email = "Please enter valid email!"; } if (!value.company) { errors.company = "Please enter company!"; } else if (value.company && value.company.length < 3) { errors.company = "Please enter a valid company name"; } if (!value.phone) { errors.phone = "Please enter phone number!"; } else if (!numberRegex.test(value.phone)) { errors.phone = "Please enter valid phone number!"; } if (!value.textarea) { errors.textarea = "Please enter message!"; } else if (value.textarea && value.textarea.length < 10) { errors.textarea = "The message should be min. 10 characters"; } return errors; }; return ( {loading ? (
Loading...
) : submit && Object.keys(formError).length === 0 ? (

Thank you submitting your request. We will review your message and contact you shortly!

) : (
{formError.firstName}
{formError.lastName}
{formError.email}
{formError.company}
{formError.phone}
(Max. 500 Characters)