diff --git a/app/(pages)/_components/ContactForm/ContactForm.jsx b/app/(pages)/_components/ContactForm/ContactForm.jsx new file mode 100644 index 0000000..f8fdf2e --- /dev/null +++ b/app/(pages)/_components/ContactForm/ContactForm.jsx @@ -0,0 +1,182 @@ +"use client"; + +import { useState } from "react"; +import styles from "./ContactForm.module.scss"; + +export default function ContactForm() { + const [firstName, setFirstName] = useState(""); + const [lastName, setLastName] = useState(""); + const [email, setEmail] = useState(""); + const [subject, setSubject] = useState(""); + const [message, setMessage] = useState(""); + + // Error handling + const [firstNameError, setFirstNameError] = useState(""); + const [lastNameError, setLastNameError] = useState(""); + const [emailError, setEmailError] = useState(""); + const [subjectError, setSubjectError] = useState(""); + const [messageError, setMessageError] = useState(""); + + // Prevent multiple submissions while loading + const [loading, setLoading] = useState(false); + + const handleSubmit = async (e) => { + console.log("submit"); + e.preventDefault(); // Prevent automatic page reload + + // Prevent multiple submissions while loading + if (loading) return; + + setLoading(true); + + // Input Validation + let valid = true; + + if (!firstName.trim()) { + setFirstNameError("Please provide a first name"); + valid = false; + } else { + setFirstNameError(""); + } + + if (!lastName.trim()) { + setLastNameError("Please provide a last name"); + valid = false; + } else { + setLastNameError(""); + } + + if (!email.trim()) { + setEmailError("Please provide an email"); + valid = false; + } else if (!/\S+@\S+\.\S+/.test(email)) { + setEmailError("Please provide a valid email"); + valid = false; + } else { + setEmailError(""); + } + + if (!subject.trim()) { + setSubjectError("Please provide a subject"); + valid = false; + } else { + setSubjectError(""); + } + + if (!message.trim()) { + setMessageError("Please provide a message"); + valid = false; + } else { + setMessageError(""); + } + + if (!valid) { + setLoading(false); // Reset loading state if validation fails + // return; + } + + const contactData = { + firstName, + lastName, + email, + subject, + message, + }; + + // Send to API + try { + const res = await fetch("/api/sendEmail", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(contactData), + }); + + if (!res.ok) throw new Error("Failed to send email"); + + const data = await res.json(); + alert(data.message || "Email sent successfully!"); + + // Reset form fields after successful submission + setFirstName(""); + setLastName(""); + setEmail(""); + setSubject(""); + setMessage(""); + } catch (error) { + alert("Something went wrong. Try again."); + } finally { + setLoading(false); + } + }; + + return ( +
+
+
+ + setFirstName(e.target.value)} + /> + {firstNameError && ( +

{firstNameError}

+ )} +
+
+ + setLastName(e.target.value)} + /> + {lastNameError &&

{lastNameError}

} +
+
+
+
+ + setEmail(e.target.value)} + /> + {emailError &&

{emailError}

} +
+
+ + setSubject(e.target.value)} + /> + {subjectError &&

{subjectError}

} +
+
+
+ +