/* eslint-disable @typescript-eslint/no-non-null-assertion, unicorn/prefer-module */ import type * as globalTypes from "../../types/globalTypes"; import type * as recordTypes from "../../types/recordTypes"; import type { cityssmGlobal } from "@cityssm/bulma-webapp-js/src/types"; import type { BulmaJS } from "@cityssm/bulma-js/types"; declare const cityssm: cityssmGlobal; declare const bulmaJS: BulmaJS; declare const los: globalTypes.LOS; declare const refreshFontAwesomeIcon: (changeEvent: Event) => void; let lotOccupantTypes: recordTypes.LotOccupantType[] = exports.lotOccupantTypes; delete exports.lotOccupantTypes; const updateLotOccupantType = (submitEvent: SubmitEvent) => { submitEvent.preventDefault(); cityssm.postJSON( los.urlPrefix + "/admin/doUpdateLotOccupantType", submitEvent.currentTarget, (responseJSON: { success: boolean; errorMessage?: string; lotOccupantTypes?: recordTypes.LotOccupantType[]; }) => { if (responseJSON.success) { lotOccupantTypes = responseJSON.lotOccupantTypes!; bulmaJS.alert({ message: exports.aliases.lot + " " + exports.aliases.occupant + " Type Updated Successfully", contextualColorName: "success" }); } else { bulmaJS.alert({ title: "Error Updating " + exports.aliases.lot + " " + exports.aliases.occupant + " Type", message: responseJSON.errorMessage || "", contextualColorName: "danger" }); } } ); }; const deleteLotOccupantType = (clickEvent: Event) => { const tableRowElement = (clickEvent.currentTarget as HTMLElement).closest("tr")!; const lotOccupantTypeId = tableRowElement.dataset.lotOccupantTypeId; const doDelete = () => { cityssm.postJSON( los.urlPrefix + "/admin/doDeleteLotOccupantType", { lotOccupantTypeId }, (responseJSON: { success: boolean; errorMessage?: string; lotOccupantTypes?: recordTypes.LotOccupantType[]; }) => { if (responseJSON.success) { lotOccupantTypes = responseJSON.lotOccupantTypes!; if (lotOccupantTypes.length === 0) { renderLotOccupantTypes(); } else { tableRowElement.remove(); } bulmaJS.alert({ message: exports.aliases.lot + " " + exports.aliases.occupant + " Type Deleted Successfully", contextualColorName: "success" }); } else { bulmaJS.alert({ title: "Error Deleting " + exports.aliases.lot + " " + exports.aliases.occupant + " Type", message: responseJSON.errorMessage || "", contextualColorName: "danger" }); } } ); }; bulmaJS.confirm({ title: "Delete " + exports.aliases.lot + " " + exports.aliases.occupant + " Type", message: "Are you sure you want to delete this " + exports.aliases.lot.toLowerCase() + " " + exports.aliases.occupant.toLowerCase() + " type?
" + "Note that no " + exports.aliases.lot.toLowerCase() + " " + exports.aliases.occupancy.toLowerCase() + " will be removed.", messageIsHtml: true, contextualColorName: "warning", okButton: { text: "Yes, Delete " + exports.aliases.lot + " " + exports.aliases.occupant + " Type", callbackFunction: doDelete } }); }; const moveLotOccupantTypeUp = (clickEvent: MouseEvent) => { const tableRowElement = (clickEvent.currentTarget as HTMLElement).closest("tr")!; const lotOccupantTypeId = tableRowElement.dataset.lotOccupantTypeId; cityssm.postJSON( los.urlPrefix + "/admin/doMoveLotOccupantTypeUp", { lotOccupantTypeId, moveToTop: clickEvent.shiftKey ? "1" : "0" }, (responseJSON: { success: boolean; errorMessage?: string; lotOccupantTypes?: recordTypes.LotOccupantType[]; }) => { if (responseJSON.success) { lotOccupantTypes = responseJSON.lotOccupantTypes!; renderLotOccupantTypes(); } else { bulmaJS.alert({ title: "Error Moving " + exports.aliases.lot + " " + exports.aliases.occupant + " Type", message: responseJSON.errorMessage || "", contextualColorName: "danger" }); } } ); }; const moveLotOccupantTypeDown = (clickEvent: MouseEvent) => { const tableRowElement = (clickEvent.currentTarget as HTMLElement).closest("tr")!; const lotOccupantTypeId = tableRowElement.dataset.lotOccupantTypeId; cityssm.postJSON( los.urlPrefix + "/admin/doMoveLotOccupantTypeDown", { lotOccupantTypeId, moveToBottom: clickEvent.shiftKey ? "1" : "0" }, (responseJSON: { success: boolean; errorMessage?: string; lotOccupantTypes?: recordTypes.LotOccupantType[]; }) => { if (responseJSON.success) { lotOccupantTypes = responseJSON.lotOccupantTypes!; renderLotOccupantTypes(); } else { bulmaJS.alert({ title: "Error Moving " + exports.aliases.lot + " " + exports.aliases.occupant + " Type", message: responseJSON.errorMessage || "", contextualColorName: "danger" }); } } ); }; const renderLotOccupantTypes = () => { const containerElement = document.querySelector( "#container--lotOccupantTypes" ) as HTMLTableSectionElement; if (lotOccupantTypes.length === 0) { containerElement.innerHTML = "" + '' + '
' + '

There are no active ' + cityssm.escapeHTML(exports.aliases.lot.toLowerCase()) + " " + cityssm.escapeHTML(exports.aliases.occupant.toLowerCase()) + " types.

" + "
" + "" + ""; return; } containerElement.innerHTML = ""; for (const lotOccupantType of lotOccupantTypes) { const tableRowElement = document.createElement("tr"); tableRowElement.dataset.lotOccupantTypeId = lotOccupantType.lotOccupantTypeId.toString(); const formId = "form--lotOccupantType-" + lotOccupantType.lotOccupantTypeId; tableRowElement.innerHTML = "" + ('
' + '
' + '' + "
" + "
") + "" + "" + ('
' + '
fa-
' + '
' + '' + "
" + '
' + '
' + "
") + "" + ("" + ('
') + '" + '' + "
" + "") + '' + '
' + '
' + los.getMoveUpDownButtonFieldHTML( "button--moveLotOccupantTypeUp", "button--moveLotOccupantTypeDown", false ) + "
" + '
' + '" + "
" + "
" + ""; const fontAwesomeInputElement = tableRowElement.querySelector( "input[name='fontAwesomeIconClass']" )!; fontAwesomeInputElement.addEventListener("keyup", refreshFontAwesomeIcon); fontAwesomeInputElement.addEventListener("change", refreshFontAwesomeIcon); tableRowElement.querySelector("form")!.addEventListener("submit", updateLotOccupantType); ( tableRowElement.querySelector(".button--moveLotOccupantTypeUp") as HTMLButtonElement ).addEventListener("click", moveLotOccupantTypeUp); ( tableRowElement.querySelector(".button--moveLotOccupantTypeDown") as HTMLButtonElement ).addEventListener("click", moveLotOccupantTypeDown); ( tableRowElement.querySelector(".button--deleteLotOccupantType") as HTMLButtonElement ).addEventListener("click", deleteLotOccupantType); containerElement.append(tableRowElement); } }; (document.querySelector("#form--addLotOccupantType") as HTMLFormElement).addEventListener( "submit", (submitEvent: SubmitEvent) => { submitEvent.preventDefault(); const formElement = submitEvent.currentTarget as HTMLFormElement; cityssm.postJSON( los.urlPrefix + "/admin/doAddLotOccupantType", formElement, (responseJSON: { success: boolean; errorMessage?: string; lotOccupantTypes?: recordTypes.LotOccupantType[]; }) => { if (responseJSON.success) { lotOccupantTypes = responseJSON.lotOccupantTypes!; renderLotOccupantTypes(); formElement.reset(); formElement.querySelector("input")!.focus(); } else { bulmaJS.alert({ title: "Error Adding " + exports.aliases.lot + " " + exports.aliases.occupant + " Type", message: responseJSON.errorMessage || "", contextualColorName: "danger" }); } } ); } ); renderLotOccupantTypes();