diff --git a/client/src/component/Footers/Contactus.jsx b/client/src/component/Footers/Contactus.jsx
index 41cd84a..4082bba 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) {
-
+