reimbursinator/front/static/js/viewHistory.js

337 lines
12 KiB
JavaScript
Raw Normal View History

// Hack to change endpoint url
function getEndpointDomain() {
return "https://" + window.location.hostname + ":8444/";
}
// Make a GET request to url and pass response to callback function
2019-01-30 23:42:09 +00:00
function getDataFromEndpoint(url, callback) {
const token = localStorage.getItem("token");
const xhr = new XMLHttpRequest();
console.log("Attempting a connection to the following endpoint: " + url);
2019-01-30 23:42:09 +00:00
2019-02-05 09:05:04 +00:00
2019-01-30 23:42:09 +00:00
xhr.open("GET", url, true);
2019-02-05 09:05:04 +00:00
xhr.setRequestHeader("Authorization", "Bearer " + token);
2019-01-30 23:42:09 +00:00
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
console.log("GET SUCCESS!");
console.log("Server response:\n" + this.response);
2019-01-30 23:42:09 +00:00
parsedData = JSON.parse(this.response);
callback(parsedData);
} else {
console.error("GET FAILURE!");
console.error("Server status: " + this.status);
console.error("Server response:\n" + this.response);
2019-01-30 23:42:09 +00:00
}
}
};
xhr.onerror = function() {
alert("Connection error!");
};
xhr.send();
}
// Wraps a Bootstrap form group around a field
function createFormGroup(key, field) {
2019-02-01 00:27:29 +00:00
const formGroup = document.createElement("div")
2019-02-03 03:05:39 +00:00
formGroup.classList.add("form-group", "row");
2019-02-01 00:27:29 +00:00
const label = document.createElement("label");
2019-02-03 21:47:12 +00:00
label.classList.add("col-sm-4", "col-form");
label.innerHTML = field.label + ": ";
label.setAttribute("for", key);
2019-02-03 03:05:39 +00:00
const div = document.createElement("div");
2019-02-03 21:47:12 +00:00
div.classList.add("col-sm-6");
2019-02-01 00:27:29 +00:00
const input = document.createElement("input");
input.name = key;
input.id = key;
switch(field.type) {
case "boolean":
input.type = "checkbox";
if (field.value === true)
input.setAttribute("checked", "checked");
2019-02-01 01:48:36 +00:00
input.classList.add("form-check-input");
2019-02-03 03:05:39 +00:00
label.className = "";
2019-02-01 00:27:29 +00:00
label.classList.add("form-check-label");
2019-02-03 03:05:39 +00:00
outerLabel = document.createElement("div");
2019-02-03 21:47:12 +00:00
outerLabel.classList.add("col-sm-4");
outerLabel.innerHTML = "Flight type: ";
2019-02-03 03:05:39 +00:00
formCheck = document.createElement("div");
formCheck.classList.add("form-check");
formCheck.appendChild(input);
formCheck.appendChild(label);
div.appendChild(formCheck);
formGroup.appendChild(outerLabel);
formGroup.appendChild(div);
break;
case "date":
case "decimal":
input.type = "text";
input.value = field.value;
2019-02-01 01:48:36 +00:00
input.classList.add("form-control");
formGroup.appendChild(label);
2019-02-03 03:05:39 +00:00
div.appendChild(input)
formGroup.appendChild(div);
break;
case "file":
input.type = "file";
2019-02-01 01:48:36 +00:00
input.classList.add("form-control-file");
2019-02-03 03:05:39 +00:00
let uploadMessage = document.createElement("p");
uploadMessage.classList.add("form-text");
uploadMessage.innerHTML = field.value;
div.appendChild(input)
div.appendChild(uploadMessage);
formGroup.appendChild(label);
2019-02-03 03:05:39 +00:00
formGroup.appendChild(div);
break;
default:
break;
}
return formGroup;
}
function createCollapsibleCard(key, sectionTitle) {
// Create card and header
const card = document.createElement("div");
card.classList.add("card");
const cardHeader = document.createElement("div");
cardHeader.classList.add("card-header");
// Create h2, button. Append button to h2, h2 to header, and header to card
const h2 = document.createElement("h2");
const button = document.createElement("button");
button.classList.add("btn", "btn-link");
button.type = "button";
button.setAttribute("data-toggle", "collapse");
button.setAttribute("data-target", "#collapse" + key);
button.innerHTML = sectionTitle;
h2.appendChild(button);
cardHeader.appendChild(h2);
card.appendChild(cardHeader);
return card;
}
function createCollapsibleCardBody(key, form, sectionDescription, sectionCompleted) {
2019-02-01 00:27:29 +00:00
// Create wrapper div
const div = document.createElement("div");
const sectionAlert = document.createElement("div");
if (sectionCompleted) {
div.classList.add("collapse");
sectionAlert.classList.add("alert", "alert-success");
sectionAlert.innerHTML = "This section is complete";
} else {
div.classList.add("collapse", "show");
sectionAlert.classList.add("alert", "alert-danger");
sectionAlert.innerHTML = "This section is not complete";
}
div.setAttribute("data-parent", "#editReportAccordion");
div.id = "collapse" + key;
2019-02-01 00:27:29 +00:00
// Create card body. Append form to body, body to wrapper div
const cardBody = document.createElement("div");
cardBody.classList.add("card-body");
cardBody.appendChild(sectionAlert);
2019-02-01 00:27:29 +00:00
cardBody.insertAdjacentHTML("beforeend", sectionDescription);
cardBody.appendChild(form);
div.appendChild(cardBody);
return div;
}
function createEditReportForm(parsedData) {
const modalBody = document.querySelector(".modal-body");
const modalLabel = document.querySelector("#editReportModalLabel");
2019-01-30 23:42:09 +00:00
while (modalBody.firstChild) {
modalBody.removeChild(modalBody.firstChild);
2019-01-30 23:42:09 +00:00
}
2019-02-03 03:05:39 +00:00
// Add report title and date
2019-02-01 01:48:36 +00:00
const reportTitle = parsedData.title;
const dateCreated = new Date(parsedData.date_created).toLocaleDateString("en-US");
modalLabel.innerHTML = reportTitle + " " + dateCreated;
// Create accordion
const accordion = document.createElement("div");
accordion.classList.add("accordion");
accordion.id = "editReportAccordion";
2019-02-01 08:07:08 +00:00
// Traverse the report's sections array
2019-01-30 23:42:09 +00:00
const sections = parsedData.sections;
for (let key in sections) {
let section = sections[key];
2019-02-01 08:07:08 +00:00
let collapsibleCard = createCollapsibleCard(key, section.title)
// Create a new form with the section key index as id
let form = document.createElement("form");
form.classList.add("form");
form.id = "form" + key;
// Traverse the fields of this section
let fields = section.fields;
for (let key in fields) {
let field = fields[key];
2019-02-01 08:07:08 +00:00
console.log("Field label: " + field.label);
console.log("Field type: " + field.type);
console.log("Field value: " + field.value);
2019-01-30 23:42:09 +00:00
2019-02-01 08:07:08 +00:00
// Create a form group for each field and add it to the form
let formGroup = createFormGroup(key, field);
form.appendChild(formGroup);
2019-01-30 23:42:09 +00:00
}
2019-02-01 08:07:08 +00:00
// Add save button to the current form
2019-02-01 01:48:36 +00:00
let saveButton = document.createElement("button");
saveButton.innerHTML = "Save";
saveButton.type = "submit";
saveButton.classList.add("btn", "btn-primary"); // TODO: add eventListener
form.appendChild(saveButton);
2019-02-01 00:27:29 +00:00
// Create collapsible card body, append form to it, append card to accordion
let cardBody = createCollapsibleCardBody(key, form, section.html_description, section.completed);
2019-02-01 08:07:08 +00:00
collapsibleCard.appendChild(cardBody);
accordion.appendChild(collapsibleCard);
}
modalBody.appendChild(accordion);
2019-01-30 23:42:09 +00:00
}
function displayListOfReports(parsedData) {
const reports = parsedData.reports;
2019-02-02 22:22:18 +00:00
if (reports.length === 0) {
const cardBody = document.querySelector(".card-body");
const p = document.createElement("p");
p.innerHTML = "No reports found.";
cardBody.appendChild(p);
} else {
2019-02-02 22:22:18 +00:00
const table = document.querySelector("table");
const tbody = document.querySelector("tbody");
// Insert data into the table row
for (let i = 0; i < reports.length; i++) {
let title = reports[i].title;
let dateCreated = new Date(reports[i].date_created).toLocaleDateString("en-US");
2019-02-05 18:12:30 +00:00
let state = reports[i].submitted;
2019-02-02 22:22:18 +00:00
let dateSubmitted;
let rid = reports[i].report_pk;
let bodyRow = tbody.insertRow(i);
bodyRow.insertCell(0).innerHTML = title;
bodyRow.insertCell(1).innerHTML = dateCreated;
let stateCell = bodyRow.insertCell(2);
2019-02-05 18:12:30 +00:00
stateCell.innerHTML = state;
2019-02-02 22:22:18 +00:00
stateCell.classList.add("d-none", "d-lg-table-cell"); // Column visible only on large displays
// Create edit/view button
let actionButton = document.createElement("button");
actionButton.type = "submit";
actionButton.setAttribute("data-rid", rid);
actionButton.classList.add("btn");
2019-02-05 18:12:30 +00:00
if (state === false) {
2019-02-02 22:22:18 +00:00
// Edit button
dateSubmitted = "TBD";
actionButton.classList.add("btn-primary", "edit-report-button"); // Add event listener class
2019-02-02 22:22:18 +00:00
actionButton.innerHTML = "Edit";
actionButton.setAttribute("data-toggle", "modal");
actionButton.setAttribute("data-target", "#editReportModal");
2019-02-02 22:22:18 +00:00
} else {
// View button
dateSubmitted = new Date(reports[i].date_submitted).toLocaleDateString("en-US");
actionButton.classList.add("btn-success", "view-report-button");
2019-02-02 22:22:18 +00:00
actionButton.innerHTML = "View";
actionButton.setAttribute("data-toggle", "modal");
actionButton.setAttribute("data-target", "#viewReportModal");
2019-02-02 22:22:18 +00:00
}
let dateSubmittedCell = bodyRow.insertCell(3);
dateSubmittedCell.innerHTML = dateSubmitted;
dateSubmittedCell.classList.add("d-none", "d-md-table-cell"); // Column visible on medium and larger displays
bodyRow.insertCell(4).appendChild(actionButton);
}
table.style.visibility = "visible";
}
}
function displayReport(parsedData){
//Able to get the correct report ID now just needs to display the
//report as an modual
const modalBody = document.querySelector(".modal-view");
const modalLabel = document.querySelector("#viewReportModalLabel");
while (modalBody.firstChild) {
modalBody.removeChild(modalBody.firstChild);
}
// Add report title and date
const reportTitle = parsedData.title;
const dateCreated = new Date(parsedData.date_created).toLocaleDateString("en-US");
modalLabel.innerHTML = reportTitle + " " + dateCreated;
const card = document.createElement("div");
card.classList.add("card");
const cardHeader = document.createElement("div");
cardHeader.classList.add("card-header");
const cardBody = document.createElement("div");
cardBody.classList.add("card-body");
const sections = parsedData.sections;
for (let key in sections) {
let section = sections[key];
const h4 = document.createElement("h4");
const value = document.createTextNode(section.title);
h4.appendChild(value);
cardBody.appendChild(h4);
let fields = section.fields;
for (let key in fields) {
let field = fields[key];
const p1 = document.createElement("p");
const p1Value = document.createTextNode(field.label + ": " +field.value);
p1.appendChild(p1Value);
cardBody.appendChild(p1);
}
cardHeader.appendChild(cardBody);
card.appendChild(cardHeader);
}
modalBody.appendChild(card);
}
document.addEventListener("DOMContentLoaded", function(event) {
2019-01-30 23:42:09 +00:00
const url = getEndpointDomain() + "api/v1/reports";
getDataFromEndpoint(url, displayListOfReports);
});
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;
getDataFromEndpoint(url, createEditReportForm);
}
if(event.target && event.target.classList.contains("view-report-button"))
{
console.log("View button clicked");
const url = getEndpointDomain() + "api/v1/report/" + event.target.dataset.rid;
getDataFromEndpoint(url, displayReport);
}
// TODO: Add view report
});