aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/core/notification/notifier.service.ts
blob: 9833c65a02b1a7591f1ee4011dd186c3643299bb (plain) (blame)
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
import { Injectable } from '@angular/core'
import { MessageService } from 'primeng/api'
import { I18n } from '@ngx-translate/i18n-polyfill'

@Injectable()
export class Notifier {
  readonly TIMEOUT = 5000

  constructor (
    private i18n: I18n,
    private messageService: MessageService) {
  }

  info (text: string, title?: string, timeout?: number) {
    if (!title) title = this.i18n('Info')

    return this.notify('info', text, title, timeout)
  }

  error (text: string, title?: string, timeout?: number) {
    if (!title) title = this.i18n('Error')

    return this.notify('error', text, title, timeout)
  }

  success (text: string, title?: string, timeout?: number) {
    if (!title) title = this.i18n('Success')

    return this.notify('success', text, title, timeout)
  }

  private notify (severity: 'success' | 'info' | 'warn' | 'error', text: string, title: string, timeout?: number) {
    this.messageService.add({
      severity,
      summary: title,
      detail: text,
      closable: true,
      life: timeout || this.TIMEOUT
    })
  }
}