From 13ba7048240c054005e3891793dee7bc4b006c47 Mon Sep 17 00:00:00 2001
From: Preston Doman <pdoman@pdx.edu>
Date: Sat, 16 Feb 2019 16:07:43 -0800
Subject: [PATCH 1/7] Fix empty report table message

---
 front/static/js/viewHistory.js | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/front/static/js/viewHistory.js b/front/static/js/viewHistory.js
index 45407a3..fdf1d2e 100644
--- a/front/static/js/viewHistory.js
+++ b/front/static/js/viewHistory.js
@@ -290,14 +290,15 @@ function createReportForm(parsedData, type) {
 
 function displayListOfReports(parsedData) {
     const reports = parsedData.reports;
+    const table = document.querySelector("table");
 
     if (reports.length === 0) {
         const cardBody = document.querySelector(".card-body");
-        const p = document.createElement("p");
-        p.innerHTML = "No reports found.";
-        cardBody.appendChild(p);
+        const h4 = document.createElement("h4");
+        h4.innerHTML = "No reports found.";
+        h4.classList.add("text-center");
+        cardBody.insertBefore(h4, table);
     } else {
-        const table = document.querySelector("table");
         const tbody = document.querySelector("tbody");
 
         // Insert data into the table row

From e1873fe4aea3b41436a23f459054b236ed4f5283 Mon Sep 17 00:00:00 2001
From: Preston Doman <pdoman@pdx.edu>
Date: Sat, 16 Feb 2019 16:41:11 -0800
Subject: [PATCH 2/7] Add loading spinner to report table

---
 front/static/edit_report.html  |  3 +++
 front/static/js/viewHistory.js | 15 ++++++++++-----
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/front/static/edit_report.html b/front/static/edit_report.html
index 9cf6b27..2ccac85 100644
--- a/front/static/edit_report.html
+++ b/front/static/edit_report.html
@@ -45,6 +45,9 @@
                         <h3>Your Report History</h3>
                     </div>
                     <div class="card-body">
+                        <div class="text-center">
+                            <i class="fas fa-spinner fa-3x fa-spin"></i>
+                        </div>
                         <table class="table table-striped table-responsive-sm" style="visibility:hidden">
                             <thead>
                                 <tr>
diff --git a/front/static/js/viewHistory.js b/front/static/js/viewHistory.js
index fdf1d2e..fbbdf9f 100644
--- a/front/static/js/viewHistory.js
+++ b/front/static/js/viewHistory.js
@@ -290,14 +290,19 @@ function createReportForm(parsedData, type) {
 
 function displayListOfReports(parsedData) {
     const reports = parsedData.reports;
+    const cardBody = document.querySelector(".card-body");
     const table = document.querySelector("table");
 
+    console.log(cardBody);
+    console.log(cardBody.firstElementChild);
+    cardBody.removeChild(cardBody.firstElementChild); // remove loading spinner
+    
     if (reports.length === 0) {
-        const cardBody = document.querySelector(".card-body");
-        const h4 = document.createElement("h4");
-        h4.innerHTML = "No reports found.";
-        h4.classList.add("text-center");
-        cardBody.insertBefore(h4, table);
+        cardBody.removeChild(table);
+        const h5 = document.createElement("h5");
+        h5.innerHTML = "No reports found.";
+        h5.classList.add("text-center");
+        cardBody.appendChild(h5);
     } else {
         const tbody = document.querySelector("tbody");
 

From 30676d35ab86d870562c276ca53933a651dbe78b Mon Sep 17 00:00:00 2001
From: Preston Doman <pdoman@pdx.edu>
Date: Sat, 16 Feb 2019 18:42:19 -0800
Subject: [PATCH 3/7] Add function to refresh page on new report close

---
 front/static/js/viewHistory.js | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/front/static/js/viewHistory.js b/front/static/js/viewHistory.js
index fbbdf9f..b19c429 100644
--- a/front/static/js/viewHistory.js
+++ b/front/static/js/viewHistory.js
@@ -475,3 +475,10 @@ document.addEventListener("submit", function(event) {
         makeAjaxRequest("PUT", url, alertCallback, null, formData);
     }
 });
+
+// Jquery is required to handle this modal event
+$(document).ready(function(){
+    $("#newReportModal").on('hidden.bs.modal', function() {
+        location.reload(true);
+    });
+});

From 8f98dc06ecc1f903a969edd49718518af0ae3c34 Mon Sep 17 00:00:00 2001
From: Preston Doman <pdoman@pdx.edu>
Date: Sat, 16 Feb 2019 18:56:16 -0800
Subject: [PATCH 4/7] Remove logging statements

---
 front/static/js/viewHistory.js | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/front/static/js/viewHistory.js b/front/static/js/viewHistory.js
index b19c429..59a95ee 100644
--- a/front/static/js/viewHistory.js
+++ b/front/static/js/viewHistory.js
@@ -292,9 +292,6 @@ function displayListOfReports(parsedData) {
     const reports = parsedData.reports;
     const cardBody = document.querySelector(".card-body");
     const table = document.querySelector("table");
-
-    console.log(cardBody);
-    console.log(cardBody.firstElementChild);
     cardBody.removeChild(cardBody.firstElementChild); // remove loading spinner
     
     if (reports.length === 0) {

From 20d3b08653b93612288cf4e58db5e267ecd07d7b Mon Sep 17 00:00:00 2001
From: Preston Doman <pdoman@pdx.edu>
Date: Sat, 16 Feb 2019 19:58:55 -0800
Subject: [PATCH 5/7] Add spinner to the save button

---
 front/static/js/viewHistory.js | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/front/static/js/viewHistory.js b/front/static/js/viewHistory.js
index 59a95ee..e30c56a 100644
--- a/front/static/js/viewHistory.js
+++ b/front/static/js/viewHistory.js
@@ -9,11 +9,13 @@ function getEndpointDomain() {
     return "https://" + window.location.hostname + ":8444/";
 }
 
-function alertCallback(parsedData) {
+function saveSectionCallback(parsedData, saveButton) {
     alert(JSON.stringify(parsedData));
+    saveButton.innerHTML = "Save";
+    saveButton.disabled = false;
 }
 
-function makeAjaxRequest(method, url, callback, type, payload) {
+function makeAjaxRequest(method, url, callback, optional, payload) {
     const token = localStorage.getItem("token");
     const xhr = new XMLHttpRequest();
 
@@ -39,7 +41,7 @@ function makeAjaxRequest(method, url, callback, type, payload) {
                 console.log(method + " SUCCESS!");
                 console.log("Server response:\n" + this.response);
                 let parsedData = JSON.parse(this.response);
-                type ? callback(parsedData, type) : callback(parsedData);
+                optional ? callback(parsedData, optional) : callback(parsedData);
             } else {
                 console.error(method + " FAILURE!");
                 console.error("Server status: " + this.status);
@@ -467,9 +469,17 @@ document.addEventListener("submit", function(event) {
     if (event.target.classList.contains("section-form")) {
         event.preventDefault();
         console.log(event.target);
+        console.log(event.target.lastElementChild);
+        let saveButton = event.target.lastElementChild;
+        saveButton.disabled = true;
+        saveButton.innerHTML = "";
+        let span = document.createElement("span");
+        span.classList.add("spinner-border", "spinner-border-sm");
+        saveButton.appendChild(span); 
+        saveButton.appendChild(document.createTextNode("  Saving..."));
         const formData = new FormData(event.target);
         const url = getEndpointDomain() + "api/v1/report/" + event.target.dataset.rid + "/section/" + event.target.dataset.sid;
-        makeAjaxRequest("PUT", url, alertCallback, null, formData);
+        makeAjaxRequest("PUT", url, saveSectionCallback, saveButton, formData);
     }
 });
 

From 48ea8d59b9c2a4133bf9324e82a67cfeaba9c62f Mon Sep 17 00:00:00 2001
From: Preston Doman <pdoman@pdx.edu>
Date: Sat, 16 Feb 2019 20:05:32 -0800
Subject: [PATCH 6/7] Remove console logs

---
 front/static/js/viewHistory.js | 2 --
 1 file changed, 2 deletions(-)

diff --git a/front/static/js/viewHistory.js b/front/static/js/viewHistory.js
index e30c56a..4cf4048 100644
--- a/front/static/js/viewHistory.js
+++ b/front/static/js/viewHistory.js
@@ -468,8 +468,6 @@ document.addEventListener("input", function(event) {
 document.addEventListener("submit", function(event) {
     if (event.target.classList.contains("section-form")) {
         event.preventDefault();
-        console.log(event.target);
-        console.log(event.target.lastElementChild);
         let saveButton = event.target.lastElementChild;
         saveButton.disabled = true;
         saveButton.innerHTML = "";

From 440f54db85e2290317e0766a35a6081204f1b180 Mon Sep 17 00:00:00 2001
From: sliang17 <sliang@pdx.edu>
Date: Mon, 18 Feb 2019 20:14:48 -0800
Subject: [PATCH 7/7] Update the submit report button

---
 front/static/edit_report.html  |  2 +-
 front/static/js/viewHistory.js | 21 +++++++++++++++++++--
 front/static/new_report.html   |  2 +-
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/front/static/edit_report.html b/front/static/edit_report.html
index 2ccac85..1ff63e6 100644
--- a/front/static/edit_report.html
+++ b/front/static/edit_report.html
@@ -86,7 +86,7 @@
                 <div class="modal-footer">
                     <button type="button" class="btn btn-danger delete-report">Delete Report</button>
                     <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
-                    <button type="button" class="btn btn-primary">Submit Report</button>
+                    <button type="button" class="btn btn-primary submit-report-button">Submit Report</button>
                 </div>
             </div>
         </div>
diff --git a/front/static/js/viewHistory.js b/front/static/js/viewHistory.js
index 4cf4048..a5ad6cf 100644
--- a/front/static/js/viewHistory.js
+++ b/front/static/js/viewHistory.js
@@ -225,6 +225,12 @@ function createReportForm(parsedData, type) {
     const accordion = document.createElement("div");
     accordion.classList.add("accordion");
 
+    //submit button
+    const submitButton = document.querySelector(".submit-report-button");
+    if (submitButton) {
+        submitButton.setAttribute("data-rid", parsedData.report_pk);
+    }
+
     if (type === reportType.EDIT) {
         modalBody = document.querySelector("#editReportModalBody");
         modalLabel = document.querySelector("#editReportModalLabel");
@@ -295,7 +301,7 @@ function displayListOfReports(parsedData) {
     const cardBody = document.querySelector(".card-body");
     const table = document.querySelector("table");
     cardBody.removeChild(cardBody.firstElementChild); // remove loading spinner
-    
+
     if (reports.length === 0) {
         cardBody.removeChild(table);
         const h5 = document.createElement("h5");
@@ -427,6 +433,17 @@ document.addEventListener("click", function(event) {
             console.log("View button clicked");
             const url = getEndpointDomain() + "api/v1/report/" + event.target.dataset.rid;
             makeAjaxRequest("GET", url, displayReport);
+        } else if (event.target.classList.contains("submit-report-button")) {
+            event.preventDefault();
+            //const title = document.querySelector("#editReportModalLabel").textContent;
+            const result = confirm("Are you sure you want to submit the report ?");
+            if (result) {
+                const url = getEndpointDomain() + "api/v1/report/" + event.target.dataset.rid;
+                makeAjaxRequest("PUT", url, function(parsedData) {
+                    alert(parsedData.message);
+                    location.reload(true);
+                });
+            }
         } else if (event.target.classList.contains("delete-report")) {
             event.preventDefault();
             const title = document.querySelector("#editReportModalLabel").textContent;
@@ -473,7 +490,7 @@ document.addEventListener("submit", function(event) {
         saveButton.innerHTML = "";
         let span = document.createElement("span");
         span.classList.add("spinner-border", "spinner-border-sm");
-        saveButton.appendChild(span); 
+        saveButton.appendChild(span);
         saveButton.appendChild(document.createTextNode("  Saving..."));
         const formData = new FormData(event.target);
         const url = getEndpointDomain() + "api/v1/report/" + event.target.dataset.rid + "/section/" + event.target.dataset.sid;
diff --git a/front/static/new_report.html b/front/static/new_report.html
index dc9ca28..00aee91 100644
--- a/front/static/new_report.html
+++ b/front/static/new_report.html
@@ -76,7 +76,7 @@
                 </div>
                 <div class="modal-footer">
                     <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
-                    <button type="button" class="btn btn-primary">Submit Report</button>
+                    <button type="button" class="btn btn-primary submit-report-button" >Submit Report</button>
                 </div>
             </div>
         </div>