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