2024-12-19 11:16:05 +01:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
|
|
|
|
export interface Notification {
|
2025-01-12 13:25:55 +01:00
|
|
|
id: string;
|
2024-12-19 11:16:05 +01:00
|
|
|
title: string;
|
|
|
|
text: string;
|
|
|
|
type: NotificationType;
|
|
|
|
indicator: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type NotificationType = "info" | "warning" | "error";
|
|
|
|
|
|
|
|
export const useNotificationStore = defineStore("notification", {
|
|
|
|
state: () => {
|
|
|
|
return {
|
|
|
|
notifications: [] as Array<Notification>,
|
|
|
|
timeouts: {} as { [key: string]: any },
|
|
|
|
};
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
push(title: string, text: string, type: NotificationType, timeout: number = 5000) {
|
2025-01-12 13:25:55 +01:00
|
|
|
let id = `${Date.now()}_${Math.random()}`;
|
2024-12-19 11:16:05 +01:00
|
|
|
this.notifications.push({
|
|
|
|
id,
|
|
|
|
title,
|
|
|
|
text,
|
|
|
|
type,
|
|
|
|
indicator: false,
|
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
this.notifications[this.notifications.findIndex((n) => n.id === id)].indicator = true;
|
|
|
|
}, 100);
|
|
|
|
this.timeouts[id] = setTimeout(() => {
|
|
|
|
this.revoke(id);
|
|
|
|
}, timeout);
|
|
|
|
},
|
2025-01-12 13:25:55 +01:00
|
|
|
revoke(id: string) {
|
2024-12-19 11:16:05 +01:00
|
|
|
this.notifications.splice(
|
|
|
|
this.notifications.findIndex((n) => n.id === id),
|
|
|
|
1
|
|
|
|
);
|
|
|
|
clearTimeout(this.timeouts[id]);
|
|
|
|
delete this.timeouts[id];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|