From fdea071e75d1611d60e8e2b82cba9596a74c01d7 Mon Sep 17 00:00:00 2001
From: jonasled <jonas@jonasled.de>
Date: Tue, 25 Feb 2020 20:21:23 +0100
Subject: [PATCH] deletelink is now a ajax function

---
 deletelink.py              |  2 +-
 templates/editEntries.html | 14 ++++++++++++++
 userprofile.py             |  4 +++-
 3 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/deletelink.py b/deletelink.py
index 2d8a04b..b68b155 100644
--- a/deletelink.py
+++ b/deletelink.py
@@ -14,7 +14,7 @@ def deleteLink(request, s):
         cursor = conn.cursor()
         try:
             cursor.execute('DELETE FROM WEB_URL WHERE SHORT_URL=? AND USERNAME=?', [linkToDelete, userID]) #Delete the entrie
-            return redirect('/user/links') #redirect the user back to the table.
+            return "OK" #response is only for ajax request
         except:
             abort(500)
             
diff --git a/templates/editEntries.html b/templates/editEntries.html
index 404ad13..b08740a 100644
--- a/templates/editEntries.html
+++ b/templates/editEntries.html
@@ -41,6 +41,20 @@
 
             });          
          }
+
+         function deleteLink(link, elementId) {
+            var xhttp = new XMLHttpRequest();
+            xhttp.onreadystatechange = function() {
+               if (this.readyState == 4 && this.status == 200) {
+                  var element = document.getElementById(elementId);
+                  element.parentNode.removeChild(element);
+               }else if(this.readyState == 4) {
+                  alert("error deleting link")
+               }
+            };
+            xhttp.open("GET", link, true);
+            xhttp.send();
+         }
       </script>
    </body>
 </html>
\ No newline at end of file
diff --git a/userprofile.py b/userprofile.py
index 216f026..d4f6737 100644
--- a/userprofile.py
+++ b/userprofile.py
@@ -25,13 +25,15 @@ def userProfile(request, cookieNotice, s, pageNumber, url_scheme):
         try:
             entriesList = res.fetchall()
             lenEntries = len(entriesList)
+            idCounter = 0
             for entries in entriesList[offset:][:25]: #for every entrie in the database add a line to the table
                 cursor2 = conn.cursor()
                 try:
                     calls = str(cursor2.execute('SELECT CALLS FROM ANALYTICS WHERE SHORT_URL=?', [entries[1]]).fetchone()[0])
                 except:
                     calls = "0"
-                response = response + "<tr>\n<td>" + entries[0] + "</td>\n<td><a href=\"" + url_scheme + "://" + entries[1] + '">' + entries[1] + '</a></td>\n<td>' + calls + '</td>\n<td><a id="red" href="/user/delete?link=' + escape(entries[1]) + '">delete</a> <a href="#" id="dialog-link" onclick="buttonListener(\'' + entries[1] + '\', this)">QR</a></tr>\n'
+                response = response + "<tr id=tr_" + str(idCounter) + ">\n<td>" + entries[0] + "</td>\n<td><a href=\"" + url_scheme + "://" + entries[1] + '">' + entries[1] + '</a></td>\n<td>' + calls + '</td>\n<td><a id="red" href="javascript:deleteLink(\'/user/delete?link=' + escape(entries[1]) + '\',\'tr_' + str(idCounter) + '\')">delete</a> <a href="#" id="dialog-link" onclick="buttonListener(\'' + entries[1] + '\', this)">QR</a></tr>\n'
+                idCounter=idCounter+1
             response = response + "</table>" #Close the table
 
             if(len(entriesList) == 0): response = 'you have no shorten links.' #If user has no shorten links make this message
-- 
GitLab