Skip to content

Instantly share code, notes, and snippets.

@omrilotan
Last active July 16, 2019 15:45
Show Gist options
  • Save omrilotan/bbfc817c13341c0efae12408be6072fd to your computer and use it in GitHub Desktop.
Save omrilotan/bbfc817c13341c0efae12408be6072fd to your computer and use it in GitHub Desktop.
Override forms' submit and send the form here, instead (https://github.com/omrilotan/contact-form-example)
/**
* Send the form over http
* @param {HTMLFormElement} form
* @return {String|undefined} Error message or nothing
*
* Prevent form's organic submit, and send it here, instead
*/
export default async function send(target) {
try {
const url = target.getAttribute('action');
const method = target.getAttribute('method');
const data = Object.assign(...[].map.call(
target,
({name, value}) => ({[name]: value})
));
const result = await fetch(url, {
method,
mode: 'cors',
body: JSON.stringify(data)
});
if (!result.ok) { return 'Oh geez, couldn\'t send the form. Maybe try again?'; }
const { status } = await result.json();
if (status !== 'success') { return `Form was sent but something went wrong along the way (status ${status}).`; }
return undefined; // Success
} catch (error) {
return `Sending form failed with error: ${error.message}.`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment