feat: added notification system with toasts #160

This commit is contained in:
2024-09-11 15:04:40 +02:00
parent b7cda2db35
commit 0e186940cc
5 changed files with 62 additions and 3 deletions

40
static/js/toast.js Normal file
View File

@@ -0,0 +1,40 @@
function getClass(type) {
switch (type) {
case "error":
return "alert-error";
case "warning":
return "alert-warning";
case "success":
return "alert-success";
// case "info":
default:
return "alert-info";
}
}
htmx.on("toast", (e) => {
const values = e.detail.value.split("|");
const type = values[0];
const message = values[1];
const template = document.getElementById("toast");
const toast = template.cloneNode(true);
const parent = document.getElementById("toasts");
toast.id = ""
toast.classList.remove("hidden");
toast.classList.add(getClass(type));
toast.innerText = message;
parent.appendChild(toast);
setTimeout(() => {
toast.classList.add("animate-fade");
toast.classList.add("opacity-0");
setTimeout(() => {
parent.removeChild(toast);
}, 250);
}, 5000);
});