cleaning up code

Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
Michael 2020-09-26 21:44:24 +01:00
parent 008070e8d3
commit b45c77be1c

View File

@ -1,32 +1,36 @@
function alert() {
let alerts = {
"success": document.getElementById("success"),
"warning": document.getElementById("warning"),
"danger": document.getElementById("danger")
}
return alerts
}
async function post() {
let formData = new FormData();
var success = document.getElementById("success")
var warning = document.getElementById("warning")
var danger = document.getElementById("danger")
success.hidden=true
warning.hidden=true
danger.hidden=true
formData.append("email", document.getElementById("email").value);
formData.append("message", document.getElementById("message").value);
var form = document.getElementById("form");
var alerts = alert();
for (const key in alerts) {
alerts[key].hidden=true
}
let response = await fetch("contact/form", {
method: "POST",
body: formData
body: new FormData(form)
});
if (response.ok) {
success.hidden=false
success.textContent="Message sent, we'll get back to you as soon as possible"
document.getElementById("email").value=""
document.getElementById("message").value="";
alerts.success.hidden=false
alerts.success.textContent="Message sent, we'll get back to you as soon as possible"
form.reset();
} else {
if (response.status == 400) {
let json = await response.json();
if (json) {
danger.hidden=false
danger.textContent=json
alerts.danger.hidden=false
alerts.danger.textContent=json
}
} else {
warning.hidden=false
warning.textContent="Unable to send, please try again later"
alerts.warning.hidden=false
alerts.warning.textContent="Unable to send, please try again later"
}
}
}