This commit is contained in:
Adrian Amaglio 2020-08-24 21:32:37 +02:00
parent b58e8c188b
commit 4181ff8455
2 changed files with 63 additions and 0 deletions

39
client/index.js Normal file
View File

@ -0,0 +1,39 @@
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')

24
client/style.css Normal file
View File

@ -0,0 +1,24 @@
.contact-mailer-form {
}
.contact-mailer-message {
border: 1px solid;
padding: 3px;
border-radius: 2px;
}
.contact-mailer-message-error {
color: red;
border-color: red;
}
.contact-mailer-message-success {
color: green;
border-color: green;
}
.contact-mailer-message-info {
color: blue;
border-color: blue;
}