slidein notification
show error message to user
This commit is contained in:
parent
354d3b8972
commit
b1dbb806c6
4 changed files with 183 additions and 1 deletions
46
src/stores/notification.ts
Normal file
46
src/stores/notification.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
import { defineStore } from "pinia";
|
||||
|
||||
export interface Notification {
|
||||
id: number;
|
||||
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) {
|
||||
let id = Date.now();
|
||||
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);
|
||||
},
|
||||
revoke(id: number) {
|
||||
this.notifications.splice(
|
||||
this.notifications.findIndex((n) => n.id === id),
|
||||
1
|
||||
);
|
||||
clearTimeout(this.timeouts[id]);
|
||||
delete this.timeouts[id];
|
||||
},
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue