2018-06-30 10:29:22 +02:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {MatSnackBar, MatSnackBarRef} from '@angular/material/snack-bar';
|
2018-10-05 15:51:46 +02:00
|
|
|
import {TranslateService} from '@ngx-translate/core';
|
2018-06-30 10:29:22 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SnackBarService {
|
2018-10-05 15:51:46 +02:00
|
|
|
constructor(private snackbar: MatSnackBar,
|
|
|
|
private translate: TranslateService) {
|
2018-06-30 10:29:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private show(message: string, action?: string, duration?: number, panelClasses?: string[]): MatSnackBarRef<any> {
|
|
|
|
const config = {};
|
|
|
|
if (duration) {
|
|
|
|
config['duration'] = duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action) {
|
|
|
|
if (panelClasses) {
|
|
|
|
panelClasses.push('snack-bar-button');
|
|
|
|
} else {
|
|
|
|
panelClasses = ['snack-bar-button'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (panelClasses) {
|
|
|
|
config['panelClass'] = panelClasses;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.snackbar.open(message, action, config);
|
|
|
|
}
|
|
|
|
|
2018-10-05 15:51:46 +02:00
|
|
|
showSuccess(i18n: string) {
|
|
|
|
this.translate.get(i18n).subscribe((translated) => {
|
|
|
|
return this.show(translated, undefined, 2500, ['custom-snack-bar', 'label-success']);
|
|
|
|
});
|
2018-06-30 10:29:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 15:51:46 +02:00
|
|
|
showError(i18n: string, duration?: number) {
|
|
|
|
this.translate.get(i18n).subscribe((translated) => {
|
|
|
|
this.translate.get('generic.snackbar.error.button.okay').subscribe((translatedOkay) => {
|
2018-10-20 22:40:18 +02:00
|
|
|
return this.show(translated, translatedOkay, duration, ['custom-snack-bar', 'label-danger']);
|
2018-10-05 15:51:46 +02:00
|
|
|
});
|
|
|
|
});
|
2018-06-30 10:29:22 +02:00
|
|
|
}
|
2018-11-24 14:12:05 +01:00
|
|
|
|
|
|
|
showUntranslatedError(message: string, duration?: number) {
|
|
|
|
return this.show(message, 'OK', duration, ['custom-snack-bar', 'label-danger']);
|
|
|
|
}
|
2018-06-30 10:29:22 +02:00
|
|
|
}
|