-
Notifications
You must be signed in to change notification settings - Fork 47
/
notification_store.ts
35 lines (32 loc) · 1.15 KB
/
notification_store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { InformationNotification } from "../types";
export interface NotificationStoreMethods {
notifyUser: (notification: InformationNotification) => void;
raiseError: (text: string, callback?: () => void) => void;
askConfirmation: (content: string, confirm: () => void, cancel?: () => void) => void;
}
export class NotificationStore {
mutators = [
"notifyUser",
"raiseError",
"askConfirmation",
"updateNotificationCallbacks",
] as const;
notifyUser: NotificationStoreMethods["notifyUser"] = (notification) =>
window.alert(notification.text);
askConfirmation: NotificationStoreMethods["askConfirmation"] = (content, confirm, cancel) => {
if (window.confirm(content)) {
confirm();
} else {
cancel?.();
}
};
raiseError: NotificationStoreMethods["raiseError"] = (text, callback) => {
window.alert(text);
callback?.();
};
updateNotificationCallbacks(methods: Partial<NotificationStoreMethods>) {
this.notifyUser = methods.notifyUser || this.notifyUser;
this.raiseError = methods.raiseError || this.raiseError;
this.askConfirmation = methods.askConfirmation || this.askConfirmation;
}
}