diff options
Diffstat (limited to 'client/src/app/core/notification')
-rw-r--r-- | client/src/app/core/notification/index.ts | 2 | ||||
-rw-r--r-- | client/src/app/core/notification/notifier.service.ts | 41 | ||||
-rw-r--r-- | client/src/app/core/notification/user-notification-socket.service.ts | 41 |
3 files changed, 84 insertions, 0 deletions
diff --git a/client/src/app/core/notification/index.ts b/client/src/app/core/notification/index.ts new file mode 100644 index 000000000..3e8d9ea65 --- /dev/null +++ b/client/src/app/core/notification/index.ts | |||
@@ -0,0 +1,2 @@ | |||
1 | export * from './notifier.service' | ||
2 | export * from './user-notification-socket.service' | ||
diff --git a/client/src/app/core/notification/notifier.service.ts b/client/src/app/core/notification/notifier.service.ts new file mode 100644 index 000000000..9833c65a0 --- /dev/null +++ b/client/src/app/core/notification/notifier.service.ts | |||
@@ -0,0 +1,41 @@ | |||
1 | import { Injectable } from '@angular/core' | ||
2 | import { MessageService } from 'primeng/api' | ||
3 | import { I18n } from '@ngx-translate/i18n-polyfill' | ||
4 | |||
5 | @Injectable() | ||
6 | export class Notifier { | ||
7 | readonly TIMEOUT = 5000 | ||
8 | |||
9 | constructor ( | ||
10 | private i18n: I18n, | ||
11 | private messageService: MessageService) { | ||
12 | } | ||
13 | |||
14 | info (text: string, title?: string, timeout?: number) { | ||
15 | if (!title) title = this.i18n('Info') | ||
16 | |||
17 | return this.notify('info', text, title, timeout) | ||
18 | } | ||
19 | |||
20 | error (text: string, title?: string, timeout?: number) { | ||
21 | if (!title) title = this.i18n('Error') | ||
22 | |||
23 | return this.notify('error', text, title, timeout) | ||
24 | } | ||
25 | |||
26 | success (text: string, title?: string, timeout?: number) { | ||
27 | if (!title) title = this.i18n('Success') | ||
28 | |||
29 | return this.notify('success', text, title, timeout) | ||
30 | } | ||
31 | |||
32 | private notify (severity: 'success' | 'info' | 'warn' | 'error', text: string, title: string, timeout?: number) { | ||
33 | this.messageService.add({ | ||
34 | severity, | ||
35 | summary: title, | ||
36 | detail: text, | ||
37 | closable: true, | ||
38 | life: timeout || this.TIMEOUT | ||
39 | }) | ||
40 | } | ||
41 | } | ||
diff --git a/client/src/app/core/notification/user-notification-socket.service.ts b/client/src/app/core/notification/user-notification-socket.service.ts new file mode 100644 index 000000000..f367d9ae4 --- /dev/null +++ b/client/src/app/core/notification/user-notification-socket.service.ts | |||
@@ -0,0 +1,41 @@ | |||
1 | import { Injectable } from '@angular/core' | ||
2 | import { environment } from '../../../environments/environment' | ||
3 | import { UserNotification as UserNotificationServer } from '../../../../../shared' | ||
4 | import { Subject } from 'rxjs' | ||
5 | import * as io from 'socket.io-client' | ||
6 | import { AuthService } from '../auth' | ||
7 | |||
8 | export type NotificationEvent = 'new' | 'read' | 'read-all' | ||
9 | |||
10 | @Injectable() | ||
11 | export class UserNotificationSocket { | ||
12 | private notificationSubject = new Subject<{ type: NotificationEvent, notification?: UserNotificationServer }>() | ||
13 | |||
14 | private socket: SocketIOClient.Socket | ||
15 | |||
16 | constructor ( | ||
17 | private auth: AuthService | ||
18 | ) {} | ||
19 | |||
20 | dispatch (type: NotificationEvent, notification?: UserNotificationServer) { | ||
21 | this.notificationSubject.next({ type, notification }) | ||
22 | } | ||
23 | |||
24 | getMyNotificationsSocket () { | ||
25 | const socket = this.getSocket() | ||
26 | |||
27 | socket.on('new-notification', (n: UserNotificationServer) => this.dispatch('new', n)) | ||
28 | |||
29 | return this.notificationSubject.asObservable() | ||
30 | } | ||
31 | |||
32 | private getSocket () { | ||
33 | if (this.socket) return this.socket | ||
34 | |||
35 | this.socket = io(environment.apiUrl + '/user-notifications', { | ||
36 | query: { accessToken: this.auth.getAccessToken() } | ||
37 | }) | ||
38 | |||
39 | return this.socket | ||
40 | } | ||
41 | } | ||