|
| 1 | +/** |
| 2 | + * Handles deletion of dynamic page rows |
| 3 | + */ |
| 4 | + |
| 5 | +(function () { |
| 6 | + "use strict"; |
| 7 | + |
| 8 | + // Initialize when DOM is loaded |
| 9 | + if (document.readyState === "loading") { |
| 10 | + document.addEventListener("DOMContentLoaded", initRowDeletion); |
| 11 | + } else { |
| 12 | + initRowDeletion(); |
| 13 | + } |
| 14 | + |
| 15 | + function initRowDeletion() { |
| 16 | + // Only run on dynamic-view with edit permissions |
| 17 | + if ( |
| 18 | + !document.body.classList.contains("template-dynamic-view") || |
| 19 | + !document.body.classList.contains("userrole-manager") |
| 20 | + ) { |
| 21 | + console.log( |
| 22 | + "Not in dynamic-view edit mode, skipping row deletion initialization" |
| 23 | + ); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + console.log("Initializing row deletion..."); |
| 28 | + |
| 29 | + const deleteModal = document.getElementById("deleteRowModal"); |
| 30 | + if (!deleteModal) { |
| 31 | + console.error("Delete row modal not found"); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + let rowToDelete = null; |
| 36 | + |
| 37 | + deleteModal.addEventListener("show.bs.modal", function (event) { |
| 38 | + const button = event.relatedTarget; |
| 39 | + rowToDelete = button.closest("section.dynamic-row"); |
| 40 | + console.log("Preparing to delete row:", rowToDelete?.dataset.rowid); |
| 41 | + }); |
| 42 | + |
| 43 | + const confirmButton = document.getElementById("confirmDeleteRow"); |
| 44 | + if (confirmButton) { |
| 45 | + confirmButton.addEventListener("click", handleDeleteRow); |
| 46 | + } else { |
| 47 | + console.error("Confirm delete button not found"); |
| 48 | + } |
| 49 | + |
| 50 | + function handleDeleteRow() { |
| 51 | + if (!rowToDelete) { |
| 52 | + console.error("No row selected for deletion"); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const rowId = rowToDelete.dataset.rowid; |
| 57 | + const rowUrl = rowToDelete.dataset.rowurl; |
| 58 | + |
| 59 | + console.log(`Deleting row ${rowId} via ${rowUrl}`); |
| 60 | + |
| 61 | + fetch(rowUrl, { |
| 62 | + method: "DELETE", |
| 63 | + headers: { |
| 64 | + Accept: "application/json", |
| 65 | + "Content-Type": "application/json", |
| 66 | + "X-CSRF-TOKEN": |
| 67 | + document.querySelector('input[name="_authenticator"]')?.value || "", |
| 68 | + }, |
| 69 | + credentials: "same-origin", |
| 70 | + }) |
| 71 | + .then((response) => { |
| 72 | + console.log(`Received response with status: ${response.status}`); |
| 73 | + console.log("Response headers:", response.headers); |
| 74 | + console.log("Response ok:", response.ok); |
| 75 | + if (!response.ok) { |
| 76 | + const error = new Error(`HTTP error! status: ${response.status}`); |
| 77 | + console.error("Response not OK:", error); |
| 78 | + throw error; |
| 79 | + } |
| 80 | + }) |
| 81 | + .finally(() => { |
| 82 | + const modal = bootstrap.Modal.getInstance(deleteModal); |
| 83 | + if (modal) { |
| 84 | + modal.hide(); |
| 85 | + } |
| 86 | + // Refresh the page after successful update |
| 87 | + window.location.reload(); |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | +})(); |
0 commit comments