From b45c77be1cc38dc314fbdfcfd78db4c02c2f40de Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 26 Sep 2020 21:44:24 +0100 Subject: [PATCH] cleaning up code Signed-off-by: Michael --- 0cd.xyz-post.js | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/0cd.xyz-post.js b/0cd.xyz-post.js index 122c111..72faf6f 100644 --- a/0cd.xyz-post.js +++ b/0cd.xyz-post.js @@ -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" } } }