From 1691392b43a0699848a6f9432e9eb1678b521344 Mon Sep 17 00:00:00 2001 From: Preston Doman Date: Sun, 27 Jan 2019 16:21:51 -0800 Subject: [PATCH 1/3] Add list report history functionality to view_history.html --- front/static/js/viewHistory.js | 83 ++++++++++++++++++++++++++++++++++ front/static/view_history.html | 16 +++++-- 2 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 front/static/js/viewHistory.js diff --git a/front/static/js/viewHistory.js b/front/static/js/viewHistory.js new file mode 100644 index 0000000..bc037b6 --- /dev/null +++ b/front/static/js/viewHistory.js @@ -0,0 +1,83 @@ +function displayListOfReports(listOfReports) { + const cardBody = document.querySelector(".card-body"); + const table = document.createElement("table"); + const reports = listOfReports.reports; + let rowsInserted = 0; + + for (let i = 0; i < reports.length; i++) { + let title = reports[i].title; + let dateCreated = new Date(reports[i].date_created).toLocaleDateString("en-US"); + let state = reports[i].state; + let dateSubmitted = (state === "created") ? "TBD": new Date(reports[i].date_submitted).toLocaleDateString("en-US"); + let bodyRow = table.insertRow(i); + + bodyRow.insertCell(0).innerHTML = title; + bodyRow.insertCell(1).innerHTML = dateCreated; + bodyRow.insertCell(2).innerHTML = state; + bodyRow.insertCell(3).innerHTML = dateSubmitted; + rowsInserted++; + } + + if (rowsInserted === 0) { + // Empty report list + const p = document.createElement("p"); + p.innerHTML = "No reports found."; + cardBody.appendChild(p); + } else { + // Create table header, add to table, and append result to the card body + const thead = document.createElement("thead"); + const tr = document.createElement("tr"); + + const headTitle = document.createElement("th"); + headTitle.innerHTML = "Title"; + tr.appendChild(headTitle); + + const headDateCreated = document.createElement("th"); + headDateCreated.innerHTML = "Date Created"; + tr.appendChild(headDateCreated); + + const headState = document.createElement("th"); + headState.innerHTML = "State"; + tr.appendChild(headState); + + const headDateSubmitted = document.createElement("th") + headDateSubmitted.innerHTML = "Date Submitted"; + tr.appendChild(headDateSubmitted); + + thead.appendChild(tr); + table.prepend(thead); + table.classList.add("table", "table-striped", "table-responsive-sm"); + cardBody.appendChild(table); + } +} + +function getReportHistory(event) { + const token = localStorage.getItem("token"); + const url = "https://localhost:8444/backend/list_report" + const xhr = new XMLHttpRequest(); + + xhr.open("GET", url, true); + xhr.setRequestHeader("Authorization", `Token ${token}`); + xhr.onreadystatechange = function() { + if (this.readyState === 4) { + if (this.status === 200) { + console.log("GET list_report SUCCESS!"); + console.log(`Server response:\n${this.response}`); + listOfReports = JSON.parse(this.response); + displayListOfReports(listOfReports); + } else { + console.log("GET list_report FAILURE!"); + console.log(`Server status: ${this.status}`); + console.log(`Server response:\n${this.response}`); + } + } + }; + + xhr.onerror = function() { + alert("Connection error!"); + }; + + xhr.send(); +} + +document.addEventListener("DOMContentLoaded", getReportHistory); diff --git a/front/static/view_history.html b/front/static/view_history.html index 8cefece..23fdccc 100644 --- a/front/static/view_history.html +++ b/front/static/view_history.html @@ -38,10 +38,20 @@ -
-

View report history

+
+
+
+
+
+

Your Report History

+
+
+
+
+
+
+ - From d3acce9b7872e9ac7ca85e67914a8d46cbe600c3 Mon Sep 17 00:00:00 2001 From: Preston Doman Date: Sun, 27 Jan 2019 21:39:48 -0800 Subject: [PATCH 2/3] Change endpoint url depending on detected OS --- front/static/js/viewHistory.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/front/static/js/viewHistory.js b/front/static/js/viewHistory.js index bc037b6..d0ad888 100644 --- a/front/static/js/viewHistory.js +++ b/front/static/js/viewHistory.js @@ -1,3 +1,30 @@ +// Hack to change endpoint url for each OS +function getEndpointDomain() { + let OSName; + let domain; + + if (navigator.appVersion.indexOf("Win") !== -1) + OSName = "Windows"; + else if (navigator.appVersion.indexOf("Mac") !== -1) + OSName = "MacOS"; + else if (navigator.appVersion.indexOf("X11") !== -1) + OSName = "UNIX"; + else if (navigator.appVersion.indexOf("Linux") !== -1) + OSName = "Linux"; + else + OSName = "Unknown OS"; + + console.log(`Detected operating system: ${OSName}`); + + if (OSName === "Windows") { + domain = "https://192.168.99.100:8444/"; + } else { + domain = "https://localhost:8444/" + } + + return domain; +} + function displayListOfReports(listOfReports) { const cardBody = document.querySelector(".card-body"); const table = document.createElement("table"); @@ -53,9 +80,11 @@ function displayListOfReports(listOfReports) { function getReportHistory(event) { const token = localStorage.getItem("token"); - const url = "https://localhost:8444/backend/list_report" + const url = getEndpointDomain() + "backend/list_report"; const xhr = new XMLHttpRequest(); + console.log(`Attempting a connection to the following endpoint: ${url}`); + xhr.open("GET", url, true); xhr.setRequestHeader("Authorization", `Token ${token}`); xhr.onreadystatechange = function() { From d4b569187e3087d271d9b07ee9622c11d1a706e6 Mon Sep 17 00:00:00 2001 From: Preston Doman Date: Sun, 27 Jan 2019 21:40:49 -0800 Subject: [PATCH 3/3] Remove setRequestHeaders line --- front/static/js/viewHistory.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/front/static/js/viewHistory.js b/front/static/js/viewHistory.js index d0ad888..983620a 100644 --- a/front/static/js/viewHistory.js +++ b/front/static/js/viewHistory.js @@ -86,7 +86,7 @@ function getReportHistory(event) { console.log(`Attempting a connection to the following endpoint: ${url}`); xhr.open("GET", url, true); - xhr.setRequestHeader("Authorization", `Token ${token}`); + //xhr.setRequestHeader("Authorization", `Token ${token}`); xhr.onreadystatechange = function() { if (this.readyState === 4) { if (this.status === 200) {