add toaster service

This commit is contained in:
Dan Percic 2021-08-20 00:05:56 +03:00
parent feebed2c56
commit e6e9fcdc72
3 changed files with 106 additions and 0 deletions

View File

@ -23,6 +23,8 @@ export * from './lib/sorting/sort-by.pipe';
export * from './lib/sorting/sorting.service';
export * from './lib/sorting/models/sorting-option.model';
export * from './lib/sorting/models/sorting-order.type';
export * from './lib/services/toaster.service';
export * from './lib/services/error-message.service';
export * from './lib/search/search.service';
export * from './lib/tables/entities.service';
export * from './lib/tables/listing-component.directive';

View File

@ -0,0 +1,19 @@
import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { HttpErrorResponse } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class ErrorMessageService {
constructor(private readonly _translateService: TranslateService) {}
private _parseErrorResponse(err: HttpErrorResponse): string {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/restrict-template-expressions
return err?.error?.message?.includes('message') ? ` ${err.error.message.match('"message":"(.*?)\\"')[1]}` : '';
}
getMessage(error: HttpErrorResponse, defaultMessage: string): string {
return (this._translateService.instant(defaultMessage) as string) + this._parseErrorResponse(error);
}
}

View File

@ -0,0 +1,85 @@
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';
import { TranslateService } from '@ngx-translate/core';
import { HttpErrorResponse } from '@angular/common/http';
import { filter } from 'rxjs/operators';
import { ErrorMessageService } from './error-message.service';
const enum NotificationType {
SUCCESS = 'SUCCESS',
WARNING = 'WARNING',
INFO = 'INFO'
}
export interface ToasterOptions extends IndividualConfig {
readonly title?: string;
/**
* These params are used as interpolateParams for translate service
*/
// eslint-disable-next-line @typescript-eslint/ban-types
readonly params?: object;
readonly actions?: { readonly title?: string; readonly action: () => void }[];
}
export interface ErrorToasterOptions extends ToasterOptions {
/**
* Pass an http error that will be processed by error message service and shown in toast
*/
readonly error?: HttpErrorResponse;
}
@Injectable({
providedIn: 'root'
})
export class Toaster {
constructor(
private readonly _toastr: ToastrService,
private readonly _router: Router,
private readonly _translateService: TranslateService,
private readonly _errorMessageService: ErrorMessageService
) {
_router.events.pipe(filter(event => event instanceof NavigationStart)).subscribe(() => {
_toastr.clear();
});
}
error(message: string, options?: Partial<ErrorToasterOptions>): ActiveToast<unknown> {
let resultedMsg;
if (options?.error) resultedMsg = this._errorMessageService.getMessage(options.error, message);
else resultedMsg = this._translateService.instant(message, options?.params) as string;
return this._toastr.error(resultedMsg, options?.title, options);
}
info(message: string, options?: Partial<ToasterOptions>): ActiveToast<unknown> {
return this._showToastNotification(message, NotificationType.INFO, options);
}
success(message: string, options?: Partial<ToasterOptions>): ActiveToast<unknown> {
return this._showToastNotification(message, NotificationType.SUCCESS, options);
}
warning(message: string, options?: Partial<ToasterOptions>): ActiveToast<unknown> {
return this._showToastNotification(message, NotificationType.WARNING, options);
}
private _showToastNotification(
message: string,
notificationType = NotificationType.INFO,
options?: Partial<ToasterOptions>
): ActiveToast<unknown> {
const translatedMsg = this._translateService.instant(message, options?.params) as string;
switch (notificationType) {
case NotificationType.SUCCESS:
return this._toastr.success(translatedMsg, options?.title, options);
case NotificationType.WARNING:
return this._toastr.warning(translatedMsg, options?.title, options);
case NotificationType.INFO:
default:
return this._toastr.info(translatedMsg, options?.title, options);
}
}
}