blob: ba58654472b5a7bacbd4a5529d4e89e514791ec3 [file] [log] [blame]
DTabidze0757f8a2024-03-28 16:49:09 +04001const confirmationDialog = document.getElementById("confirmation");
2const cancelButton = document.getElementById("cancel-button");
3const confirmButton = document.getElementById("confirm-button");
4
Davit Tabidze5f1a2c62024-07-17 17:57:27 +04005const errorCancelButton = document.getElementById("error-cancel-button");
6const errorMessageDialog = document.getElementById("error-message");
7
8let activeModalStatus = false;
9let activeModal = '';
10
11function keydownHandler(event) {
12 if (event.key === "Escape" && activeModalStatus && activeModal === "confirmation") {
13 hideConfirmationDialog();
14 }
15}
16
17function oustideModalHandler(event) {
18 if (activeModalStatus && confirmationDialog === event.target) {
19 hideConfirmationDialog();
20 errorMessageDialog.close();
21 }
22}
23
DTabidze0757f8a2024-03-28 16:49:09 +040024function showConfirmationDialog(form) {
Davit Tabidze5f1a2c62024-07-17 17:57:27 +040025 activeModalStatus = true;
26 activeModal = "confirmation";
27 document.addEventListener("keydown", keydownHandler);
28 document.addEventListener("click", oustideModalHandler);
DTabidze0757f8a2024-03-28 16:49:09 +040029 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
46function hideConfirmationDialog() {
Davit Tabidze5f1a2c62024-07-17 17:57:27 +040047 activeModalStatus = false;
48 activeModal = '';
49 document.removeEventListener("keydown", keydownHandler);
50 document.removeEventListener("click", oustideModalHandler);
DTabidze0757f8a2024-03-28 16:49:09 +040051 confirmationDialog.close();
52}
53
54async function handleRemoveOwnerSubmit(form) {
55 event.preventDefault();
56 return await showConfirmationDialog(form);
57}
58
59document.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 });
DTabidze4b44ff42024-04-02 03:16:26 +040074
DTabidze4b44ff42024-04-02 03:16:26 +040075 errorCancelButton.addEventListener("click", function () {
76 errorMessageDialog.close();
77 });
Davit Tabidze5f1a2c62024-07-17 17:57:27 +040078
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 });
DTabidze4b44ff42024-04-02 03:16:26 +040089});