| DTabidze | 0757f8a | 2024-03-28 16:49:09 +0400 | [diff] [blame] | 1 | const confirmationDialog = document.getElementById("confirmation"); |
| 2 | const cancelButton = document.getElementById("cancel-button"); |
| 3 | const confirmButton = document.getElementById("confirm-button"); |
| 4 | |
| Davit Tabidze | 5f1a2c6 | 2024-07-17 17:57:27 +0400 | [diff] [blame] | 5 | const errorCancelButton = document.getElementById("error-cancel-button"); |
| 6 | const errorMessageDialog = document.getElementById("error-message"); |
| 7 | |
| 8 | let activeModalStatus = false; |
| 9 | let activeModal = ''; |
| 10 | |
| 11 | function keydownHandler(event) { |
| 12 | if (event.key === "Escape" && activeModalStatus && activeModal === "confirmation") { |
| 13 | hideConfirmationDialog(); |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | function oustideModalHandler(event) { |
| 18 | if (activeModalStatus && confirmationDialog === event.target) { |
| 19 | hideConfirmationDialog(); |
| 20 | errorMessageDialog.close(); |
| 21 | } |
| 22 | } |
| 23 | |
| DTabidze | 0757f8a | 2024-03-28 16:49:09 +0400 | [diff] [blame] | 24 | function showConfirmationDialog(form) { |
| Davit Tabidze | 5f1a2c6 | 2024-07-17 17:57:27 +0400 | [diff] [blame] | 25 | activeModalStatus = true; |
| 26 | activeModal = "confirmation"; |
| 27 | document.addEventListener("keydown", keydownHandler); |
| 28 | document.addEventListener("click", oustideModalHandler); |
| DTabidze | 0757f8a | 2024-03-28 16:49:09 +0400 | [diff] [blame] | 29 | const message = form.dataset.confirmationMessage; |
| 30 | document.getElementById("confirmation-message").innerHTML = message; |
| 31 | confirmationDialog.showModal(); |
| 32 | let confirmed; |
| 33 | let p = new Promise((resolve) => { |
| 34 | confirmed = resolve; |
| 35 | }); |
| 36 | confirmButton.onclick = () => { |
| 37 | confirmed(true); |
| 38 | }; |
| 39 | cancelButton.onclick = () => { |
| 40 | hideConfirmationDialog(); |
| 41 | confirmed(false); |
| 42 | }; |
| 43 | return p; |
| 44 | } |
| 45 | |
| 46 | function hideConfirmationDialog() { |
| Davit Tabidze | 5f1a2c6 | 2024-07-17 17:57:27 +0400 | [diff] [blame] | 47 | activeModalStatus = false; |
| 48 | activeModal = ''; |
| 49 | document.removeEventListener("keydown", keydownHandler); |
| 50 | document.removeEventListener("click", oustideModalHandler); |
| DTabidze | 0757f8a | 2024-03-28 16:49:09 +0400 | [diff] [blame] | 51 | confirmationDialog.close(); |
| 52 | } |
| 53 | |
| 54 | async function handleRemoveOwnerSubmit(form) { |
| 55 | event.preventDefault(); |
| 56 | return await showConfirmationDialog(form); |
| 57 | } |
| 58 | |
| 59 | document.addEventListener("DOMContentLoaded", function () { |
| 60 | const removeOwnerForms = document.querySelectorAll(".remove-form"); |
| 61 | removeOwnerForms.forEach((form) => { |
| 62 | form.addEventListener("submit", async function (event) { |
| 63 | event.preventDefault(); |
| 64 | try { |
| 65 | isConfirmed = await handleRemoveOwnerSubmit(form); |
| 66 | if (isConfirmed) { |
| 67 | form.submit(); |
| 68 | } |
| 69 | } catch (error) { |
| 70 | console.error(error); |
| 71 | } |
| 72 | }); |
| 73 | }); |
| DTabidze | 4b44ff4 | 2024-04-02 03:16:26 +0400 | [diff] [blame] | 74 | |
| DTabidze | 4b44ff4 | 2024-04-02 03:16:26 +0400 | [diff] [blame] | 75 | errorCancelButton.addEventListener("click", function () { |
| 76 | errorMessageDialog.close(); |
| 77 | }); |
| Davit Tabidze | 5f1a2c6 | 2024-07-17 17:57:27 +0400 | [diff] [blame] | 78 | |
| 79 | document.addEventListener("keydown", function (event) { |
| 80 | if (event.key === "Escape") { |
| 81 | errorMessageDialog.close(); |
| 82 | } |
| 83 | }); |
| 84 | document.addEventListener("click", function (event) { |
| 85 | if (errorMessageDialog === event.target) { |
| 86 | errorMessageDialog.close(); |
| 87 | } |
| 88 | }); |
| DTabidze | 4b44ff4 | 2024-04-02 03:16:26 +0400 | [diff] [blame] | 89 | }); |