Skip to content

Commit

Permalink
Add sequential logic to ensure form submission is always be executed …
Browse files Browse the repository at this point in the history
…after the email is sent
  • Loading branch information
Phillweston committed Apr 10, 2024
1 parent df9d8c6 commit 62db774
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions js/contact-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ document.getElementById('contactForm').addEventListener('submit', function(event
successMessageDiv.textContent = "Your action was successfully completed! We'll reply to you soon.";
successMessageDiv.style.display = 'block';
// Email sending for subscription
sendSubscriptionEmail(userEmail);
// Submit the form after sending the email
document.getElementById('contactForm').submit();
sendSubscriptionEmail(userEmail)
.then(() => {
console.log('Email sent successfully');
document.getElementById('contactForm').submit(); // Submit the form after the email is sent
})
.catch(error => {
console.log('Failed to send email: ' + error);
});

setTimeout(function() {
successMessageDiv.style.display = 'none';
Expand Down Expand Up @@ -70,9 +75,14 @@ document.getElementById('subscriptionForm').addEventListener('submit', function(
// If email is valid
document.getElementById('subscriptionSuccess').textContent = 'Email is valid! Proceeding with subscription.';
// Email sending for subscription
sendSubscriptionEmail(email);
// Submit the form after sending the email
document.getElementById('subscriptionForm').submit();
sendSubscriptionEmail(email)
.then(() => {
console.log('Email sent successfully');
document.getElementById('subscriptionForm').submit(); // Submit the form after the email is sent
})
.catch(error => {
console.log('Failed to send email: ' + error);
});
}
});

Expand All @@ -88,7 +98,7 @@ function loadConfig() {
}

function sendSubscriptionEmail(userEmail) {
Promise.all([
return Promise.all([
loadConfig(),
fetch('../email/index.html').then(response => response.text())
])
Expand Down Expand Up @@ -117,7 +127,5 @@ function sendSubscriptionEmail(userEmail) {
throw new Error('Failed to send email');
}
return response;
})
.then(() => console.log('Email sent successfully'))
.catch(error => console.log('Failed to send email: ' + error));
});
}

0 comments on commit 62db774

Please sign in to comment.