2019-02-10 06:23:08 +00:00
|
|
|
const reportType = {
|
|
|
|
NEW : 1,
|
|
|
|
EDIT : 2,
|
|
|
|
VIEW : 3
|
|
|
|
};
|
|
|
|
|
2019-02-04 03:03:20 +00:00
|
|
|
// Hack to change endpoint url
|
2019-01-28 05:39:48 +00:00
|
|
|
function getEndpointDomain() {
|
2019-02-04 03:03:20 +00:00
|
|
|
return "https://" + window.location.hostname + ":8444/";
|
2019-01-28 05:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 08:48:35 +00:00
|
|
|
function alertCallback(parsedData) {
|
|
|
|
alert(JSON.stringify(parsedData));
|
|
|
|
}
|
|
|
|
|
2019-02-11 02:15:32 +00:00
|
|
|
function makeAjaxRequest(method, url, callback, type, payload) {
|
2019-02-10 23:46:27 +00:00
|
|
|
const token = localStorage.getItem("token");
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
console.log("Attempting a connection to the following endpoint: " + url);
|
|
|
|
|
2019-02-11 02:15:32 +00:00
|
|
|
xhr.open(method, url, true);
|
2019-02-10 23:46:27 +00:00
|
|
|
xhr.setRequestHeader("Authorization", "Bearer " + token);
|
2019-02-13 08:48:35 +00:00
|
|
|
|
|
|
|
switch (method) {
|
|
|
|
case "PUT":
|
|
|
|
break;
|
|
|
|
case "POST":
|
|
|
|
xhr.setRequestHeader("Content-Type", "application/json");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
payload = null;
|
|
|
|
break;
|
|
|
|
}
|
2019-01-30 23:42:09 +00:00
|
|
|
|
2019-02-10 05:16:53 +00:00
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (this.readyState === 4) {
|
|
|
|
if (this.status === 200) {
|
2019-02-11 02:15:32 +00:00
|
|
|
console.log(method + " SUCCESS!");
|
2019-02-10 05:16:53 +00:00
|
|
|
console.log("Server response:\n" + this.response);
|
|
|
|
let parsedData = JSON.parse(this.response);
|
2019-02-11 02:15:32 +00:00
|
|
|
type ? callback(parsedData, type) : callback(parsedData);
|
2019-02-10 05:16:53 +00:00
|
|
|
} else {
|
2019-02-11 02:15:32 +00:00
|
|
|
console.error(method + " FAILURE!");
|
2019-02-10 05:16:53 +00:00
|
|
|
console.error("Server status: " + this.status);
|
|
|
|
console.error("Server response:\n" + this.response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onerror = function() {
|
|
|
|
alert("Connection error!");
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(payload);
|
|
|
|
}
|
|
|
|
|
2019-02-01 07:49:36 +00:00
|
|
|
// Wraps a Bootstrap form group around a field
|
2019-02-14 23:58:27 +00:00
|
|
|
function createFormGroup(sectionIdStr, field) {
|
|
|
|
const inputId = sectionIdStr + field.field_name;
|
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-01-31 07:54:27 +00:00
|
|
|
|
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 + ": ";
|
2019-02-14 23:36:31 +00:00
|
|
|
label.setAttribute("for", inputId);
|
2019-02-11 06:51:19 +00:00
|
|
|
|
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 07:49:36 +00:00
|
|
|
|
2019-02-01 00:27:29 +00:00
|
|
|
const input = document.createElement("input");
|
2019-02-10 05:16:53 +00:00
|
|
|
input.name = field.field_name;
|
2019-02-14 23:36:31 +00:00
|
|
|
input.id = inputId;
|
2019-01-31 07:54:27 +00:00
|
|
|
|
2019-02-14 01:38:27 +00:00
|
|
|
switch(field.field_type) {
|
2019-01-31 07:54:27 +00:00
|
|
|
case "boolean":
|
2019-02-14 22:17:25 +00:00
|
|
|
const select = document.createElement("select");
|
|
|
|
select.name = field.field_name;
|
2019-02-14 23:36:31 +00:00
|
|
|
select.id = inputId;
|
2019-02-14 22:17:25 +00:00
|
|
|
select.classList.add("form-control");
|
|
|
|
const yesOption = document.createElement("option");
|
|
|
|
yesOption.innerHTML = "Yes";
|
|
|
|
yesOption.value = "true";
|
|
|
|
const noOption = document.createElement("option");
|
|
|
|
noOption.innerHTML = "No";
|
|
|
|
noOption.value = "false";
|
|
|
|
if (field.value === true) {
|
|
|
|
yesOption.setAttribute("selected", "selected");
|
|
|
|
} else {
|
|
|
|
noOption.setAttribute("selected", "selected");
|
|
|
|
}
|
|
|
|
select.appendChild(yesOption);
|
|
|
|
select.appendChild(noOption);
|
|
|
|
formGroup.appendChild(label);
|
|
|
|
div.appendChild(select)
|
2019-02-03 03:05:39 +00:00
|
|
|
formGroup.appendChild(div);
|
2019-01-31 07:54:27 +00:00
|
|
|
break;
|
|
|
|
case "date":
|
2019-02-10 05:16:53 +00:00
|
|
|
case "string":
|
|
|
|
input.type = "text";
|
|
|
|
input.value = field.value;
|
|
|
|
input.classList.add("form-control");
|
|
|
|
formGroup.appendChild(label);
|
|
|
|
div.appendChild(input)
|
|
|
|
formGroup.appendChild(div);
|
|
|
|
break;
|
2019-01-31 07:54:27 +00:00
|
|
|
case "decimal":
|
|
|
|
input.type = "text";
|
|
|
|
input.value = field.value;
|
2019-02-01 01:48:36 +00:00
|
|
|
input.classList.add("form-control");
|
2019-02-10 05:16:53 +00:00
|
|
|
input.pattern = "\\d+(\\.\\d{2})?";
|
|
|
|
formGroup.appendChild(label);
|
|
|
|
div.appendChild(input)
|
|
|
|
formGroup.appendChild(div);
|
|
|
|
break;
|
|
|
|
case "integer":
|
|
|
|
input.type = "number";
|
|
|
|
input.value = field.value;
|
|
|
|
input.classList.add("form-control");
|
|
|
|
input.step = 1;
|
|
|
|
input.min = 0;
|
2019-01-31 07:54:27 +00:00
|
|
|
formGroup.appendChild(label);
|
2019-02-03 03:05:39 +00:00
|
|
|
div.appendChild(input)
|
|
|
|
formGroup.appendChild(div);
|
2019-01-31 07:54:27 +00:00
|
|
|
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);
|
2019-02-01 07:49:36 +00:00
|
|
|
formGroup.appendChild(label);
|
2019-02-03 03:05:39 +00:00
|
|
|
formGroup.appendChild(div);
|
2019-01-31 07:54:27 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return formGroup;
|
|
|
|
}
|
|
|
|
|
2019-02-14 23:58:27 +00:00
|
|
|
function createCollapsibleCard(sectionIdStr, sectionTitle) {
|
2019-01-31 23:48:33 +00:00
|
|
|
// 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");
|
2019-02-14 23:58:27 +00:00
|
|
|
button.setAttribute("data-target", "#" + sectionIdStr + "collapse");
|
2019-01-31 23:48:33 +00:00
|
|
|
button.innerHTML = sectionTitle;
|
|
|
|
h2.appendChild(button);
|
|
|
|
cardHeader.appendChild(h2);
|
|
|
|
card.appendChild(cardHeader);
|
|
|
|
|
|
|
|
return card;
|
|
|
|
}
|
|
|
|
|
2019-02-14 23:58:27 +00:00
|
|
|
function createCollapsibleCardBody(form, type, sectionIdStr, sectionDescription, sectionCompleted) {
|
2019-02-01 00:27:29 +00:00
|
|
|
// Create wrapper div
|
2019-01-31 23:48:33 +00:00
|
|
|
const div = document.createElement("div");
|
2019-02-14 23:58:27 +00:00
|
|
|
div.id = sectionIdStr + "collapse";
|
2019-02-04 03:03:20 +00:00
|
|
|
const sectionAlert = document.createElement("div");
|
2019-02-04 09:39:42 +00:00
|
|
|
const cardBody = document.createElement("div");
|
|
|
|
cardBody.classList.add("card-body");
|
2019-02-04 03:03:20 +00:00
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2019-02-04 09:39:42 +00:00
|
|
|
if (type === reportType.EDIT) {
|
|
|
|
div.setAttribute("data-parent", "#editReportAccordion");
|
|
|
|
} else {
|
|
|
|
div.setAttribute("data-parent", "#newReportAccordion");
|
|
|
|
}
|
2019-02-01 00:27:29 +00:00
|
|
|
|
|
|
|
// Create card body. Append form to body, body to wrapper div
|
2019-02-04 03:03:20 +00:00
|
|
|
cardBody.appendChild(sectionAlert);
|
2019-02-11 06:51:19 +00:00
|
|
|
cardBody.insertAdjacentHTML("beforeend", sectionDescription);
|
2019-01-31 23:48:33 +00:00
|
|
|
cardBody.appendChild(form);
|
|
|
|
div.appendChild(cardBody);
|
2019-02-11 06:51:19 +00:00
|
|
|
|
2019-01-31 23:48:33 +00:00
|
|
|
return div;
|
|
|
|
}
|
2019-01-31 07:54:27 +00:00
|
|
|
|
2019-02-04 09:39:42 +00:00
|
|
|
function createReportForm(parsedData, type) {
|
|
|
|
let modalBody;
|
2019-02-14 22:21:43 +00:00
|
|
|
let modalLabel;
|
2019-02-04 09:39:42 +00:00
|
|
|
const accordion = document.createElement("div");
|
|
|
|
accordion.classList.add("accordion");
|
|
|
|
|
|
|
|
if (type === reportType.EDIT) {
|
|
|
|
modalBody = document.querySelector("#editReportModalBody");
|
|
|
|
modalLabel = document.querySelector("#editReportModalLabel");
|
|
|
|
accordion.id = "editReportAccordion";
|
2019-02-11 06:51:19 +00:00
|
|
|
const deleteButton = document.querySelector(".delete-report");
|
|
|
|
if (deleteButton) {
|
|
|
|
deleteButton.setAttribute("data-rid", parsedData.report_pk);
|
|
|
|
}
|
2019-02-04 09:39:42 +00:00
|
|
|
} else if (type === reportType.NEW) {
|
|
|
|
modalBody = document.querySelector("#newReportModalBody");
|
|
|
|
modalLabel = document.querySelector("#newReportModalLabel");
|
|
|
|
accordion.id = "newReportAccordion";
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-30 23:42:09 +00:00
|
|
|
|
2019-02-04 03:03:20 +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;
|
2019-02-11 02:15:32 +00:00
|
|
|
modalLabel.innerHTML = reportTitle;
|
2019-01-31 07:54:27 +00:00
|
|
|
|
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;
|
2019-02-14 23:36:31 +00:00
|
|
|
for (let i = 0; i < sections.length; i++) {
|
2019-02-14 23:58:27 +00:00
|
|
|
let sectionIdStr = "section-" + sections[i].id + "-";
|
|
|
|
let collapsibleCard = createCollapsibleCard(sectionIdStr, sections[i].title)
|
2019-01-31 23:48:33 +00:00
|
|
|
|
|
|
|
// Create a new form with the section key index as id
|
|
|
|
let form = document.createElement("form");
|
2019-02-13 08:48:35 +00:00
|
|
|
form.classList.add("form", "section-form");
|
2019-02-14 23:58:27 +00:00
|
|
|
form.id = sectionIdStr + "form";
|
2019-02-13 08:48:35 +00:00
|
|
|
form.setAttribute("data-rid", parsedData.report_pk);
|
2019-02-14 23:36:31 +00:00
|
|
|
form.setAttribute("data-sid", sections[i].id);
|
2019-01-31 23:48:33 +00:00
|
|
|
|
|
|
|
// Traverse the fields of this section
|
2019-02-14 23:36:31 +00:00
|
|
|
let fields = sections[i].fields;
|
|
|
|
for (let j = 0; j < fields.length; j++) {
|
2019-02-01 08:07:08 +00:00
|
|
|
|
2019-02-14 23:36:31 +00:00
|
|
|
console.log("Field label: " + fields[j].label);
|
|
|
|
console.log("Field type: " + fields[j].field_type);
|
|
|
|
console.log("Field value: " + fields[j].value);
|
2019-02-11 06:51:19 +00:00
|
|
|
|
2019-02-01 08:07:08 +00:00
|
|
|
// Create a form group for each field and add it to the form
|
2019-02-14 23:58:27 +00:00
|
|
|
form.appendChild(createFormGroup(sectionIdStr, fields[j]));
|
2019-01-30 23:42:09 +00:00
|
|
|
}
|
2019-01-31 07:54:27 +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";
|
2019-02-14 23:36:31 +00:00
|
|
|
saveButton.classList.add("btn", "btn-primary", "save-section");
|
2019-02-01 01:48:36 +00:00
|
|
|
form.appendChild(saveButton);
|
|
|
|
|
2019-02-01 00:27:29 +00:00
|
|
|
// Create collapsible card body, append form to it, append card to accordion
|
2019-02-14 23:58:27 +00:00
|
|
|
let cardBody = createCollapsibleCardBody(form, type, sectionIdStr, sections[i].html_description, sections[i].completed);
|
2019-02-11 06:51:19 +00:00
|
|
|
collapsibleCard.appendChild(cardBody);
|
2019-02-01 08:07:08 +00:00
|
|
|
accordion.appendChild(collapsibleCard);
|
2019-01-31 23:48:33 +00:00
|
|
|
}
|
2019-02-11 06:51:19 +00:00
|
|
|
|
2019-02-04 03:03:20 +00:00
|
|
|
modalBody.appendChild(accordion);
|
2019-01-30 23:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function displayListOfReports(parsedData) {
|
|
|
|
const reports = parsedData.reports;
|
2019-01-28 00:21:51 +00:00
|
|
|
|
2019-02-02 22:22:18 +00:00
|
|
|
if (reports.length === 0) {
|
|
|
|
const cardBody = document.querySelector(".card-body");
|
2019-01-28 00:21:51 +00:00
|
|
|
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;
|
|
|
|
|
2019-02-11 06:51:19 +00:00
|
|
|
let bodyRow = tbody.insertRow(i);
|
2019-02-02 22:22:18 +00:00
|
|
|
bodyRow.insertCell(0).innerHTML = title;
|
2019-02-11 06:51:19 +00:00
|
|
|
bodyRow.insertCell(1).innerHTML = dateCreated;
|
2019-02-02 22:22:18 +00:00
|
|
|
|
|
|
|
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";
|
2019-02-02 23:02:09 +00:00
|
|
|
actionButton.classList.add("btn-primary", "edit-report-button"); // Add event listener class
|
2019-02-02 22:22:18 +00:00
|
|
|
actionButton.innerHTML = "Edit";
|
2019-02-04 03:03:20 +00:00
|
|
|
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");
|
2019-02-05 23:31:55 +00:00
|
|
|
actionButton.classList.add("btn-success", "view-report-button");
|
2019-02-02 22:22:18 +00:00
|
|
|
actionButton.innerHTML = "View";
|
2019-02-09 01:26:45 +00:00
|
|
|
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";
|
2019-01-28 00:21:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 23:31:55 +00:00
|
|
|
function displayReport(parsedData){
|
2019-02-09 01:26:45 +00:00
|
|
|
//Able to get the correct report ID now just needs to display the
|
2019-02-05 23:31:55 +00:00
|
|
|
//report as an modual
|
2019-02-09 01:26:45 +00:00
|
|
|
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");
|
2019-02-09 20:32:28 +00:00
|
|
|
|
2019-02-09 01:26:45 +00:00
|
|
|
const cardHeader = document.createElement("div");
|
|
|
|
cardHeader.classList.add("card-header");
|
2019-02-09 20:32:28 +00:00
|
|
|
|
2019-02-09 01:26:45 +00:00
|
|
|
const cardBody = document.createElement("div");
|
|
|
|
cardBody.classList.add("card-body");
|
|
|
|
|
2019-02-09 20:32:28 +00:00
|
|
|
/*
|
|
|
|
const displayTable = document.createElement("table");
|
|
|
|
displayTable.classList.add("table table-striped table-responsive-sm");
|
|
|
|
displayTable.style.visibility = "visible";
|
|
|
|
cardBody.appendChild(displayTable);
|
|
|
|
*/
|
|
|
|
|
2019-02-09 01:26:45 +00:00
|
|
|
|
|
|
|
const sections = parsedData.sections;
|
|
|
|
for (let key in sections) {
|
|
|
|
let section = sections[key];
|
2019-02-09 20:32:28 +00:00
|
|
|
if(section.completed) {
|
|
|
|
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);
|
2019-02-09 01:26:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
modalBody.appendChild(card);
|
2019-02-05 23:31:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-02 23:02:09 +00:00
|
|
|
document.addEventListener("DOMContentLoaded", function(event) {
|
2019-02-04 09:39:42 +00:00
|
|
|
if (window.location.pathname === "/edit_report.html") {
|
|
|
|
const url = getEndpointDomain() + "api/v1/reports";
|
2019-02-11 02:15:32 +00:00
|
|
|
makeAjaxRequest("GET", url, displayListOfReports);
|
2019-02-04 09:39:42 +00:00
|
|
|
}
|
2019-02-02 23:02:09 +00:00
|
|
|
});
|
2019-01-28 05:39:48 +00:00
|
|
|
|
2019-02-02 23:02:09 +00:00
|
|
|
document.addEventListener("click", function(event) {
|
2019-02-04 09:39:42 +00:00
|
|
|
if (event.target) {
|
|
|
|
if (event.target.classList.contains("edit-report-button")) {
|
|
|
|
const url = getEndpointDomain() + "api/v1/report/" + event.target.dataset.rid;
|
|
|
|
const type = reportType.EDIT;
|
2019-02-11 02:15:32 +00:00
|
|
|
makeAjaxRequest("GET", url, createReportForm, type);
|
2019-02-10 05:16:53 +00:00
|
|
|
} else if (event.target.classList.contains("view-report-button")) {
|
|
|
|
console.log("View button clicked");
|
2019-02-10 23:46:27 +00:00
|
|
|
const url = getEndpointDomain() + "api/v1/report/" + event.target.dataset.rid;
|
2019-02-11 02:15:32 +00:00
|
|
|
makeAjaxRequest("GET", url, displayReport);
|
2019-02-10 23:57:05 +00:00
|
|
|
} else if (event.target.classList.contains("delete-report")) {
|
|
|
|
event.preventDefault();
|
2019-02-11 02:15:32 +00:00
|
|
|
const title = document.querySelector("#editReportModalLabel").textContent;
|
|
|
|
const result = confirm("Are you sure you want to delete the report \"" + title + "\"?");
|
2019-02-10 23:57:05 +00:00
|
|
|
if (result) {
|
|
|
|
const url = getEndpointDomain() + "api/v1/report/" + event.target.dataset.rid;
|
2019-02-11 02:15:32 +00:00
|
|
|
makeAjaxRequest("DELETE", url, function(parsedData) {
|
|
|
|
alert(parsedData.message);
|
|
|
|
location.reload(true);
|
|
|
|
});
|
2019-02-10 23:57:05 +00:00
|
|
|
}
|
2019-02-10 23:46:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-02 23:02:09 +00:00
|
|
|
});
|
2019-01-28 00:21:51 +00:00
|
|
|
|
2019-02-10 07:02:01 +00:00
|
|
|
const newReportForm = document.querySelector(".new-report-form");
|
|
|
|
if (newReportForm) {
|
|
|
|
newReportForm.addEventListener("submit", function(event) {
|
|
|
|
event.preventDefault();
|
2019-02-10 05:16:53 +00:00
|
|
|
const url = getEndpointDomain() + "api/v1/report";
|
|
|
|
const payload = JSON.stringify({ "title": event.target.elements.title.value });
|
|
|
|
console.log("Payload:\n" + payload);
|
|
|
|
const type = reportType.NEW;
|
2019-02-11 02:15:32 +00:00
|
|
|
makeAjaxRequest("POST", url, createReportForm, type, payload);
|
2019-02-10 07:02:01 +00:00
|
|
|
this.reset();
|
|
|
|
});
|
|
|
|
}
|
2019-02-13 08:48:35 +00:00
|
|
|
|
|
|
|
document.addEventListener("submit", function(event) {
|
|
|
|
if (event.target.classList.contains("section-form")) {
|
|
|
|
event.preventDefault();
|
|
|
|
console.log(event.target);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|