contact-mailer/client/index.js
2020-08-24 21:32:37 +02:00

40 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function message (level, text) {
const messageContainer = document.getElementById('contact-mailer-message')
const messageElement = document.createElement('p')
messageContainer.appendChild(messageElement)
messageElement.textContent = text
messageElement.classList.add('contact-mailer-message')
messageElement.classList.add('contact-mailer-message-'+level)
setTimeout(() => {
messageContainer.removeChild(messageElement)
}, 10000)
}
function interceptForm (formId) {
/*
* This function intercepts a form submission and send it via XHR.
* Param formId is the HTML id of the form
*/
const formElem = document.getElementById(formId)
formElem.onsubmit = async (e) => {
e.preventDefault()
fetch(formElem.action, {
method: formElem.method,
body: new FormData(formElem)
})
.then(data => {
if (!data.ok || data.status == 500) {
message('error', 'Erreur du service denvoi. Réessayez plus tard ou contactez https://jean-cloud.net')
} else if (data.ok || data.status == 200) {
message('success', 'Le message a bien été envoyé !')
formElem.reset()
}
})
.catch((error) => {
message('error', 'Impossible denvoyer le formulaire. Vérifiez votre connexion internet ou réessayez plus tard.')
})
}
}
interceptForm ('contact-mailer-form')