|
| 1 | +import React, { useRef, useState } from "react"; |
| 2 | +import emailjs from "emailjs-com"; |
| 3 | +import ReCAPTCHA from "react-google-recaptcha"; |
| 4 | + |
| 5 | +const ContactForm: React.FC = () => { |
| 6 | + const form = useRef<HTMLFormElement>(null); |
| 7 | + const recaptchaRef = useRef<any>(null); // Added typing to ref for better type support. |
| 8 | + const [done, setDone] = useState(false); |
| 9 | + const [isCaptchaVerified, setIsCaptchaVerified] = useState(false); |
| 10 | + const [showPopup, setShowPopup] = useState(false); |
| 11 | + |
| 12 | + const closeModal = () => { |
| 13 | + setShowPopup(false); |
| 14 | + }; |
| 15 | + |
| 16 | + const sendEmail = (e: React.FormEvent) => { |
| 17 | + e.preventDefault(); |
| 18 | + if (!isCaptchaVerified) { |
| 19 | + setShowPopup(true); |
| 20 | + return; |
| 21 | + } |
| 22 | + emailjs.sendForm( |
| 23 | + process.env.NEXT_PUBLIC_SERVICE_ID, |
| 24 | + process.env.NEXT_PUBLIC_TEMPLATE_ID, |
| 25 | + form.current!, |
| 26 | + process.env.NEXT_PUBLIC_EMAILJS_USER_ID |
| 27 | + ).then((result) => { |
| 28 | + console.log(result.text); |
| 29 | + setDone(true); |
| 30 | + form.current!.reset(); |
| 31 | + setIsCaptchaVerified(false); |
| 32 | + }).catch((error) => { |
| 33 | + console.log(error.text); |
| 34 | + }); |
| 35 | + }; |
| 36 | + |
| 37 | + return ( |
| 38 | + <> |
| 39 | + {showPopup && ( |
| 40 | + <div className="fixed inset-0 flex items-center justify-center z-50"> |
| 41 | + <div className="relative w-full max-w-md"> |
| 42 | + <div className="relative bg-white rounded-lg shadow dark:bg-gray-700 p-6"> |
| 43 | + <h3 className="mb-5 text-lg font-normal text-gray-500 dark:text-gray-400 text-center"> |
| 44 | + Please complete the CAPTCHA |
| 45 | + </h3> |
| 46 | + <button |
| 47 | + onClick={closeModal} |
| 48 | + type="button" |
| 49 | + className="button w-full text-white bg-red-600 hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-red-300 dark:focus:ring-red-800 font-medium rounded-lg text-sm px-5 py-2.5" |
| 50 | + > |
| 51 | + Close |
| 52 | + </button> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + </div> |
| 56 | + )} |
| 57 | + <div className="max-w-md mx-auto bg-white rounded-lg shadow-2xl p-8 glowingCard"> |
| 58 | + <form ref={form} onSubmit={sendEmail}> |
| 59 | + {['Name', 'Email', 'Subject', 'Message'].map((label, idx) => ( |
| 60 | + <div className="mb-4" key={label}> |
| 61 | + <label className="block font-medium text-gray-700">{label} *</label> |
| 62 | + {label !== 'Message' ? ( |
| 63 | + <input |
| 64 | + type={label.toLowerCase()} |
| 65 | + name={`user_${label.toLowerCase()}`} |
| 66 | + placeholder={label} |
| 67 | + required |
| 68 | + className="user w-full px-4 py-2 border rounded-md shadow-sm focus:ring focus:ring-purple-500 focus:border-purple-500" |
| 69 | + /> |
| 70 | + ) : ( |
| 71 | + <textarea |
| 72 | + rows={2} |
| 73 | + required |
| 74 | + className="user w-full px-4 py-2 border rounded-md shadow-sm focus:ring focus:ring-purple-500 focus:border-purple-500" |
| 75 | + name="message" |
| 76 | + placeholder={label} |
| 77 | + ></textarea> |
| 78 | + )} |
| 79 | + </div> |
| 80 | + ))} |
| 81 | + <div className="mb-4"> |
| 82 | + <ReCAPTCHA |
| 83 | + ref={recaptchaRef} |
| 84 | + sitekey={process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY} |
| 85 | + onChange={() => setIsCaptchaVerified(true)} |
| 86 | + /> |
| 87 | + </div> |
| 88 | + <button |
| 89 | + type="submit" |
| 90 | + value="Send" |
| 91 | + className="button w-full bg-gradient-to-br from-pink-600 to-purple-700 text-white py-2 px-4 rounded-md hover:from-pink-500 hover:to-purple-600 transition duration-300 shadow-md" |
| 92 | + > |
| 93 | + Send |
| 94 | + </button> |
| 95 | + </form> |
| 96 | + {done && ( |
| 97 | + <span className="mb-8 flex items-center justify-center text-xl text-gray-400 pt-6"> |
| 98 | + Thanks for contacting Us! |
| 99 | + </span> |
| 100 | + )} |
| 101 | + </div> |
| 102 | + </> |
| 103 | + ); |
| 104 | +}; |
| 105 | + |
| 106 | +export default ContactForm; |
0 commit comments