Skip to content

Commit

Permalink
added error handler for fetch() call
Browse files Browse the repository at this point in the history
  • Loading branch information
James Tacker committed Apr 26, 2023
1 parent 4b5f858 commit 9766113
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/components/ContactModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ const ContactModal = () => {
const nameError = validateName(name);
const emailError = validateEmail(email);
if (!nameError && !emailError && agreeToTerms) {
const response = await fetch(`${process.env.REACT_APP_API_URL}/send-invite`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({real_name: name, email}),
});

if (response.ok) {
console.log('message sent');
} else {
console.log('error sending request');
try {
const response = await fetch(`${process.env.REACT_APP_API_URL}/send-invite`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({real_name: name, email}),
});
if (response.ok) {
console.log('message sent');
} else {
console.log('error sending request');
}
} catch (error) {
console.log(error);
alert('An error occurred while sending the request. Please try again later.');
}
} else {
alert('Please fix the errors in the form and agree to the terms of the code of conduct');
Expand Down

0 comments on commit 9766113

Please sign in to comment.