update toaster service

This commit is contained in:
Dan Percic 2023-07-10 12:57:00 +03:00
parent a4e425d373
commit eb58e96d01

View File

@ -4,9 +4,9 @@ import { IndividualConfig } from 'ngx-toastr/toastr/toastr-config';
import { NavigationStart, Router } from '@angular/router'; import { NavigationStart, Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http'; import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http';
import { filter } from 'rxjs/operators'; import { filter, tap } from 'rxjs/operators';
import { ErrorMessageService } from './error-message.service'; import { ErrorMessageService } from './error-message.service';
import { DomSanitizer } from '@angular/platform-browser'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
const enum NotificationType { const enum NotificationType {
SUCCESS = 'SUCCESS', SUCCESS = 'SUCCESS',
@ -40,15 +40,18 @@ export interface ErrorToasterOptions extends ToasterOptions {
}) })
export class Toaster { export class Toaster {
constructor( constructor(
router: Router,
private readonly _toastr: ToastrService, private readonly _toastr: ToastrService,
private readonly _router: Router,
private readonly _translateService: TranslateService, private readonly _translateService: TranslateService,
private readonly _errorMessageService: ErrorMessageService, private readonly _errorMessageService: ErrorMessageService,
private readonly _sanitizer: DomSanitizer,
) { ) {
_router.events.pipe(filter(event => event instanceof NavigationStart)).subscribe(() => { router.events
_toastr.clear(); .pipe(
}); filter(event => event instanceof NavigationStart),
tap(() => _toastr.clear()),
takeUntilDestroyed(),
)
.subscribe();
} }
error(message: string, options?: Partial<ErrorToasterOptions>): ActiveToast<unknown> { error(message: string, options?: Partial<ErrorToasterOptions>): ActiveToast<unknown> {
@ -63,18 +66,18 @@ export class Toaster {
} }
info(message: string, options?: Partial<ToasterOptions>): ActiveToast<unknown> { info(message: string, options?: Partial<ToasterOptions>): ActiveToast<unknown> {
return this._showToastNotification(message, NotificationType.INFO, options); return this.#showToastNotification(message, NotificationType.INFO, options);
} }
success(message: string, options?: Partial<ToasterOptions>): ActiveToast<unknown> { success(message: string, options?: Partial<ToasterOptions>): ActiveToast<unknown> {
return this._showToastNotification(message, NotificationType.SUCCESS, options); return this.#showToastNotification(message, NotificationType.SUCCESS, options);
} }
warning(message: string, options?: Partial<ToasterOptions>): ActiveToast<unknown> { warning(message: string, options?: Partial<ToasterOptions>): ActiveToast<unknown> {
return this._showToastNotification(message, NotificationType.WARNING, options); return this.#showToastNotification(message, NotificationType.WARNING, options);
} }
private _showToastNotification( #showToastNotification(
message: string, message: string,
notificationType = NotificationType.INFO, notificationType = NotificationType.INFO,
options?: Partial<ToasterOptions>, options?: Partial<ToasterOptions>,