ldc-showroom-bo/resources/ts/bo/base/common.ts
2025-05-30 11:38:28 +02:00

99 lines
4.3 KiB
TypeScript

import type { SweetAlertResult } from "sweetalert2";
import Swal from "sweetalert2";
import sweetalert from "../../modules/sweetalertConfirm";
import trans from "../../modules/trans";
window.addEventListener("DOMContentLoaded", () => {
/**
* Popup delete item.
*/
const elementsToDelete = document.getElementsByClassName(
"confirmDeleteTS"
) as HTMLCollectionOf<Element>;
for (const element of elementsToDelete) {
element.addEventListener("submit", function (e) {
e.preventDefault();
const el = e.target;
if (!el || !(el instanceof HTMLFormElement)) {
throw new Error(
"confirmDoneJS can only be executed on an exising form"
);
}
sweetalert.methods.confirm(
"Êtes-vous sûr ?",
"Toutes les données vont être perdu",
el as HTMLFormElement,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function (response: SweetAlertResult<any>) {
(el as HTMLFormElement).submit();
},
{ icon: "warning" }
);
return false;
});
}
/**
* Popup delete item with double check.
*/
const elementsToDeleteDoubleCheck = document.getElementsByClassName("doubleConfirmDeleteTS");
for (const element of elementsToDeleteDoubleCheck) {
element.addEventListener("submit", (e) => {
const self = e.target as HTMLFormElement;
e.preventDefault();
Swal.fire({
title: trans.methods.__("Confirmez"),
text: trans.methods.__("Voulez-vous vraiment supprimer cet élément ?"),
color: getComputedStyle(document.body).getPropertyValue("--bs-body-color"),
icon: "error",
showCancelButton: true,
confirmButtonText:trans.methods.__("Oui"),
cancelButtonText: trans.methods.__("Annuler"),
// * Bootstrap Styling
customClass: {
confirmButton: "btn btn-primary mx-1",
cancelButton: "btn btn-danger mx-1",
},
buttonsStyling: false,
background: getComputedStyle(document.body).getPropertyValue("--bs-body-bg"),
}).then((result) => {
if (result.isConfirmed) {
Swal.fire({
title: trans.methods.__("Attention aux conséquences"),
html: "Attention cette action est irréversible, toutes les informations liées à ce tenant seront supprimées !",
color: getComputedStyle(document.body).getPropertyValue("--bs-body-color"),
icon: "warning",
showCancelButton: true,
confirmButtonText:trans.methods.__("Je comprends le danger et suis conscient que cela aura des conséquences"),
cancelButtonText: trans.methods.__("Annuler"),
// * Bootstrap Styling
customClass: {
confirmButton: "btn btn-primary mx-1",
cancelButton: "btn btn-danger mx-1 mt-1",
},
buttonsStyling: false,
background: getComputedStyle(document.body).getPropertyValue("--bs-body-bg"),
}).then((result) => {
if (result.isConfirmed) {
self?.submit();
}
});
} else if (
/* Read more about handling dismissals below */
result.dismiss === Swal.DismissReason.cancel
) {
Swal.fire({
title: trans.methods.__("L'opération n'a pas été effectuée"),
confirmButtonText: trans.methods.__("Ok"),
// * Bootstrap Styling
customClass: {
confirmButton: "btn btn-primary",
},
buttonsStyling: false,
});
}
});
});
}
});