121 lines
4.0 KiB
TypeScript
121 lines
4.0 KiB
TypeScript
import { HttpErrorResponse, HttpStatusCode } from '@angular/common/http';
|
|
import { Injectable } from '@angular/core';
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
import { NavigationStart, Router } from '@angular/router';
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
import { ActiveToast, ToastrService } from 'ngx-toastr';
|
|
import { IndividualConfig } from 'ngx-toastr/toastr/toastr-config';
|
|
import { filter, tap } from 'rxjs/operators';
|
|
import { ErrorMessageService } from './error-message.service';
|
|
import { isIqserDevMode } from './iqser-user-preference.service';
|
|
|
|
const enum NotificationType {
|
|
SUCCESS = 'SUCCESS',
|
|
WARNING = 'WARNING',
|
|
INFO = 'INFO',
|
|
}
|
|
|
|
export interface ToasterActions {
|
|
readonly title?: string;
|
|
readonly action: () => void;
|
|
}
|
|
|
|
export interface ToasterOptions extends IndividualConfig {
|
|
readonly title?: string;
|
|
/**
|
|
* These params are used as interpolateParams for translate service
|
|
*/
|
|
readonly params?: Record<string, string | number>;
|
|
readonly actions?: ToasterActions[];
|
|
readonly useRaw?: boolean;
|
|
}
|
|
|
|
export interface ErrorToasterOptions extends ToasterOptions {
|
|
/**
|
|
* Pass a http error that will be processed by error message service and shown in toast
|
|
*/
|
|
readonly error?: HttpErrorResponse;
|
|
}
|
|
|
|
const defaultDevToastOptions: Partial<ToasterOptions> = {
|
|
timeOut: 10000,
|
|
easing: 'ease-in-out',
|
|
easeTime: 500,
|
|
useRaw: true,
|
|
};
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class Toaster {
|
|
readonly #isIqserDevMode = isIqserDevMode();
|
|
|
|
constructor(
|
|
router: Router,
|
|
private readonly _toastr: ToastrService,
|
|
private readonly _translateService: TranslateService,
|
|
private readonly _errorMessageService: ErrorMessageService,
|
|
) {
|
|
router.events
|
|
.pipe(
|
|
filter(event => event instanceof NavigationStart),
|
|
tap(() => _toastr.clear()),
|
|
takeUntilDestroyed(),
|
|
)
|
|
.subscribe();
|
|
}
|
|
|
|
error(message: string, options?: Partial<ErrorToasterOptions>): ActiveToast<unknown> {
|
|
let resultedMsg;
|
|
if (options?.error && options.error.status !== HttpStatusCode.Conflict) {
|
|
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);
|
|
}
|
|
|
|
rawError(message: string, config?: Partial<IndividualConfig<unknown>>) {
|
|
return this._toastr.error(message, undefined, config);
|
|
}
|
|
|
|
info(message: string, options?: Partial<ToasterOptions>): ActiveToast<unknown> {
|
|
return this.#showToastNotification(message, NotificationType.INFO, options);
|
|
}
|
|
|
|
devInfo(message: string): ActiveToast<unknown> | undefined {
|
|
if (!this.#isIqserDevMode) {
|
|
return;
|
|
}
|
|
|
|
return this.info(message, defaultDevToastOptions);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
#showToastNotification(
|
|
message: string,
|
|
notificationType = NotificationType.INFO,
|
|
options?: Partial<ToasterOptions>,
|
|
): ActiveToast<unknown> {
|
|
const msg = options?.useRaw ? message : (this._translateService.instant(message, options?.params) as string);
|
|
|
|
switch (notificationType) {
|
|
case NotificationType.SUCCESS:
|
|
return this._toastr.success(msg, options?.title, options);
|
|
case NotificationType.WARNING:
|
|
return this._toastr.warning(msg, options?.title, options);
|
|
case NotificationType.INFO:
|
|
default:
|
|
return this._toastr.info(msg, options?.title, options);
|
|
}
|
|
}
|
|
}
|