Skip to content

Commit

Permalink
allow creation of new Incident from Field Report page
Browse files Browse the repository at this point in the history
  • Loading branch information
srabraham committed Nov 25, 2024
1 parent a031da5 commit 45823a9
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/ims/auth/_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,10 @@ async def authorizeRequestForIncidentReport(
rangerHandle = user.shortNames[0]
for reportEntry in incidentReport.reportEntries:
if reportEntry.author == rangerHandle:
request.authorizations = ( # type: ignore[attr-defined]
Authorization.writeIncidentReports
await self.authorizeRequest(
request,
incidentReport.eventID,
Authorization.writeIncidentReports,
)
return

Expand Down
11 changes: 11 additions & 0 deletions src/ims/element/incident/report/_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,14 @@ def incident_report_number(
JSON integer: incident report number.
"""
return jsonTextFromObject(self.number)

@renderer
def can_write_incidents(
self, request: IRequest, tag: Tag
) -> KleinRenderable:
if (
request.authorizations # type: ignore[attr-defined]
& Authorization.writeIncidents
):
return jsonTrue
return jsonFalse
1 change: 1 addition & 0 deletions src/ims/element/incident/report/template.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
var editingAllowed = <json t:render="editing_allowed" />;
var eventID = <json t:render="event_id" />;
var incidentReportNumber = <json t:render="incident_report_number" />;
var canWriteIncidents = <json t:render="can_write_incidents" />;
var pageTemplateURL = url_viewIncidentReportTemplate;

initIncidentReportPage();
Expand Down
25 changes: 23 additions & 2 deletions src/ims/element/incident/report_template/template.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,35 @@
<label class="control-label">FR #:</label>
<span id="incident_report_number" class="form-control-static" />
</div>
<div class="form-group">
<label class="control-label">IMS #:</label>
<span id="incident_number" class="form-control-static" />
<button
id="create_incident"
class="btn btn-sm btn-warning hidden"
title="Only click this if there's no preexisting incident for this FR"
onclick="makeIncident()"
>
Create new incident from FR
</button>
</div>
</div>
</div>

<!-- Instructions -->

<div class="row">
<div class="col-sm-12 no-print">
<label>Instructions</label>

<button
id="instructions_toggle"
role="button"
class="btn btn-sm btn-default"
data-target="#instructions_content"
data-toggle="collapse"
>
Guidance for writing a great Field Report <span class="caret" />
</button>
<div id="instructions_content" class="collapse">
<p>
Enter a thorough account of what happened below.
</p>
Expand Down Expand Up @@ -60,6 +80,7 @@
Click on "Add Entry" to save your report entry.
Once you have saved, you cannot edit the saved entry, but you can still add additional entries.
</p>
</div>
</div>
</div>

Expand Down
68 changes: 68 additions & 0 deletions src/ims/element/static/incident_report.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function loadAndDisplayIncidentReport(success) {

drawTitle();
drawNumber();
drawIncident();
drawSummary();
drawReportEntries(incidentReport.report_entries);
clearErrorMessage();
Expand Down Expand Up @@ -178,6 +179,33 @@ function drawNumber() {
$("#incident_report_number").text(number);
}

//
// Populate incident number or show "create incident" button
//

function drawIncident() {
// New Incident Report. There can be no Incident
if (incidentReport.number === null) {
return;
}
// If there's an attached Incident, then show a link to it
if (incidentReport.incident !== null) {
const incidentURL = urlReplace(url_viewIncidentNumber).replace("<number>", incidentReport.incident);
const $a = $("<a>", {href: incidentURL});
$a.text(incidentReport.incident);
$("#incident_number").append($a);
} else {
$("#incident_number").text("(none)");
}
// If there's no attached Incident, show a button for making
// a new Incident
if (incidentReport.incident === null && canWriteIncidents) {
$("#create_incident").removeClass("hidden");
} else {
$("#create_incident").addClass("hidden");
}
}


//
// Populate field report summary
Expand Down Expand Up @@ -278,3 +306,43 @@ function sendEdits(edits, success, error) {
function editSummary() {
editFromElement($("#incident_report_summary"), "summary");
}

//
// Make a new incident and attach this incident report to it
//

function makeIncident() {
const incidentsURL = urlReplace(url_incidents);

function ok(data, status, xhr) {
const newIncident = xhr.getResponseHeader("X-IMS-Incident-Number");
incidentReport.incident = parseInt(newIncident);

const url = (
urlReplace(url_incidentReports) + incidentReport.number +
"?action=attach;incident=" + newIncident
);

function attachSuccess(data, status, xhr) {
console.log("Created and attached to new incident " + newIncident);
loadAndDisplayIncidentReport();
}

function attachFail(data, status, xhr) {
console.log("Failed to attach incident report");
}

jsonRequest(url, {}, attachSuccess, attachFail);
}

const authors = [];
if (incidentReport.report_entries?.length > 0) {
authors.push(incidentReport.report_entries[0].author);
}

jsonRequest(incidentsURL, {
"summary": incidentReport.summary,
"ranger_handles": authors,
}, ok, null,
);
}
4 changes: 4 additions & 0 deletions src/ims/element/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ h1 {
resize: vertical;
}

#instructions_toggle {
margin-bottom: 5px;
}

.item-hidden > .badge-visible {
display: none;
}
Expand Down

0 comments on commit 45823a9

Please sign in to comment.