/
notification.service.ts
54 lines (47 loc) · 2.12 KB
/
notification.service.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
import { Injectable } from '@angular/core';
import { MatSnackBar, MatSnackBarConfig } from '@angular/material/snack-bar';
import { ApiResponseError } from '@dasch-swiss/dsp-js';
import { HttpStatusMsg } from 'src/assets/http/statusMsg';
@Injectable({
providedIn: 'root'
})
export class NotificationService {
constructor(
private _snackBar: MatSnackBar,
private _statusMsg: HttpStatusMsg
) { }
// todo: maybe we can add more parameters like:
// action: string = 'x', duration: number = 4200
// and / or type: 'note' | 'warning' | 'error' | 'success'; which can be used for the panelClass
openSnackBar(notification: string | ApiResponseError, type?: 'success' | 'error'): void {
let message: string;
const conf: MatSnackBarConfig = {
duration: 5000,
horizontalPosition: 'center',
verticalPosition: 'top',
panelClass: type ? type : 'error',
};
if (notification instanceof ApiResponseError) {
conf.panelClass = type ? type : 'error';
if (notification.error && !notification.error['message'].startsWith('ajax error')) {
// the Api response error contains a complex error message from dsp-js-lib
message = notification.error['message'];
} else {
const defaultStatusMsg = this._statusMsg.default;
message = `${defaultStatusMsg[notification.status].message} (${notification.status}): `;
if (notification.status === 504) {
message += `There was a timeout issue with one or several requests.
The resource(s) or a part of it cannot be displayed correctly.
Failed on ${notification.url}`;
conf.duration = null;
} else {
message += `${defaultStatusMsg[notification.status].description}`;
}
}
} else {
conf.panelClass = type ? type : 'success';
message = notification;
}
this._snackBar.open(message, 'x', conf);
}
}