/
notificator.ts
58 lines (47 loc) · 1.94 KB
/
notificator.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
import { h } from 'vue';
import { AnalyticsErrorEventSource } from '@/utils/constants/analyticsEventNames';
import { useNotificationsStore } from '@/store/modules/notificationsStore';
import { APIError } from '@/utils/error';
import { NotificationMessage } from '@/types/DelayedNotification';
/**
* Exposes UI notifications functionality.
*/
export class Notificator {
public constructor() {}
public success(message: NotificationMessage, title?: string): void {
const notificationsStore = useNotificationsStore();
notificationsStore.notifySuccess(message, title);
}
public notifyError(error: Error, source?: AnalyticsErrorEventSource): void {
const notificationsStore = useNotificationsStore();
let msg: NotificationMessage = error.message;
if (error instanceof APIError && error.requestID) {
msg = () => [
h('p', { class: 'message-title' }, error.message),
h('p', { class: 'message-footer' }, `Request ID: ${error.requestID}`),
];
}
notificationsStore.notifyError(msg, source);
}
public error(message: NotificationMessage, source: AnalyticsErrorEventSource | null = null): void {
const notificationsStore = useNotificationsStore();
notificationsStore.notifyError(message, source);
}
public notify(message: NotificationMessage, title?: string): void {
const notificationsStore = useNotificationsStore();
notificationsStore.notifyInfo(message, title);
}
public warning(message: NotificationMessage): void {
const notificationsStore = useNotificationsStore();
notificationsStore.notifyWarning(message);
}
}
export default {
install(app): void {
const instance = new Notificator();
app.config.globalProperties = instance;
app.provide('notify', instance);
},
};