/* 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 lotOccupancyId: string let lotOccupancyComments: recordTypes.LotOccupancyComment[] = exports.lotOccupancyComments delete exports.lotOccupancyComments function openEditLotOccupancyComment(clickEvent: Event): void { const lotOccupancyCommentId = Number.parseInt( (clickEvent.currentTarget as HTMLElement).closest('tr')!.dataset .lotOccupancyCommentId!, 10 ) const lotOccupancyComment = lotOccupancyComments.find( (currentLotOccupancyComment) => { return ( currentLotOccupancyComment.lotOccupancyCommentId === lotOccupancyCommentId ) } )! let editFormElement: HTMLFormElement let editCloseModalFunction: () => void function editComment(submitEvent: SubmitEvent): void { submitEvent.preventDefault() cityssm.postJSON( los.urlPrefix + '/lotOccupancies/doUpdateLotOccupancyComment', editFormElement, (responseJSON: { success: boolean errorMessage?: string lotOccupancyComments?: recordTypes.LotOccupancyComment[] }) => { if (responseJSON.success) { lotOccupancyComments = responseJSON.lotOccupancyComments! editCloseModalFunction() renderLotOccupancyComments() } else { bulmaJS.alert({ title: 'Error Updating Comment', message: responseJSON.errorMessage ?? '', contextualColorName: 'danger' }) } } ) } cityssm.openHtmlModal('lotOccupancy-editComment', { onshow(modalElement) { los.populateAliases(modalElement) ;( modalElement.querySelector( '#lotOccupancyCommentEdit--lotOccupancyId' ) as HTMLInputElement ).value = lotOccupancyId ;( modalElement.querySelector( '#lotOccupancyCommentEdit--lotOccupancyCommentId' ) as HTMLInputElement ).value = lotOccupancyCommentId.toString() ;( modalElement.querySelector( '#lotOccupancyCommentEdit--lotOccupancyComment' ) as HTMLInputElement ).value = lotOccupancyComment.lotOccupancyComment! const lotOccupancyCommentDateStringElement = modalElement.querySelector( '#lotOccupancyCommentEdit--lotOccupancyCommentDateString' ) as HTMLInputElement lotOccupancyCommentDateStringElement.value = lotOccupancyComment.lotOccupancyCommentDateString! const currentDateString = cityssm.dateToString(new Date()) lotOccupancyCommentDateStringElement.max = lotOccupancyComment.lotOccupancyCommentDateString! <= currentDateString ? currentDateString : lotOccupancyComment.lotOccupancyCommentDateString! ;( modalElement.querySelector( '#lotOccupancyCommentEdit--lotOccupancyCommentTimeString' ) as HTMLInputElement ).value = lotOccupancyComment.lotOccupancyCommentTimeString! }, onshown(modalElement, closeModalFunction) { bulmaJS.toggleHtmlClipped() los.initializeDatePickers(modalElement) ;( modalElement.querySelector( '#lotOccupancyCommentEdit--lotOccupancyComment' ) as HTMLTextAreaElement ).focus() editFormElement = modalElement.querySelector('form')! editFormElement.addEventListener('submit', editComment) editCloseModalFunction = closeModalFunction }, onremoved() { bulmaJS.toggleHtmlClipped() } }) } function deleteLotOccupancyComment(clickEvent: Event): void { const lotOccupancyCommentId = Number.parseInt( (clickEvent.currentTarget as HTMLElement).closest('tr')!.dataset .lotOccupancyCommentId!, 10 ) function doDelete(): void { cityssm.postJSON( los.urlPrefix + '/lotOccupancies/doDeleteLotOccupancyComment', { lotOccupancyId, lotOccupancyCommentId }, (responseJSON: { success: boolean errorMessage?: string lotOccupancyComments: recordTypes.LotOccupancyComment[] }) => { if (responseJSON.success) { lotOccupancyComments = responseJSON.lotOccupancyComments renderLotOccupancyComments() } 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' }) } function renderLotOccupancyComments(): void { const containerElement = document.querySelector( '#container--lotOccupancyComments' ) as HTMLElement if (lotOccupancyComments.length === 0) { containerElement.innerHTML = '
' return } const tableElement = document.createElement('table') tableElement.className = 'table is-fullwidth is-striped is-hoverable' tableElement.innerHTML = '