diff --git a/handlers/lotOccupancies-post/doAddLotOccupancyOccupant.d.ts b/handlers/lotOccupancies-post/doAddLotOccupancyOccupant.d.ts new file mode 100644 index 00000000..9621c611 --- /dev/null +++ b/handlers/lotOccupancies-post/doAddLotOccupancyOccupant.d.ts @@ -0,0 +1,3 @@ +import type { RequestHandler } from "express"; +export declare const handler: RequestHandler; +export default handler; diff --git a/handlers/lotOccupancies-post/doAddLotOccupancyOccupant.js b/handlers/lotOccupancies-post/doAddLotOccupancyOccupant.js new file mode 100644 index 00000000..b298c4ac --- /dev/null +++ b/handlers/lotOccupancies-post/doAddLotOccupancyOccupant.js @@ -0,0 +1,11 @@ +import { addLotOccupancyOccupant } from "../../helpers/lotOccupancyDB/addLotOccupancyOccupant.js"; +import { getLotOccupancyOccupants } from "../../helpers/lotOccupancyDB/getLotOccupancyOccupants.js"; +export const handler = async (request, response) => { + addLotOccupancyOccupant(request.body, request.session); + const lotOccupancyOccupants = getLotOccupancyOccupants(request.body.lotOccupancyId); + response.json({ + success: true, + lotOccupancyOccupants + }); +}; +export default handler; diff --git a/handlers/lotOccupancies-post/doAddLotOccupancyOccupant.ts b/handlers/lotOccupancies-post/doAddLotOccupancyOccupant.ts new file mode 100644 index 00000000..94a0d1aa --- /dev/null +++ b/handlers/lotOccupancies-post/doAddLotOccupancyOccupant.ts @@ -0,0 +1,27 @@ +import type { + RequestHandler +} from "express"; + +import { + addLotOccupancyOccupant +} from "../../helpers/lotOccupancyDB/addLotOccupancyOccupant.js"; + +import { + getLotOccupancyOccupants +} from "../../helpers/lotOccupancyDB/getLotOccupancyOccupants.js"; + + +export const handler: RequestHandler = async (request, response) => { + + addLotOccupancyOccupant(request.body, request.session); + + const lotOccupancyOccupants = getLotOccupancyOccupants(request.body.lotOccupancyId); + + response.json({ + success: true, + lotOccupancyOccupants + }); +}; + + +export default handler; \ No newline at end of file diff --git a/handlers/lotOccupancies-post/doDeleteLotOccupancyOccupant.d.ts b/handlers/lotOccupancies-post/doDeleteLotOccupancyOccupant.d.ts new file mode 100644 index 00000000..9621c611 --- /dev/null +++ b/handlers/lotOccupancies-post/doDeleteLotOccupancyOccupant.d.ts @@ -0,0 +1,3 @@ +import type { RequestHandler } from "express"; +export declare const handler: RequestHandler; +export default handler; diff --git a/handlers/lotOccupancies-post/doDeleteLotOccupancyOccupant.js b/handlers/lotOccupancies-post/doDeleteLotOccupancyOccupant.js new file mode 100644 index 00000000..6a0907b0 --- /dev/null +++ b/handlers/lotOccupancies-post/doDeleteLotOccupancyOccupant.js @@ -0,0 +1,11 @@ +import { deleteLotOccupancyOccupant } from "../../helpers/lotOccupancyDB/deleteLotOccupancyOccupant.js"; +import { getLotOccupancyOccupants } from "../../helpers/lotOccupancyDB/getLotOccupancyOccupants.js"; +export const handler = async (request, response) => { + deleteLotOccupancyOccupant(request.body.lotOccupancyId, request.body.lotOccupantIndex, request.session); + const lotOccupancyOccupants = getLotOccupancyOccupants(request.body.lotOccupancyId); + response.json({ + success: true, + lotOccupancyOccupants + }); +}; +export default handler; diff --git a/handlers/lotOccupancies-post/doDeleteLotOccupancyOccupant.ts b/handlers/lotOccupancies-post/doDeleteLotOccupancyOccupant.ts new file mode 100644 index 00000000..adea17fc --- /dev/null +++ b/handlers/lotOccupancies-post/doDeleteLotOccupancyOccupant.ts @@ -0,0 +1,27 @@ +import type { + RequestHandler +} from "express"; + +import { + deleteLotOccupancyOccupant +} from "../../helpers/lotOccupancyDB/deleteLotOccupancyOccupant.js"; + +import { + getLotOccupancyOccupants +} from "../../helpers/lotOccupancyDB/getLotOccupancyOccupants.js"; + + +export const handler: RequestHandler = async (request, response) => { + + deleteLotOccupancyOccupant(request.body.lotOccupancyId, request.body.lotOccupantIndex, request.session); + + const lotOccupancyOccupants = getLotOccupancyOccupants(request.body.lotOccupancyId); + + response.json({ + success: true, + lotOccupancyOccupants + }); +}; + + +export default handler; \ No newline at end of file diff --git a/handlers/lotOccupancies-post/doSearchLotOccupancies.ts b/handlers/lotOccupancies-post/doSearchLotOccupancies.ts index 3b5ea26a..7870fe89 100644 --- a/handlers/lotOccupancies-post/doSearchLotOccupancies.ts +++ b/handlers/lotOccupancies-post/doSearchLotOccupancies.ts @@ -1,20 +1,24 @@ -import type { RequestHandler } from "express"; +import type { + RequestHandler +} from "express"; -import { getLotOccupancies } from "../../helpers/lotOccupancyDB/getLotOccupancies.js"; +import { + getLotOccupancies +} from "../../helpers/lotOccupancyDB/getLotOccupancies.js"; export const handler: RequestHandler = async (request, response) => { - const result = getLotOccupancies(request.body, { - limit: request.body.limit, - offset: request.body.offset, - includeOccupants: true - }); + const result = getLotOccupancies(request.body, { + limit: request.body.limit, + offset: request.body.offset, + includeOccupants: true + }); - response.json({ - count: result.count, - lotOccupancies: result.lotOccupancies - }); + response.json({ + count: result.count, + lotOccupancies: result.lotOccupancies + }); }; diff --git a/handlers/lotOccupancies-post/doUpdateLotOccupancy.ts b/handlers/lotOccupancies-post/doUpdateLotOccupancy.ts index 4070100b..719ff058 100644 --- a/handlers/lotOccupancies-post/doUpdateLotOccupancy.ts +++ b/handlers/lotOccupancies-post/doUpdateLotOccupancy.ts @@ -1,16 +1,20 @@ -import type { RequestHandler } from "express"; +import type { + RequestHandler +} from "express"; -import { updateLotOccupancy } from "../../helpers/lotOccupancyDB/updateLotOccupancy.js"; +import { + updateLotOccupancy +} from "../../helpers/lotOccupancyDB/updateLotOccupancy.js"; export const handler: RequestHandler = async (request, response) => { - const success = updateLotOccupancy(request.body, request.session); + const success = updateLotOccupancy(request.body, request.session); - response.json({ - success, - lotOccupancyId: request.body.lotOccupancyId - }); + response.json({ + success, + lotOccupancyId: request.body.lotOccupancyId + }); }; diff --git a/handlers/lotOccupancies-post/doUpdateLotOccupancyOccupant.d.ts b/handlers/lotOccupancies-post/doUpdateLotOccupancyOccupant.d.ts new file mode 100644 index 00000000..9621c611 --- /dev/null +++ b/handlers/lotOccupancies-post/doUpdateLotOccupancyOccupant.d.ts @@ -0,0 +1,3 @@ +import type { RequestHandler } from "express"; +export declare const handler: RequestHandler; +export default handler; diff --git a/handlers/lotOccupancies-post/doUpdateLotOccupancyOccupant.js b/handlers/lotOccupancies-post/doUpdateLotOccupancyOccupant.js new file mode 100644 index 00000000..b7ed4331 --- /dev/null +++ b/handlers/lotOccupancies-post/doUpdateLotOccupancyOccupant.js @@ -0,0 +1,11 @@ +import { updateLotOccupancyOccupant } from "../../helpers/lotOccupancyDB/updateLotOccupancyOccupant.js"; +import { getLotOccupancyOccupants } from "../../helpers/lotOccupancyDB/getLotOccupancyOccupants.js"; +export const handler = async (request, response) => { + updateLotOccupancyOccupant(request.body, request.session); + const lotOccupancyOccupants = getLotOccupancyOccupants(request.body.lotOccupancyId); + response.json({ + success: true, + lotOccupancyOccupants + }); +}; +export default handler; diff --git a/handlers/lotOccupancies-post/doUpdateLotOccupancyOccupant.ts b/handlers/lotOccupancies-post/doUpdateLotOccupancyOccupant.ts new file mode 100644 index 00000000..7cd98440 --- /dev/null +++ b/handlers/lotOccupancies-post/doUpdateLotOccupancyOccupant.ts @@ -0,0 +1,27 @@ +import type { + RequestHandler +} from "express"; + +import { + updateLotOccupancyOccupant +} from "../../helpers/lotOccupancyDB/updateLotOccupancyOccupant.js"; + +import { + getLotOccupancyOccupants +} from "../../helpers/lotOccupancyDB/getLotOccupancyOccupants.js"; + + +export const handler: RequestHandler = async (request, response) => { + + updateLotOccupancyOccupant(request.body, request.session); + + const lotOccupancyOccupants = getLotOccupancyOccupants(request.body.lotOccupancyId); + + response.json({ + success: true, + lotOccupancyOccupants + }); +}; + + +export default handler; \ No newline at end of file diff --git a/helpers/lotOccupancyDB/deleteLotOccupancyOccupant.d.ts b/helpers/lotOccupancyDB/deleteLotOccupancyOccupant.d.ts new file mode 100644 index 00000000..1716e61e --- /dev/null +++ b/helpers/lotOccupancyDB/deleteLotOccupancyOccupant.d.ts @@ -0,0 +1,3 @@ +import type * as recordTypes from "../../types/recordTypes"; +export declare const deleteLotOccupancyOccupant: (lotOccupancyId: number | string, lotOccupantIndex: number | string, requestSession: recordTypes.PartialSession) => boolean; +export default deleteLotOccupancyOccupant; diff --git a/helpers/lotOccupancyDB/deleteLotOccupancyOccupant.js b/helpers/lotOccupancyDB/deleteLotOccupancyOccupant.js new file mode 100644 index 00000000..cb00dbfa --- /dev/null +++ b/helpers/lotOccupancyDB/deleteLotOccupancyOccupant.js @@ -0,0 +1,16 @@ +import sqlite from "better-sqlite3"; +import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; +export const deleteLotOccupancyOccupant = (lotOccupancyId, lotOccupantIndex, requestSession) => { + const database = sqlite(databasePath); + const rightNowMillis = Date.now(); + const result = database + .prepare("update LotOccupancyOccupants" + + " set recordDelete_userName = ?," + + " recordDelete_timeMillis = ?" + + " where lotOccupancyId = ?" + + " and lotOccupantIndex = ?") + .run(requestSession.user.userName, rightNowMillis, lotOccupancyId, lotOccupantIndex); + database.close(); + return (result.changes > 0); +}; +export default deleteLotOccupancyOccupant; diff --git a/helpers/lotOccupancyDB/deleteLotOccupancyOccupant.ts b/helpers/lotOccupancyDB/deleteLotOccupancyOccupant.ts new file mode 100644 index 00000000..de217a3e --- /dev/null +++ b/helpers/lotOccupancyDB/deleteLotOccupancyOccupant.ts @@ -0,0 +1,36 @@ +import sqlite from "better-sqlite3"; + +import { + lotOccupancyDB as databasePath +} from "../../data/databasePaths.js"; + +import type * as recordTypes from "../../types/recordTypes"; + + +export const deleteLotOccupancyOccupant = + (lotOccupancyId: number | string, + lotOccupantIndex: number | string, + requestSession: recordTypes.PartialSession): boolean => { + + const database = sqlite(databasePath); + + const rightNowMillis = Date.now(); + + const result = database + .prepare("update LotOccupancyOccupants" + + " set recordDelete_userName = ?," + + " recordDelete_timeMillis = ?" + + " where lotOccupancyId = ?" + + " and lotOccupantIndex = ?") + .run(requestSession.user.userName, + rightNowMillis, + lotOccupancyId, + lotOccupantIndex); + + database.close(); + + return (result.changes > 0); + }; + + +export default deleteLotOccupancyOccupant; \ No newline at end of file diff --git a/helpers/lotOccupancyDB/updateLotOccupancyOccupant.d.ts b/helpers/lotOccupancyDB/updateLotOccupancyOccupant.d.ts new file mode 100644 index 00000000..5a2476d5 --- /dev/null +++ b/helpers/lotOccupancyDB/updateLotOccupancyOccupant.d.ts @@ -0,0 +1,15 @@ +import type * as recordTypes from "../../types/recordTypes"; +interface UpdateLotOccupancyOccupantForm { + lotOccupancyId: string | number; + lotOccupantIndex: string | number; + lotOccupantTypeId: string | number; + occupantName: string; + occupantAddress1: string; + occupantAddress2: string; + occupantCity: string; + occupantProvince: string; + occupantPostalCode: string; + occupantPhoneNumber: string; +} +export declare const updateLotOccupancyOccupant: (lotOccupancyOccupantForm: UpdateLotOccupancyOccupantForm, requestSession: recordTypes.PartialSession) => boolean; +export default updateLotOccupancyOccupant; diff --git a/helpers/lotOccupancyDB/updateLotOccupancyOccupant.js b/helpers/lotOccupancyDB/updateLotOccupancyOccupant.js new file mode 100644 index 00000000..713e5ec9 --- /dev/null +++ b/helpers/lotOccupancyDB/updateLotOccupancyOccupant.js @@ -0,0 +1,25 @@ +import sqlite from "better-sqlite3"; +import { lotOccupancyDB as databasePath } from "../../data/databasePaths.js"; +export const updateLotOccupancyOccupant = (lotOccupancyOccupantForm, requestSession) => { + const database = sqlite(databasePath); + const rightNowMillis = Date.now(); + const results = database + .prepare("update LotOccupancyOccupants" + + " set occupantName = ?," + + " occupantAddress1 = ?," + + " occupantAddress2 = ?," + + " occupantCity = ?," + + " occupantProvince = ?," + + " occupantPostalCode = ?," + + " occupantPhoneNumber = ?," + + " lotOccupantTypeId = ?," + + " recordUpdate_userName = ?," + + " recordUpdate_timeMillis = ?" + + " where recordDelete_timeMillis is null" + + " and lotOccupancyId = ?" + + " and lotOccupantIndex = ?") + .run(lotOccupancyOccupantForm.occupantName, lotOccupancyOccupantForm.occupantAddress1, lotOccupancyOccupantForm.occupantAddress2, lotOccupancyOccupantForm.occupantCity, lotOccupancyOccupantForm.occupantProvince, lotOccupancyOccupantForm.occupantPostalCode, lotOccupancyOccupantForm.occupantPhoneNumber, lotOccupancyOccupantForm.lotOccupantTypeId, requestSession.user.userName, rightNowMillis, lotOccupancyOccupantForm.lotOccupancyId, lotOccupancyOccupantForm.lotOccupantIndex); + database.close(); + return results.changes > 0; +}; +export default updateLotOccupancyOccupant; diff --git a/helpers/lotOccupancyDB/updateLotOccupancyOccupant.ts b/helpers/lotOccupancyDB/updateLotOccupancyOccupant.ts new file mode 100644 index 00000000..669dbde6 --- /dev/null +++ b/helpers/lotOccupancyDB/updateLotOccupancyOccupant.ts @@ -0,0 +1,65 @@ +import sqlite from "better-sqlite3"; + +import { + lotOccupancyDB as databasePath +} from "../../data/databasePaths.js"; + +import type * as recordTypes from "../../types/recordTypes"; + + +interface UpdateLotOccupancyOccupantForm { + lotOccupancyId: string | number; + lotOccupantIndex: string | number; + lotOccupantTypeId: string | number; + occupantName: string; + occupantAddress1: string; + occupantAddress2: string; + occupantCity: string; + occupantProvince: string; + occupantPostalCode: string; + occupantPhoneNumber: string; +} + + +export const updateLotOccupancyOccupant = + (lotOccupancyOccupantForm: UpdateLotOccupancyOccupantForm, requestSession: recordTypes.PartialSession): boolean => { + + const database = sqlite(databasePath); + + const rightNowMillis = Date.now(); + + const results = database + .prepare("update LotOccupancyOccupants" + + " set occupantName = ?," + + " occupantAddress1 = ?," + + " occupantAddress2 = ?," + + " occupantCity = ?," + + " occupantProvince = ?," + + " occupantPostalCode = ?," + + " occupantPhoneNumber = ?," + + " lotOccupantTypeId = ?," + + " recordUpdate_userName = ?," + + " recordUpdate_timeMillis = ?" + + " where recordDelete_timeMillis is null" + + " and lotOccupancyId = ?" + + " and lotOccupantIndex = ?") + .run(lotOccupancyOccupantForm.occupantName, + lotOccupancyOccupantForm.occupantAddress1, + lotOccupancyOccupantForm.occupantAddress2, + lotOccupancyOccupantForm.occupantCity, + lotOccupancyOccupantForm.occupantProvince, + lotOccupancyOccupantForm.occupantPostalCode, + lotOccupancyOccupantForm.occupantPhoneNumber, + lotOccupancyOccupantForm.lotOccupantTypeId, + requestSession.user.userName, + rightNowMillis, + lotOccupancyOccupantForm.lotOccupancyId, + lotOccupancyOccupantForm.lotOccupantIndex); + + database.close(); + + return results.changes > 0; + }; + + +export default updateLotOccupancyOccupant; \ No newline at end of file diff --git a/public-typescript/lotOccupancyEdit.js b/public-typescript/lotOccupancyEdit.js index 41e72c42..6c815de5 100644 --- a/public-typescript/lotOccupancyEdit.js +++ b/public-typescript/lotOccupancyEdit.js @@ -158,6 +158,103 @@ Object.defineProperty(exports, "__esModule", { value: true }); los.initializeUnlockFieldButtons(formElement); if (!isCreate) { let lotOccupancyOccupants = exports.lotOccupancyOccupants; + const openEditLotOccupancyOccupant = (clickEvent) => { + const lotOccupantIndex = Number.parseInt(clickEvent.currentTarget.closest("tr").dataset.lotOccupantIndex, 10); + const lotOccupancyOccupant = lotOccupancyOccupants.find((currentLotOccupancyOccupant) => { + return currentLotOccupancyOccupant.lotOccupantIndex === lotOccupantIndex; + }); + let editFormElement; + let editCloseModalFunction; + const editOccupant = (submitEvent) => { + submitEvent.preventDefault(); + cityssm.postJSON(urlPrefix + "/lotOccupancies/doUpdateLotOccupancyOccupant", editFormElement, (responseJSON) => { + if (responseJSON.success) { + lotOccupancyOccupants = responseJSON.lotOccupancyOccupants; + editCloseModalFunction(); + renderLotOccupancyOccupants(); + } + else { + bulmaJS.alert({ + title: "Error Updating " + exports.aliases.occupant, + message: responseJSON.errorMessage, + contextualColorName: "danger" + }); + } + }); + }; + cityssm.openHtmlModal("lotOccupancy-editOccupant", { + onshow: (modalElement) => { + los.populateAliases(modalElement); + modalElement.querySelector("#lotOccupancyOccupantEdit--lotOccupancyId").value = lotOccupancyId; + modalElement.querySelector("#lotOccupancyOccupantEdit--lotOccupantIndex").value = lotOccupantIndex.toString(); + const lotOccupantTypeSelectElement = modalElement.querySelector("#lotOccupancyOccupantEdit--lotOccupantTypeId"); + let lotOccupantTypeSelected = false; + for (const lotOccupantType of exports.lotOccupantTypes) { + const optionElement = document.createElement("option"); + optionElement.value = lotOccupantType.lotOccupantTypeId.toString(); + optionElement.textContent = lotOccupantType.lotOccupantType; + if (lotOccupantType.lotOccupantTypeId === lotOccupancyOccupant.lotOccupantTypeId) { + optionElement.selected = true; + lotOccupantTypeSelected = true; + } + lotOccupantTypeSelectElement.append(optionElement); + } + if (!lotOccupantTypeSelected) { + const optionElement = document.createElement("option"); + optionElement.value = lotOccupancyOccupant.lotOccupantTypeId.toString(); + optionElement.textContent = lotOccupancyOccupant.lotOccupantType; + optionElement.selected = true; + lotOccupantTypeSelectElement.append(optionElement); + } + modalElement.querySelector("#lotOccupancyOccupantEdit--occupantName").value = lotOccupancyOccupant.occupantName; + modalElement.querySelector("#lotOccupancyOccupantEdit--occupantAddress1").value = lotOccupancyOccupant.occupantAddress1; + modalElement.querySelector("#lotOccupancyOccupantEdit--occupantAddress2").value = lotOccupancyOccupant.occupantAddress2; + modalElement.querySelector("#lotOccupancyOccupantEdit--occupantCity").value = lotOccupancyOccupant.occupantCity; + modalElement.querySelector("#lotOccupancyOccupantEdit--occupantProvince").value = lotOccupancyOccupant.occupantProvince; + modalElement.querySelector("#lotOccupancyOccupantEdit--occupantPostalCode").value = lotOccupancyOccupant.occupantPostalCode; + modalElement.querySelector("#lotOccupancyOccupantEdit--occupantPhoneNumber").value = lotOccupancyOccupant.occupantPhoneNumber; + }, + onshown: (modalElement, closeModalFunction) => { + bulmaJS.toggleHtmlClipped(); + editFormElement = modalElement.querySelector("form"); + editFormElement.addEventListener("submit", editOccupant); + editCloseModalFunction = closeModalFunction; + }, + onremoved: () => { + bulmaJS.toggleHtmlClipped(); + } + }); + }; + const deleteLotOccupancyOccupant = (clickEvent) => { + const lotOccupantIndex = clickEvent.currentTarget.closest("tr").dataset.lotOccupantIndex; + const doDelete = () => { + cityssm.postJSON(urlPrefix + "/lotOccupancies/doDeleteLotOccupancyOccupant", { + lotOccupancyId, + lotOccupantIndex + }, (responseJSON) => { + if (responseJSON.success) { + lotOccupancyOccupants = responseJSON.lotOccupancyOccupants; + renderLotOccupancyOccupants(); + } + else { + bulmaJS.alert({ + title: "Error Removing " + exports.aliases.occupant, + message: responseJSON.errorMessage, + contextualColorName: "danger" + }); + } + }); + }; + bulmaJS.confirm({ + title: "Remove " + exports.aliases.occupant + "?", + message: "Are you sure you want to remove this " + exports.aliases.occupant.toLowerCase() + "?", + okButton: { + text: "Yes, Remove " + exports.aliases.occupant, + callbackFunction: doDelete + }, + contextualColorName: "warning" + }); + }; const renderLotOccupancyOccupants = () => { const occupantsContainer = document.querySelector("#container--lotOccupancyOccupants"); cityssm.clearElement(occupantsContainer); @@ -200,16 +297,54 @@ Object.defineProperty(exports, "__esModule", { value: true }); "") + "" + ""); + tableRowElement.querySelector(".button--edit").addEventListener("click", openEditLotOccupancyOccupant); + tableRowElement.querySelector(".button--delete").addEventListener("click", deleteLotOccupancyOccupant); tableElement.querySelector("tbody").append(tableRowElement); } occupantsContainer.append(tableElement); }; document.querySelector("#button--addOccupant").addEventListener("click", () => { + let addFormElement; + let addCloseModalFunction; + const addOccupant = (submitEvent) => { + submitEvent.preventDefault(); + cityssm.postJSON(urlPrefix + "/lotOccupancies/doAddLotOccupancyOccupant", addFormElement, (responseJSON) => { + if (responseJSON.success) { + lotOccupancyOccupants = responseJSON.lotOccupancyOccupants; + addCloseModalFunction(); + renderLotOccupancyOccupants(); + } + else { + bulmaJS.alert({ + title: "Error Adding " + exports.aliases.occupant, + message: responseJSON.errorMessage, + contextualColorName: "danger" + }); + } + }); + }; cityssm.openHtmlModal("lotOccupancy-addOccupant", { onshow: (modalElement) => { los.populateAliases(modalElement); + modalElement.querySelector("#lotOccupancyOccupantAdd--lotOccupancyId").value = lotOccupancyId; + const lotOccupantTypeSelectElement = modalElement.querySelector("#lotOccupancyOccupantAdd--lotOccupantTypeId"); + for (const lotOccupantType of exports.lotOccupantTypes) { + const optionElement = document.createElement("option"); + optionElement.value = lotOccupantType.lotOccupantTypeId.toString(); + optionElement.textContent = lotOccupantType.lotOccupantType; + lotOccupantTypeSelectElement.append(optionElement); + } modalElement.querySelector("#lotOccupancyOccupantAdd--occupantCity").value = exports.occupantCityDefault; modalElement.querySelector("#lotOccupancyOccupantAdd--occupantProvince").value = exports.occupantProvinceDefault; + }, + onshown: (modalElement, closeModalFunction) => { + bulmaJS.toggleHtmlClipped(); + addFormElement = modalElement.querySelector("form"); + addFormElement.addEventListener("submit", addOccupant); + addCloseModalFunction = closeModalFunction; + }, + onremoved: () => { + bulmaJS.toggleHtmlClipped(); } }); }); diff --git a/public-typescript/lotOccupancyEdit.ts b/public-typescript/lotOccupancyEdit.ts index 6b82c690..cf438f00 100644 --- a/public-typescript/lotOccupancyEdit.ts +++ b/public-typescript/lotOccupancyEdit.ts @@ -8,7 +8,7 @@ import type { } from "@cityssm/bulma-webapp-js/src/types"; import type { - BulmaJS + BulmaJS, StringConfigProperties } from "@cityssm/bulma-js/types"; declare const cityssm: cityssmGlobal; @@ -249,6 +249,129 @@ declare const bulmaJS: BulmaJS; if (!isCreate) { let lotOccupancyOccupants: recordTypes.LotOccupancyOccupant[] = exports.lotOccupancyOccupants; + const openEditLotOccupancyOccupant = (clickEvent: Event) => { + const lotOccupantIndex = Number.parseInt((clickEvent.currentTarget as HTMLElement).closest("tr").dataset.lotOccupantIndex, 10); + + const lotOccupancyOccupant = lotOccupancyOccupants.find((currentLotOccupancyOccupant) => { + return currentLotOccupancyOccupant.lotOccupantIndex === lotOccupantIndex; + }); + + let editFormElement: HTMLFormElement; + let editCloseModalFunction: () => void; + + const editOccupant = (submitEvent: SubmitEvent) => { + + submitEvent.preventDefault(); + + cityssm.postJSON(urlPrefix + "/lotOccupancies/doUpdateLotOccupancyOccupant", + editFormElement, + (responseJSON: { success: boolean; errorMessage?: string; lotOccupancyOccupants?: recordTypes.LotOccupancyOccupant[]; }) => { + + if (responseJSON.success) { + lotOccupancyOccupants = responseJSON.lotOccupancyOccupants; + editCloseModalFunction(); + renderLotOccupancyOccupants(); + } else { + bulmaJS.alert({ + title: "Error Updating " + exports.aliases.occupant, + message: responseJSON.errorMessage, + contextualColorName: "danger" + }); + } + }); + }; + + cityssm.openHtmlModal("lotOccupancy-editOccupant", { + onshow: (modalElement) => { + los.populateAliases(modalElement); + + (modalElement.querySelector("#lotOccupancyOccupantEdit--lotOccupancyId") as HTMLInputElement).value = lotOccupancyId; + (modalElement.querySelector("#lotOccupancyOccupantEdit--lotOccupantIndex") as HTMLInputElement).value = lotOccupantIndex.toString(); + + const lotOccupantTypeSelectElement = modalElement.querySelector("#lotOccupancyOccupantEdit--lotOccupantTypeId") as HTMLSelectElement; + + let lotOccupantTypeSelected = false; + + for (const lotOccupantType of (exports.lotOccupantTypes as recordTypes.LotOccupantType[])) { + const optionElement = document.createElement("option"); + optionElement.value = lotOccupantType.lotOccupantTypeId.toString(); + optionElement.textContent = lotOccupantType.lotOccupantType; + + if (lotOccupantType.lotOccupantTypeId === lotOccupancyOccupant.lotOccupantTypeId) { + optionElement.selected = true; + lotOccupantTypeSelected = true; + } + + lotOccupantTypeSelectElement.append(optionElement); + } + + if (!lotOccupantTypeSelected) { + const optionElement = document.createElement("option"); + + optionElement.value = lotOccupancyOccupant.lotOccupantTypeId.toString(); + optionElement.textContent = lotOccupancyOccupant.lotOccupantType as string; + optionElement.selected = true; + + lotOccupantTypeSelectElement.append(optionElement); + } + + (modalElement.querySelector("#lotOccupancyOccupantEdit--occupantName") as HTMLInputElement).value = lotOccupancyOccupant.occupantName; + (modalElement.querySelector("#lotOccupancyOccupantEdit--occupantAddress1") as HTMLInputElement).value = lotOccupancyOccupant.occupantAddress1; + (modalElement.querySelector("#lotOccupancyOccupantEdit--occupantAddress2") as HTMLInputElement).value = lotOccupancyOccupant.occupantAddress2; + (modalElement.querySelector("#lotOccupancyOccupantEdit--occupantCity") as HTMLInputElement).value = lotOccupancyOccupant.occupantCity; + (modalElement.querySelector("#lotOccupancyOccupantEdit--occupantProvince") as HTMLInputElement).value = lotOccupancyOccupant.occupantProvince; + (modalElement.querySelector("#lotOccupancyOccupantEdit--occupantPostalCode") as HTMLInputElement).value = lotOccupancyOccupant.occupantPostalCode; + (modalElement.querySelector("#lotOccupancyOccupantEdit--occupantPhoneNumber") as HTMLInputElement).value = lotOccupancyOccupant.occupantPhoneNumber; + }, + onshown: (modalElement, closeModalFunction) => { + + bulmaJS.toggleHtmlClipped(); + + editFormElement = modalElement.querySelector("form"); + editFormElement.addEventListener("submit", editOccupant); + + editCloseModalFunction = closeModalFunction; + }, + onremoved: () => { + bulmaJS.toggleHtmlClipped(); + } + }); + }; + + const deleteLotOccupancyOccupant = (clickEvent: Event) => { + + const lotOccupantIndex = (clickEvent.currentTarget as HTMLElement).closest("tr").dataset.lotOccupantIndex; + + const doDelete = () => { + cityssm.postJSON(urlPrefix + "/lotOccupancies/doDeleteLotOccupancyOccupant", { + lotOccupancyId, + lotOccupantIndex + }, + (responseJSON: { success: boolean; errorMessage?: string; lotOccupancyOccupants: recordTypes.LotOccupancyOccupant[];}) => { + if (responseJSON.success) { + lotOccupancyOccupants = responseJSON.lotOccupancyOccupants; + renderLotOccupancyOccupants(); + } else { + bulmaJS.alert({ + title: "Error Removing " + exports.aliases.occupant, + message: responseJSON.errorMessage, + contextualColorName: "danger" + }); + } + }); + }; + + bulmaJS.confirm({ + title: "Remove " + exports.aliases.occupant + "?", + message: "Are you sure you want to remove this " + exports.aliases.occupant.toLowerCase() + "?", + okButton: { + text: "Yes, Remove " + exports.aliases.occupant, + callbackFunction: doDelete + }, + contextualColorName: "warning" + }); + }; + const renderLotOccupancyOccupants = () => { const occupantsContainer = document.querySelector("#container--lotOccupancyOccupants") as HTMLElement; @@ -290,17 +413,20 @@ declare const bulmaJS: BulmaJS; "") + ("
Searching...
Searching...