diff --git a/src/lib/services/toaster.service.ts b/src/lib/services/toaster.service.ts index 2e84c8e..33d7814 100644 --- a/src/lib/services/toaster.service.ts +++ b/src/lib/services/toaster.service.ts @@ -1,4 +1,4 @@ -import { Injectable, SecurityContext } from '@angular/core'; +import { Injectable } from '@angular/core'; import { ActiveToast, ToastrService } from 'ngx-toastr'; import { IndividualConfig } from 'ngx-toastr/toastr/toastr-config'; import { NavigationStart, Router } from '@angular/router'; @@ -6,8 +6,8 @@ import { TranslateService } from '@ngx-translate/core'; import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http'; import { filter } from 'rxjs/operators'; import { ErrorMessageService } from './error-message.service'; -import { DomSanitizer } from '@angular/platform-browser'; import { stripHtml } from 'string-strip-html'; +import { DomSanitizer } from '@angular/platform-browser'; const enum NotificationType { SUCCESS = 'SUCCESS', @@ -25,14 +25,13 @@ export interface ToasterOptions extends IndividualConfig { /** * These params are used as interpolateParams for translate service */ - // eslint-disable-next-line @typescript-eslint/ban-types - readonly params?: object; + readonly params?: Record; readonly actions?: ToasterActions[]; } export interface ErrorToasterOptions extends ToasterOptions { /** - * Pass an http error that will be processed by error message service and shown in toast + * Pass a http error that will be processed by error message service and shown in toast */ readonly error?: HttpErrorResponse; } @@ -44,9 +43,9 @@ export class Toaster { constructor( private readonly _toastr: ToastrService, private readonly _router: Router, - private readonly _domSanitize: DomSanitizer, private readonly _translateService: TranslateService, private readonly _errorMessageService: ErrorMessageService, + private readonly _sanitizer: DomSanitizer, ) { _router.events.pipe(filter(event => event instanceof NavigationStart)).subscribe(() => { _toastr.clear(); @@ -81,19 +80,9 @@ export class Toaster { notificationType = NotificationType.INFO, options?: Partial, ): ActiveToast { - const sanitized: any = {}; + const params = options?.params ? this._sanitizeParams(options.params) : undefined; - if (options?.params) { - const params: any = options?.params; - for (let key of Object.keys(params)) { - const value = params[key]; - sanitized[key] = stripHtml(value).result; - } - } - - console.log(sanitized); - - const translatedMsg = this._translateService.instant(message, sanitized) as string; + const translatedMsg = this._translateService.instant(message, params) as string; switch (notificationType) { case NotificationType.SUCCESS: @@ -105,4 +94,11 @@ export class Toaster { return this._toastr.info(translatedMsg, options?.title, options); } } + + private _sanitizeParams(params: Record): Record { + return Object.entries(params).reduce((acc, [key, value]) => { + acc[key] = stripHtml(value.toString()).result; + return acc; + }, {} as Record); + } } diff --git a/src/lib/shared/toast/toast.component.html b/src/lib/shared/toast/toast.component.html index 02c5af8..d17d19e 100644 --- a/src/lib/shared/toast/toast.component.html +++ b/src/lib/shared/toast/toast.component.html @@ -2,7 +2,9 @@
{{ title }}
+
+
{{ message }}