From f9bd9c60ed9fae27af1d65535af2164c66eeb46a Mon Sep 17 00:00:00 2001 From: Sawan Date: Sat, 26 Oct 2024 11:59:16 +0530 Subject: [PATCH 1/2] Added backend api of contact form --- client/src/component/Footers/Contactus.jsx | 63 +++++++++++++++------- server/Controllers/contact.js | 49 +++++++++++++++++ server/Models/Contact.js | 31 +++++++++++ server/index.js | 3 +- server/routes/contact.js | 9 ++++ 5 files changed, 134 insertions(+), 21 deletions(-) create mode 100644 server/Controllers/contact.js create mode 100644 server/Models/Contact.js create mode 100644 server/routes/contact.js diff --git a/client/src/component/Footers/Contactus.jsx b/client/src/component/Footers/Contactus.jsx index 41cd84a..db48cd2 100644 --- a/client/src/component/Footers/Contactus.jsx +++ b/client/src/component/Footers/Contactus.jsx @@ -8,17 +8,22 @@ function ContactUs(props) { const [isSubmitting, setIsSubmitting] = useState(false); const [email, setEmail] = useState(''); const [name, setName] = useState(''); + const [message, setMessage] = useState(''); + const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || 'https://bitbox-uxbo.onrender.com'; - const handleSubmit = (e) => { + + const handleSubmit = async (e) => { e.preventDefault(); - if (isSubmitting) return; // If already submitting, do nothing - setIsSubmitting(true); // Set isSubmitting to true to disable the button + if (isSubmitting) return; // Prevent multiple submissions + setIsSubmitting(true); // Disable the button - if(name==""){ + // Validate name + if (!name.trim()) { toast.error("Please enter a valid name"); setIsSubmitting(false); return; } + // Basic email validation regex const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailPattern.test(email)) { @@ -27,25 +32,43 @@ function ContactUs(props) { return; } - const scriptURL = 'https://script.google.com/macros/s/AKfycby9s-kpS5yJrvU-igVgY4-B2m0YDoSVyhXHtpjmMAYjBQ2ECPBT7uZzy5qya9IyYq4/exec'; - const form = document.forms['submit-to-google-sheet']; + // Validate message + if (!message.trim()) { + toast.error("Please enter a valid message"); + setIsSubmitting(false); + return; + } + - fetch(scriptURL, { method: 'POST', body: new FormData(form) }) - .then(response => { - form.reset(); - setIsSubmitting(false); // Re-enable the button - // props.showAlert("Form Submitted Successfully", "success"); - toast.success("Form Submitted Successfully"); - console.log('Success!', response); - }) - .catch(error => { - setIsSubmitting(false); // Re-enable the button - // props.showAlert("Form Submission Failed", "danger"); - toast.error("Form Submission Failed"); - console.error('Error!', error.message); + try { + const response = await fetch(`${VITE_SERVER_PORT}/api/contact/submitContactForm`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ name, email, message }), }); + + const result = await response.json(); + // Check if the request was successful + if (response.ok) { + toast.success('Message Sent Successfully!'); + setName(''); + setEmail(''); + setMessage(''); + } else { + toast.error(result.message || 'Error in submission!'); + } + } catch (error) { + // Handle network or other fetch-related errors + toast.error('Something went wrong. Please try again.'); + } finally { + // Reset the submitting state regardless of the outcome + setIsSubmitting(false); + } }; + return (

Contact Us

@@ -69,7 +92,7 @@ function ContactUs(props) {
- +