/* 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; (() => { const los = exports.los as globalTypes.LOS; const lotId = (document.querySelector("#lot--lotId") as HTMLInputElement).value; const isCreate = lotId === ""; // Main form const formElement = document.querySelector("#form--lot") as HTMLFormElement; const updateLot = (formEvent: SubmitEvent) => { formEvent.preventDefault(); cityssm.postJSON( los.urlPrefix + "/lots/" + (isCreate ? "doCreateLot" : "doUpdateLot"), formElement, (responseJSON: { success: boolean; lotId?: number; errorMessage?: string }) => { if (responseJSON.success) { if (isCreate) { window.location.href = los.urlPrefix + "/lots/" + responseJSON.lotId + "/edit"; } else { bulmaJS.alert({ message: exports.aliases.lot + " Updated Successfully", contextualColorName: "success" }); } } else { bulmaJS.alert({ title: "Error Updating " + exports.aliases.lot, message: responseJSON.errorMessage || "", contextualColorName: "danger" }); } } ); }; formElement.addEventListener("submit", updateLot); los.initializeUnlockFieldButtons(formElement); if (!isCreate) { (document.querySelector("#button--deleteLot") as HTMLButtonElement).addEventListener( "click", (clickEvent) => { clickEvent.preventDefault(); const doDelete = () => { cityssm.postJSON( los.urlPrefix + "/lots/doDeleteLot", { lotId }, (responseJSON: { success: boolean; errorMessage?: string }) => { if (responseJSON.success) { cityssm.disableNavBlocker(); window.location.href = los.urlPrefix + "/lots/?t=" + Date.now(); } else { bulmaJS.alert({ title: "Error Deleting " + exports.aliases.lot, message: responseJSON.errorMessage || "", contextualColorName: "danger" }); } } ); }; bulmaJS.confirm({ title: "Delete " + exports.aliases.lot, message: "Are you sure you want to delete this " + exports.aliases.lot.toLowerCase() + "?", contextualColorName: "warning", okButton: { text: "Yes, Delete " + exports.aliases.lot, callbackFunction: doDelete } }); } ); } // Comments let lotComments: recordTypes.LotComment[] = exports.lotComments; delete exports.lotComments; const openEditLotComment = (clickEvent: Event) => { const lotCommentId = Number.parseInt( (clickEvent.currentTarget as HTMLElement).closest("tr")!.dataset.lotCommentId!, 10 ); const lotComment = lotComments.find((currentLotComment) => { return currentLotComment.lotCommentId === lotCommentId; })!; let editFormElement: HTMLFormElement; let editCloseModalFunction: () => void; const editComment = (submitEvent: SubmitEvent) => { submitEvent.preventDefault(); cityssm.postJSON( los.urlPrefix + "/lots/doUpdateLotComment", editFormElement, (responseJSON: { success: boolean; errorMessage?: string; lotComments?: recordTypes.LotComment[]; }) => { if (responseJSON.success) { lotComments = responseJSON.lotComments!; editCloseModalFunction(); renderLotComments(); } else { bulmaJS.alert({ title: "Error Updating Comment", message: responseJSON.errorMessage || "", contextualColorName: "danger" }); } } ); }; cityssm.openHtmlModal("lot-editComment", { onshow: (modalElement) => { los.populateAliases(modalElement); (modalElement.querySelector("#lotCommentEdit--lotId") as HTMLInputElement).value = lotId; ( modalElement.querySelector("#lotCommentEdit--lotCommentId") as HTMLInputElement ).value = lotCommentId.toString(); ( modalElement.querySelector("#lotCommentEdit--lotComment") as HTMLInputElement ).value = lotComment.lotComment!; const lotCommentDateStringElement = modalElement.querySelector( "#lotCommentEdit--lotCommentDateString" ) as HTMLInputElement; lotCommentDateStringElement.value = lotComment.lotCommentDateString!; const currentDateString = cityssm.dateToString(new Date()); lotCommentDateStringElement.max = lotComment.lotCommentDateString! <= currentDateString ? currentDateString : lotComment.lotCommentDateString!; ( modalElement.querySelector( "#lotCommentEdit--lotCommentTimeString" ) as HTMLInputElement ).value = lotComment.lotCommentTimeString!; }, onshown: (modalElement, closeModalFunction) => { bulmaJS.toggleHtmlClipped(); los.initializeDatePickers(modalElement); los.initializeTimePickers(modalElement); ( modalElement.querySelector("#lotCommentEdit--lotComment") as HTMLTextAreaElement ).focus(); editFormElement = modalElement.querySelector("form")!; editFormElement.addEventListener("submit", editComment); editCloseModalFunction = closeModalFunction; }, onremoved: () => { bulmaJS.toggleHtmlClipped(); } }); }; const deleteLotComment = (clickEvent: Event) => { const lotCommentId = Number.parseInt( (clickEvent.currentTarget as HTMLElement).closest("tr")!.dataset.lotCommentId!, 10 ); const doDelete = () => { cityssm.postJSON( los.urlPrefix + "/lots/doDeleteLotComment", { lotId, lotCommentId }, (responseJSON: { success: boolean; errorMessage?: string; lotComments: recordTypes.LotComment[]; }) => { if (responseJSON.success) { lotComments = responseJSON.lotComments; renderLotComments(); } else { bulmaJS.alert({ title: "Error Removing Comment", message: responseJSON.errorMessage || "", contextualColorName: "danger" }); } } ); }; bulmaJS.confirm({ title: "Remove Comment?", message: "Are you sure you want to remove this comment?", okButton: { text: "Yes, Remove Comment", callbackFunction: doDelete }, contextualColorName: "warning" }); }; const renderLotComments = () => { const containerElement = document.querySelector("#container--lotComments") as HTMLElement; if (lotComments.length === 0) { containerElement.innerHTML = '
"; return; } const tableElement = document.createElement("table"); tableElement.className = "table is-fullwidth is-striped is-hoverable"; tableElement.innerHTML = "