From 3d239c189c64b3b412bbbf83d3a59050472583f5 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Tue, 8 Nov 2022 19:18:14 -0600 Subject: [PATCH] Refactor error box adding --- src/main/resources/public/js/main.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/resources/public/js/main.js b/src/main/resources/public/js/main.js index 87578f3..93c0411 100644 --- a/src/main/resources/public/js/main.js +++ b/src/main/resources/public/js/main.js @@ -20,6 +20,15 @@ const displayData = (data) => { .forEach(tr => table.appendChild(tr)); }; +const addErrBox = () => { + const controls = document.querySelector(".pure-controls"); + const errBox = document.createElement("p"); + errBox.setAttribute("id", "errBox"); + errBox.setAttribute("style", "color:red"); + errBox.innerHTML = "Short URL not valid or already in use!"; + controls.appendChild(errBox); +} + const TR = (row) => { const tr = document.createElement("tr"); const longTD = TD(A(row.long)); @@ -73,14 +82,10 @@ const submitForm = () => { body: `${longUrl.value};${shortUrl.value}` }) .then((res) => { + console.log("0"); if (!res.ok) { if (document.getElementById("errBox") == null) { - const controls = document.querySelector(".pure-controls"); - const errBox = document.createElement("p"); - errBox.setAttribute("id", "errBox"); - errBox.setAttribute("style", "color:red"); - errBox.innerHTML = "Short URL not valid or already in use!"; - controls.appendChild(errBox); + addErrBox(); } } else {