/* eslint-disable unicorn/prefer-module */ import type { cityssmGlobal } from "@cityssm/bulma-webapp-js/src/types"; import type { BulmaJS } from "@cityssm/bulma-js/types"; import type * as recordTypes from "../types/recordTypes"; declare const cityssm: cityssmGlobal; declare const bulmaJS: BulmaJS; (() => { const urlPrefix = document.querySelector("main").dataset.urlPrefix; const workOrderId = ( document.querySelector( "#workOrderEdit--workOrderId" ) as HTMLInputElement ).value; const isCreate = workOrderId === ""; document .querySelector("#form--workOrderEdit") .addEventListener("submit", (submitEvent) => { submitEvent.preventDefault(); cityssm.postJSON( urlPrefix + "/workOrders/" + (isCreate ? "doCreateWorkOrder" : "doUpdateWorkOrder"), submitEvent.currentTarget, (responseJSON: { success: boolean; workOrderId?: number; errorMessage?: string; }) => { if (responseJSON.success) { if (isCreate) { window.location.href = urlPrefix + "/workOrders/" + responseJSON.workOrderId + "/edit"; } else { bulmaJS.alert({ message: "Work Order Updated Successfully", contextualColorName: "success" }); } } else { bulmaJS.alert({ title: "Error Updating Work Order", message: responseJSON.errorMessage, contextualColorName: "danger" }); } } ); }); /* * Related Lots */ if (!isCreate) { let workOrderLots: recordTypes.Lot[] = exports.workOrderLots; delete exports.workOrderLots; let workOrderLotOccupancies: recordTypes.LotOccupancy[] = exports.workOrderLotOccupancies; delete exports.workOrderLotOccupancies; const deleteLotOccupancy = (clickEvent: Event) => { const lotOccupancyId = ( (clickEvent.currentTarget as HTMLElement).closest( ".container--lotOccupancy" ) as HTMLElement ).dataset.lotOccupancyId; const doDelete = () => { cityssm.postJSON( urlPrefix + "/workOrders/doDeleteWorkOrderLotOccupancy", { workOrderId, lotOccupancyId }, (responseJSON: { success: boolean; errorMessage?: string; workOrderLotOccupancies?: recordTypes.LotOccupancy[]; }) => { if (responseJSON.success) { workOrderLotOccupancies = responseJSON.workOrderLotOccupancies; renderRelatedLotsAndOccupancies(); } else { bulmaJS.alert({ title: "Error Deleting Relationship", message: responseJSON.errorMessage, contextualColorName: "danger" }); } } ); }; bulmaJS.confirm({ title: "Delete " + exports.aliases.lot + " " + exports.aliases.occupancy + " Relationship", message: "Are you sure you want to remove the relationship to this " + exports.aliases.lot.toLowerCase() + " " + exports.aliases.occupancy.toLowerCase() + " record from this work order? Note that the record will remain.", contextualColorName: "warning", okButton: { text: "Yes, Delete Relationship", callbackFunction: doDelete } }); }; const renderRelatedOccupancies = () => { const occupanciesContainerElement = document.querySelector( "#relatedTab--lotOccupancies" ) as HTMLElement; document.querySelector( ".tabs a[href='#relatedTab--lotOccupancies'] .tag" ).textContent = workOrderLotOccupancies.length.toString(); if (workOrderLotOccupancies.length === 0) { occupanciesContainerElement.innerHTML = '
' + '

There are no ' + exports.aliases.occupancies.toLowerCase() + " associated with this work order.

" + "
"; return; } occupanciesContainerElement.innerHTML = '' + "" + "" + '' + ("") + ("") + "" + "" + ("") + '' + "" + "" + "" + "
" + exports.aliases.occupancy + " Type" + exports.aliases.lot + "Start DateEnd Date" + exports.aliases.occupants + "
"; const currentDateString = cityssm.dateToString(new Date()); for (const lotOccupancy of workOrderLotOccupancies) { const rowElement = document.createElement("tr"); rowElement.className = "container--lotOccupancy"; rowElement.dataset.lotOccupancyId = lotOccupancy.lotOccupancyId.toString(); const isActive = !( lotOccupancy.occupancyEndDate && lotOccupancy.occupancyEndDateString < currentDateString ); rowElement.innerHTML = '' + (isActive ? '' : '') + "" + ("" + '' + cityssm.escapeHTML(lotOccupancy.occupancyType) + "" + "") + ("" + (lotOccupancy.lotId ? cityssm.escapeHTML(lotOccupancy.lotName) : '(No ' + exports.aliases.lot + ")") + "") + ("" + lotOccupancy.occupancyStartDateString + "") + ("" + (lotOccupancy.occupancyEndDate ? lotOccupancy.occupancyEndDateString : '(No End Date)') + "") + ("" + (lotOccupancy.lotOccupancyOccupants.length === 0 ? '(No ' + cityssm.escapeHTML(exports.aliases.occupants) + ")" : cityssm.escapeHTML( lotOccupancy.lotOccupancyOccupants[0] .occupantName ) + (lotOccupancy.lotOccupancyOccupants.length > 1 ? " plus " + (lotOccupancy.lotOccupancyOccupants.length - 1) : "")) + "") + ("" + '" + ""); rowElement .querySelector(".button--deleteLotOccupancy") .addEventListener("click", deleteLotOccupancy); occupanciesContainerElement .querySelector("tbody") .append(rowElement); } }; const deleteLot = (clickEvent: Event) => { const lotId = ( (clickEvent.currentTarget as HTMLElement).closest( ".container--lot" ) as HTMLElement ).dataset.lotId; const doDelete = () => { cityssm.postJSON( urlPrefix + "/workOrders/doDeleteWorkOrderLot", { workOrderId, lotId }, (responseJSON: { success: boolean; errorMessage?: string; workOrderLots?: recordTypes.Lot[]; }) => { if (responseJSON.success) { workOrderLots = responseJSON.workOrderLots; renderRelatedLotsAndOccupancies(); } else { bulmaJS.alert({ title: "Error Deleting Relationship", message: responseJSON.errorMessage, contextualColorName: "danger" }); } } ); }; bulmaJS.confirm({ title: "Delete " + exports.aliases.lot + " " + exports.aliases.occupancy + " Relationship", message: "Are you sure you want to remove the relationship to this " + exports.aliases.lot.toLowerCase() + " " + exports.aliases.occupancy.toLowerCase() + " record from this work order? Note that the record will remain.", contextualColorName: "warning", okButton: { text: "Yes, Delete Relationship", callbackFunction: doDelete } }); }; const renderRelatedLots = () => { const lotsContainerElement = document.querySelector( "#relatedTab--lots" ) as HTMLElement; document.querySelector( ".tabs a[href='#relatedTab--lots'] .tag" ).textContent = workOrderLots.length.toString(); if (workOrderLots.length === 0) { lotsContainerElement.innerHTML = '
' + '

There are no ' + exports.aliases.lots.toLowerCase() + " associated with this work order.

" + "
"; return; } lotsContainerElement.innerHTML = '' + "" + "" + ("") + ("") + ("") + "" + '' + "" + "" + "" + "
" + exports.aliases.lot + "" + exports.aliases.map + "" + exports.aliases.lot + " TypeStatus
"; for (const lot of workOrderLots) { const rowElement = document.createElement("tr"); rowElement.className = "container--lot"; rowElement.dataset.lotId = lot.lotId.toString(); rowElement.innerHTML = "" + '' + cityssm.escapeHTML(lot.lotName) + "" + "" + ("" + cityssm.escapeHTML(lot.mapName) + "") + ("" + cityssm.escapeHTML(lot.lotType) + "") + ("" + cityssm.escapeHTML(lot.lotStatus) + "") + ("" + '" + ""); rowElement .querySelector(".button--deleteLot") .addEventListener("click", deleteLot); lotsContainerElement.querySelector("tbody").append(rowElement); } }; const renderRelatedLotsAndOccupancies = () => { renderRelatedOccupancies(); renderRelatedLots(); }; renderRelatedLotsAndOccupancies(); } /* * Comments */ /* * Milestones */ })();