From d196c0373fa74a8a6205f888d19f39e564aa4e33 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Thu, 10 Nov 2022 18:11:57 -0600 Subject: [PATCH] Copy shortUrl after adding to db --- .../java/tk/draganczuk/url/UrlRepository.java | 2 +- src/main/resources/public/js/main.js | 29 ++++++++++--------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main/java/tk/draganczuk/url/UrlRepository.java b/src/main/java/tk/draganczuk/url/UrlRepository.java index cc87503..767c1ba 100644 --- a/src/main/java/tk/draganczuk/url/UrlRepository.java +++ b/src/main/java/tk/draganczuk/url/UrlRepository.java @@ -75,7 +75,7 @@ public class UrlRepository { } catch (SQLException e) { e.printStackTrace(); } - return ""; + return shortUrl; } public void addHit(String shortURL) { diff --git a/src/main/resources/public/js/main.js b/src/main/resources/public/js/main.js index 862d045..391805d 100644 --- a/src/main/resources/public/js/main.js +++ b/src/main/resources/public/js/main.js @@ -27,13 +27,13 @@ const displayData = (data) => { } }; -const addErrBox = async () => { +const addAlertBox = async (s, t) => { 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 alertBox = document.createElement("p"); + alertBox.setAttribute("id", "alertBox"); + alertBox.setAttribute("style", `color:${t}`); + alertBox.innerHTML = s; + controls.appendChild(alertBox); } const TR = (row) => { @@ -94,17 +94,20 @@ const submitForm = () => { }) .then((res) => { if (!res.ok) { - if (document.getElementById("errBox") == null) { - addErrBox(); + if (document.getElementById("alertBox") == null) { + addAlertBox("Short URL not valid or already in use!", "red"); } } else { - document.getElementById("errBox")?.remove(); - longUrl.value = ""; - shortUrl.value = ""; - - refreshData(); + return res.text(); } + }).then((text) => { + navigator.clipboard.writeText(`${window.location.host}/${text}`); + addAlertBox("Short URL copied to clipboard!", "green"); + longUrl.value = ""; + shortUrl.value = ""; + + refreshData(); }); };