Skip to content

Instantly share code, notes, and snippets.

@afbelardi
Created March 15, 2023 20:25
Show Gist options
  • Save afbelardi/cca344c7310c1ebc2c16060dd0fbc95d to your computer and use it in GitHub Desktop.
Save afbelardi/cca344c7310c1ebc2c16060dd0fbc95d to your computer and use it in GitHub Desktop.
A submit function for a form to handle scenarios if correct information was input or not and display success or fail toast messages
const handleSubmit = async (e) => {
e.preventDefault();
try {
if (name.current.value === '' || email.current.value === '') {
setFieldIsEmpty(true);
} else {
const response = await fetch('https://nft-email.herokuapp.com/api/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: name.current.value,
email: email.current.value,
phoneNumber: phoneNumber.current.value
})
}).then(() => {
email.current.value = ""
name.current.value = ""
phoneNumber.current.value = ""
})
setEmailSubmitted(true)
}
} catch(error) {
console.error(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment