From 3e058ac604f76db8d2048166206e2ceed90419c1 Mon Sep 17 00:00:00 2001 From: Preston Doman Date: Sun, 10 Feb 2019 15:46:27 -0800 Subject: [PATCH 1/5] Add delete report functionality --- front/static/edit_report.html | 2 +- front/static/js/viewHistory.js | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/front/static/edit_report.html b/front/static/edit_report.html index 50af1af..0e4880d 100644 --- a/front/static/edit_report.html +++ b/front/static/edit_report.html @@ -74,7 +74,7 @@ diff --git a/front/static/js/viewHistory.js b/front/static/js/viewHistory.js index 6eae3cf..0004e47 100644 --- a/front/static/js/viewHistory.js +++ b/front/static/js/viewHistory.js @@ -3,6 +3,37 @@ function getEndpointDomain() { return "https://" + window.location.hostname + ":8444/"; } +function removeDataFromEndpoint(url) { + const token = localStorage.getItem("token"); + const xhr = new XMLHttpRequest(); + + console.log("Attempting a connection to the following endpoint: " + url); + + + xhr.open("DELETE", url, true); + xhr.setRequestHeader("Authorization", "Bearer " + token); + xhr.onreadystatechange = function() { + if (this.readyState === 4) { + if (this.status === 200) { + console.log("DELETE SUCCESS!"); + console.log("Server response:\n" + this.response); + alert("Report deleted"); + location.reload(true); + } else { + console.error("DELETE FAILURE!"); + console.error("Server status: " + this.status); + console.error("Server response:\n" + this.response); + } + } + }; + + xhr.onerror = function() { + alert("Connection error!"); + }; + + xhr.send(); +} + // Make a GET request to url and pass response to callback function function getDataFromEndpoint(url, callback) { const token = localStorage.getItem("token"); @@ -334,6 +365,10 @@ document.addEventListener("click", function(event) { if (event.target && event.target.classList.contains("edit-report-button")) { console.log("Edit button clicked"); const url = getEndpointDomain() + "api/v1/report/" + event.target.dataset.rid; + const deleteButton = document.querySelector(".delete-report"); + if (deleteButton) { + deleteButton.setAttribute("data-rid", event.target.dataset.rid); + } getDataFromEndpoint(url, createEditReportForm); } if(event.target && event.target.classList.contains("view-report-button")) @@ -343,4 +378,14 @@ document.addEventListener("click", function(event) { getDataFromEndpoint(url, displayReport); } // TODO: Add View Report + + if(event.target && event.target.classList.contains("delete-report")) { + event.preventDefault(); + console.log("Delete report button clicked"); + const result = confirm("Are you sure you want to delete this report?"); + if (result) { + const url = getEndpointDomain() + "api/v1/report/" + event.target.dataset.rid; + removeDataFromEndpoint(url); + } + } }); From 74d503c24439aaf727905995d0b5e95ff84cee89 Mon Sep 17 00:00:00 2001 From: Preston Doman Date: Sun, 10 Feb 2019 18:15:32 -0800 Subject: [PATCH 2/5] Refactor AJAX request code --- front/static/edit_report.html | 2 +- front/static/js/viewHistory.js | 97 +++++++--------------------------- 2 files changed, 19 insertions(+), 80 deletions(-) diff --git a/front/static/edit_report.html b/front/static/edit_report.html index b252cd5..903a064 100644 --- a/front/static/edit_report.html +++ b/front/static/edit_report.html @@ -66,7 +66,7 @@