reimbursinator/front/static/js/reports.js

698 lines
25 KiB
JavaScript
Raw Permalink Normal View History

2019-02-10 06:23:08 +00:00
const reportType = {
NEW : 1,
EDIT : 2
2019-02-10 06:23:08 +00:00
};
// Hack to change endpoint url
function getEndpointDomain() {
return "https://" + window.location.hostname + ":8444/";
}
2019-03-16 04:40:11 +00:00
/*
XMLHttpRequest wrapper for requesting or sending data to/from the backend
method (string): HTTP request type
url (string): the url of an api endpoint
callback (function): function to execute on success
optional: optional argument
payload (JSON): optional JSON payload
returns: n/a
*/
2019-02-17 03:58:55 +00:00
function makeAjaxRequest(method, url, callback, optional, 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);
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-17 03:58:55 +00:00
optional ? callback(parsedData, optional) : 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-03-16 04:40:11 +00:00
/*
Disables a button and changes its display text
button (object): the button to update
buttonText (string): the new display text
returns: n/a
*/
2019-03-07 07:26:55 +00:00
function animateButton(button, buttonText) {
2019-03-07 07:13:09 +00:00
button.disabled = true;
button.innerHTML = "";
let span = document.createElement("span");
span.classList.add("spinner-border", "spinner-border-sm");
button.appendChild(span);
button.appendChild(document.createTextNode(buttonText));
}
2019-03-16 04:40:11 +00:00
/*
Updates a section's header and footer based on its current state
parsedData (object): Object representing a section's data
saveButton (object): the button to update
returns: n/a
*/
2019-02-20 06:42:30 +00:00
function updateSection(parsedData, saveButton) {
2019-02-21 04:56:33 +00:00
const sectionIdStr = "#section-" + parsedData.id + "-";
const sectionState = document.querySelector(sectionIdStr + "state");
const collapseDiv = document.querySelector(sectionIdStr + "collapse");
// A completed section gets a header icon
2019-02-20 06:42:30 +00:00
if (parsedData.completed) {
2019-02-21 04:56:33 +00:00
const sectionAlert = collapseDiv.querySelector(".section-alert");
if (sectionAlert) {
2019-03-11 22:14:01 +00:00
collapseDiv.firstElementChild.removeChild(sectionAlert);
2019-02-21 04:56:33 +00:00
}
if (parsedData.rule_violations.length === 0) {
// Complete with no rule violations
sectionState.classList = "fas fa-check-square";
collapseDiv.className = "collapse";
} else {
// Complete but with rule violations
sectionState.classList = "fas fa-exclamation-triangle";
}
2019-02-20 06:42:30 +00:00
}
2019-03-16 04:40:11 +00:00
// Add card footer with rule violations if needed
2019-02-20 06:42:30 +00:00
const cardFooter = createCardFooter(parsedData.rule_violations);
if (collapseDiv.lastElementChild.classList.contains("card-footer")) {
collapseDiv.removeChild(collapseDiv.lastElementChild);
if (cardFooter) {
collapseDiv.appendChild(cardFooter);
}
} else {
if (cardFooter) {
collapseDiv.appendChild(cardFooter);
}
}
2019-02-21 04:56:33 +00:00
saveButton.innerHTML = "Save";
saveButton.disabled = false;
2019-02-20 06:42:30 +00:00
}
2019-03-16 04:40:11 +00:00
/*
Wraps a Bootstrap form group around a field
sectionIdStr (string): section id prefix
field (object): Object representing a field
returns: div element with form group styling
*/
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-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", inputId);
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");
2019-02-10 05:16:53 +00:00
input.name = field.field_name;
input.id = inputId;
switch(field.field_type) {
case "boolean":
2019-02-14 22:17:25 +00:00
const select = document.createElement("select");
select.name = field.field_name;
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);
break;
case "date":
2019-02-16 04:15:39 +00:00
input.type = "date";
input.placeholder = "mm-dd-yyyy";
if (field.value === "None") {
input.value = "";
} else {
input.value = field.value;
}
input.classList.add("form-control");
formGroup.appendChild(label);
div.appendChild(input)
formGroup.appendChild(div);
break;
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;
case "decimal":
input.type = "number";
2019-02-16 04:15:39 +00:00
if (field.value === "0.00") {
input.value = "";
} else {
input.value = field.value;
}
2019-02-01 01:48:36 +00:00
input.classList.add("form-control");
input.step = 0.01;
input.min = 0.00;
2019-02-10 05:16:53 +00:00
formGroup.appendChild(label);
div.appendChild(input)
formGroup.appendChild(div);
break;
case "integer":
input.type = "number";
2019-02-16 04:15:39 +00:00
if (field.value === 0) {
input.value = "";
} else {
input.value = field.value;
}
2019-02-10 05:16:53 +00:00
input.classList.add("form-control");
input.step = 1;
input.min = 0;
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;
}
2019-03-16 04:40:11 +00:00
/*
Creates a card and card header
sectionIdStr (string): section id prefix
sectionTitle (string): section title for the collapse button
sectionCompleted (boolean): flag indicating section state
ruleViolations (array): list of policy rule violations for this section
returns: div element with card styling
*/
2019-02-21 04:56:33 +00:00
function createCollapsibleCard(sectionIdStr, sectionTitle, sectionCompleted, ruleViolations) {
// Create card and header
const card = document.createElement("div");
card.classList.add("card");
const cardHeader = document.createElement("div");
cardHeader.classList.add("card-header");
const sectionState = document.createElement("i");
sectionState.id = sectionIdStr + "state";
2019-02-21 04:56:33 +00:00
// A completed section gets a header icon
if (sectionCompleted) {
2019-02-21 04:56:33 +00:00
if (ruleViolations.length === 0) {
sectionState.classList.add("fas", "fa-check-square");
} else {
sectionState.classList.add("fas", "fa-exclamation-triangle");
}
}
// Create h2, button. Append button to h2, h2 to header, and header to card
const h2 = document.createElement("h2");
h2.classList.add("mb-0");
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");
button.innerHTML = sectionTitle;
h2.appendChild(button);
h2.appendChild(sectionState);
cardHeader.appendChild(h2);
card.appendChild(cardHeader);
return card;
}
2019-03-16 04:40:11 +00:00
/*
Creates a collapsible card body
form (HTML element): form element
sectionIdStr (string): section id prefix
sectionDescription (string): HTML string description of this section
sectionCompleted (boolean): flag indicating section state
ruleViolations (array): list of policy rule violations for this section
returns: div element with collapse styling
*/
function createCollapsibleCardBody(form, sectionIdStr, sectionDescription, sectionCompleted, ruleViolations) {
2019-02-01 00:27:29 +00:00
// Create wrapper div
2019-02-16 20:24:19 +00:00
const collapseDiv = document.createElement("div");
collapseDiv.id = sectionIdStr + "collapse";
2019-02-04 09:39:42 +00:00
const cardBody = document.createElement("div");
cardBody.classList.add("card-body");
2019-02-21 04:56:33 +00:00
const sectionAlert = document.createElement("div");
2019-02-24 08:24:23 +00:00
if (sectionCompleted && ruleViolations.length === 0) {
collapseDiv.classList.add("collapse");
} else if (sectionCompleted && ruleViolations.length > 0) {
collapseDiv.classList.add("collapse", "show");
} else {
2019-03-16 04:40:11 +00:00
// Add section alert
2019-02-21 04:56:33 +00:00
sectionAlert.classList.add("alert", "alert-danger", "section-alert");
sectionAlert.innerHTML = "This section is not complete";
2019-02-24 08:24:23 +00:00
collapseDiv.classList.add("collapse", "show");
2019-02-04 09:39:42 +00:00
}
2019-02-01 00:27:29 +00:00
// Create card body. Append form to body, body to wrapper div
2019-02-21 04:56:33 +00:00
cardBody.appendChild(sectionAlert);
cardBody.insertAdjacentHTML("beforeend", sectionDescription);
cardBody.appendChild(form);
2019-02-16 20:24:19 +00:00
collapseDiv.appendChild(cardBody);
return collapseDiv;
}
2019-03-16 04:40:11 +00:00
/*
Creates a card footer and populates it with rule violations
ruleViolations (array): a list of policy rule violations
returns: div element with card footer styling
*/
2019-02-16 20:24:19 +00:00
function createCardFooter(ruleViolations) {
if (ruleViolations.length === 0) {
return null;
2019-02-16 20:24:19 +00:00
}
const cardFooter = document.createElement("div");
cardFooter.classList.add("card-footer");
2019-02-16 20:24:19 +00:00
const violationMessage = document.createElement("div");
violationMessage.classList.add("alert", "alert-danger");
const heading = document.createElement("div");
heading.innerHTML = "Rule Violations";
heading.classList.add("alert-heading");
violationMessage.appendChild(heading);
violationMessage.appendChild(document.createElement("hr"));
2019-02-16 20:24:19 +00:00
for (let i = 0; i < ruleViolations.length; i++) {
let violation = document.createElement("p");
let violationLabel = document.createElement("strong");
violationLabel.innerHTML = ruleViolations[i].label;
violation.appendChild(violationLabel);
violation.appendChild(document.createElement("br"));
let ruleBreakText = document.createTextNode(ruleViolations[i].rule_break_text);
violation.appendChild(ruleBreakText);
violationMessage.appendChild(violation);
}
2019-02-16 20:24:19 +00:00
cardFooter.appendChild(violationMessage);
return cardFooter;
}
2019-03-16 04:40:11 +00:00
/*
Creates a form within a modal popup
parsedData (object): Object representing report data
type (number): The report type (edit or new)
returns: n/a
*/
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";
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
const reviewButton = document.querySelector(".review-report");
if (reviewButton) {
reviewButton.setAttribute("data-rid", parsedData.report_pk);
}
const finalizeButton = document.querySelector(".finalize-report");
if (finalizeButton) {
finalizeButton.setAttribute("data-rid", parsedData.report_pk);
}
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-16 20:24:19 +00:00
modalLabel.innerHTML = parsedData.title;
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 i = 0; i < sections.length; i++) {
// Create a new form
2019-02-16 20:24:19 +00:00
let sectionIdStr = "section-" + sections[i].id + "-";
let form = document.createElement("form");
form.classList.add("form", "section-form");
2019-02-14 23:58:27 +00:00
form.id = sectionIdStr + "form";
form.setAttribute("data-rid", parsedData.report_pk);
form.setAttribute("data-sid", sections[i].id);
// Traverse the fields of this section
let fields = sections[i].fields;
for (let j = 0; j < fields.length; j++) {
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-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", "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
let cardBody = createCollapsibleCardBody(form, sectionIdStr,
2019-02-21 04:56:33 +00:00
sections[i].html_description, sections[i].completed, sections[i].rule_violations);
let cardFooter = createCardFooter(sections[i].rule_violations);
if (cardFooter) {
cardBody.appendChild(cardFooter);
2019-02-16 20:24:19 +00:00
}
2019-02-21 04:56:33 +00:00
let collapsibleCard = createCollapsibleCard(sectionIdStr, sections[i].title,
sections[i].completed, sections[i].rule_violations)
collapsibleCard.appendChild(cardBody);
2019-02-01 08:07:08 +00:00
accordion.appendChild(collapsibleCard);
}
modalBody.appendChild(accordion);
2019-01-30 23:42:09 +00:00
}
2019-03-16 04:40:11 +00:00
/*
Displays a table containing all of a user's reports
parsedData (object): Object representing report data
returns: n/a
*/
2019-01-30 23:42:09 +00:00
function displayListOfReports(parsedData) {
const reports = parsedData.reports;
2019-02-17 00:41:11 +00:00
const cardBody = document.querySelector(".card-body");
2019-02-17 00:07:43 +00:00
const table = document.querySelector("table");
2019-02-17 00:41:11 +00:00
cardBody.removeChild(cardBody.firstElementChild); // remove loading spinner
2019-02-02 22:22:18 +00:00
if (reports.length === 0) {
2019-02-17 00:41:11 +00:00
cardBody.removeChild(table);
const h5 = document.createElement("h5");
h5.innerHTML = "No reports found.";
h5.classList.add("text-center");
cardBody.appendChild(h5);
} else {
2019-02-02 22:22:18 +00:00
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);
2019-02-02 22:22:18 +00:00
bodyRow.insertCell(0).innerHTML = title;
bodyRow.insertCell(1).innerHTML = dateCreated;
2019-02-02 22:22:18 +00:00
// 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
}
2019-02-21 04:56:33 +00:00
let dateSubmittedCell = bodyRow.insertCell(2);
2019-02-02 22:22:18 +00:00
dateSubmittedCell.innerHTML = dateSubmitted;
dateSubmittedCell.classList.add("d-none", "d-md-table-cell"); // Column visible on medium and larger displays
2019-02-21 04:56:33 +00:00
bodyRow.insertCell(3).appendChild(actionButton);
2019-02-02 22:22:18 +00:00
}
table.style.visibility = "visible";
}
}
2019-03-16 04:40:11 +00:00
/*
Populates modal popup with a readonly, finalized report
parsedData (object): Object representing report data
returns: n/a
*/
function displayReport(parsedData){
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];
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");
2019-02-23 22:40:30 +00:00
let p1Value = "";
if(field.field_type == "boolean")
{
if(field.value == true)
2019-02-23 22:40:30 +00:00
{
p1Value = document.createTextNode(field.label + ": " + "Yes");
p1.appendChild(p1Value);
cardBody.appendChild(p1);
}
else
{
p1Value = document.createTextNode(field.label + ": " + "No");
p1.appendChild(p1Value);
cardBody.appendChild(p1);
}
}
else if(field.value == "")
{
p1Value = document.createTextNode(field.label + ": " + "None");
p1.appendChild(p1Value);
cardBody.appendChild(p1);
}
else
{
p1Value = document.createTextNode(field.label + ": " + field.value);
p1.appendChild(p1Value);
cardBody.appendChild(p1);
}
}
cardHeader.appendChild(cardBody);
card.appendChild(cardHeader);
}
}
modalBody.appendChild(card);
}
2019-03-16 04:40:11 +00:00
// Display list of reports on page load
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-03-16 04:40:11 +00:00
// Listens for button click events
document.addEventListener("click", function(event) {
2019-02-04 09:39:42 +00:00
if (event.target) {
if (event.target.classList.contains("edit-report-button")) {
2019-03-16 04:40:11 +00:00
// Edit button clicked
2019-02-04 09:39:42 +00:00
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")) {
2019-03-16 04:40:11 +00:00
// View button clicked
2019-02-10 05:16:53 +00:00
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);
} else if (event.target.classList.contains("review-report")) {
2019-03-16 04:40:11 +00:00
// Submit for review button clicked
2019-02-19 04:14:48 +00:00
event.preventDefault();
const result = confirm("Are you sure you want to submit this report for review?");
2019-02-19 04:14:48 +00:00
if (result) {
2019-03-07 07:13:09 +00:00
animateButton(event.target, " Submitting...");
2019-02-19 04:14:48 +00:00
const url = getEndpointDomain() + "api/v1/report/" + event.target.dataset.rid;
makeAjaxRequest("PUT", url, function(parsedData) {
2019-03-07 07:13:09 +00:00
event.target.disabled = false;
event.target.innerHTML = "Submit for Review";
alert(parsedData.message);
2019-02-19 04:14:48 +00:00
location.reload(true);
});
}
} else if (event.target.classList.contains("finalize-report")) {
2019-03-16 04:40:11 +00:00
// Finalize report button clicked
event.preventDefault();
console.log("finalize-report");
const result = confirm("Are you sure you want to finalize this report? This means you will no longer be able to modify it.");
if (result) {
2019-03-07 07:13:09 +00:00
animateButton(event.target, " Submitting...");
const url = getEndpointDomain() + "api/v1/report/" + event.target.dataset.rid + "/final";
makeAjaxRequest("PUT", url, function(parsedData) {
2019-03-07 07:13:09 +00:00
event.target.disabled = false;
event.target.innerHTML = "Finalize Report";
alert(parsedData.message);
location.reload(true);
});
}
2019-02-10 23:57:05 +00:00
} else if (event.target.classList.contains("delete-report")) {
2019-03-16 04:40:11 +00:00
// Delete report button clicked
2019-02-10 23:57:05 +00:00
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) {
2019-03-07 07:13:09 +00:00
animateButton(event.target, " Deleting...");
2019-02-10 23:57:05 +00:00
const url = getEndpointDomain() + "api/v1/report/" + event.target.dataset.rid;
2019-02-11 02:15:32 +00:00
makeAjaxRequest("DELETE", url, function(parsedData) {
2019-03-07 07:13:09 +00:00
event.target.disabled = false;
event.target.innerHTML = "Delete Report";
alert(parsedData.message);
2019-02-11 02:15:32 +00:00
location.reload(true);
});
2019-02-10 23:57:05 +00:00
}
2019-02-10 23:46:27 +00:00
}
}
});
2019-03-16 04:40:11 +00:00
// Listens for a create report submission
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, "reference": event.target.elements.reference.value });
2019-02-10 05:16:53 +00:00
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
});
}
2019-03-16 04:40:11 +00:00
// Listens for a date input
2019-02-16 04:15:39 +00:00
document.addEventListener("input", function(event) {
if (event.target.type === "date") {
if (!moment(event.target.value, "YYYY-MM-DD", true).isValid()) {
event.target.setCustomValidity("Invalid date format");
} else {
event.target.setCustomValidity("");
}
}
});
2019-03-16 04:40:11 +00:00
// Listens for a section saving event
document.addEventListener("submit", function(event) {
if (event.target.classList.contains("section-form")) {
event.preventDefault();
2019-02-17 03:58:55 +00:00
let saveButton = event.target.lastElementChild;
2019-03-07 07:13:09 +00:00
animateButton(saveButton, " Saving...");
const formData = new FormData(event.target);
const url = getEndpointDomain() + "api/v1/section/" + event.target.dataset.sid;
2019-02-20 06:42:30 +00:00
makeAjaxRequest("PUT", url, updateSection, saveButton, formData);
}
});
// Jquery is required to handle this modal event
$(document).ready(function(){
$("#newReportModal").on('hidden.bs.modal', function() {
location.reload(true);
});
});