Files
afrobiz/stores/notify.store.ts
Michael Dausmann a7f8c37f99 prettier fixes #16
2023-10-24 21:18:03 +11:00

52 lines
1.3 KiB
TypeScript

import { defineStore } from 'pinia';
/*
This store manages User and Account state including the ActiveAccount
It is used in the Account administration page and the header due to it's account switching features.
*/
export interface Notification {
message: string;
type: NotificationType;
notifyTime: number;
}
export enum NotificationType {
Info,
Success,
Warning,
Error
}
interface State {
notifications: Notification[];
notificationsArchive: Notification[];
}
export const useNotifyStore = defineStore('notify', {
state: (): State => {
return {
notifications: [],
notificationsArchive: []
};
},
actions: {
notify(messageOrError: unknown, type: NotificationType) {
let message: string = '';
if (messageOrError instanceof Error) message = messageOrError.message;
if (typeof messageOrError === 'string') message = messageOrError;
const notification: Notification = {
message,
type,
notifyTime: Date.now()
};
this.notifications.push(notification);
setTimeout(this.removeNotification.bind(this), 5000, notification);
},
removeNotification(notification: Notification) {
this.notifications = this.notifications.filter(
n => n.notifyTime != notification.notifyTime
);
}
}
});