From eb58e96d01290051858613dcfaed0288adaa3d53 Mon Sep 17 00:00:00 2001 From: Dan Percic Date: Mon, 10 Jul 2023 12:57:00 +0300 Subject: [PATCH] update toaster service --- src/lib/services/toaster.service.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/lib/services/toaster.service.ts b/src/lib/services/toaster.service.ts index 1662d69..726cc7d 100644 --- a/src/lib/services/toaster.service.ts +++ b/src/lib/services/toaster.service.ts @@ -4,9 +4,9 @@ import { IndividualConfig } from 'ngx-toastr/toastr/toastr-config'; import { NavigationStart, Router } from '@angular/router'; import { TranslateService } from '@ngx-translate/core'; 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 { DomSanitizer } from '@angular/platform-browser'; +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; const enum NotificationType { SUCCESS = 'SUCCESS', @@ -40,15 +40,18 @@ export interface ErrorToasterOptions extends ToasterOptions { }) export class Toaster { constructor( + router: Router, private readonly _toastr: ToastrService, - private readonly _router: Router, private readonly _translateService: TranslateService, private readonly _errorMessageService: ErrorMessageService, - private readonly _sanitizer: DomSanitizer, ) { - _router.events.pipe(filter(event => event instanceof NavigationStart)).subscribe(() => { - _toastr.clear(); - }); + router.events + .pipe( + filter(event => event instanceof NavigationStart), + tap(() => _toastr.clear()), + takeUntilDestroyed(), + ) + .subscribe(); } error(message: string, options?: Partial): ActiveToast { @@ -63,18 +66,18 @@ export class Toaster { } info(message: string, options?: Partial): ActiveToast { - return this._showToastNotification(message, NotificationType.INFO, options); + return this.#showToastNotification(message, NotificationType.INFO, options); } success(message: string, options?: Partial): ActiveToast { - return this._showToastNotification(message, NotificationType.SUCCESS, options); + return this.#showToastNotification(message, NotificationType.SUCCESS, options); } warning(message: string, options?: Partial): ActiveToast { - return this._showToastNotification(message, NotificationType.WARNING, options); + return this.#showToastNotification(message, NotificationType.WARNING, options); } - private _showToastNotification( + #showToastNotification( message: string, notificationType = NotificationType.INFO, options?: Partial,