Created
March 15, 2023 20:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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