File

src/app/config/interceptors/notification.interceptor.ts

Index

Methods

Constructor

constructor(toasterService: ToasterService)
Parameters :
Name Type Optional
toasterService ToasterService no

Methods

intercept
intercept(request: HttpRequest, next: HttpHandler)
Parameters :
Name Type Optional
request HttpRequest<any> no
next HttpHandler no
Returns : Observable<HttpEvent<any>>
popNotification
popNotification(title: string, body: string)
Parameters :
Name Type Optional
title string no
body string no
Returns : void
import {Injectable} from '@angular/core';
import {
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpInterceptor,
} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {BodyOutputType, Toast, ToasterService} from "angular2-toaster";

const exceptionList: Array<any> = ['/assets/'];

@Injectable()
export class NotificationInterceptor implements HttpInterceptor {
  constructor(private toasterService: ToasterService) {
  }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const {url} = request;
    return next.handle(request).map((event: any) => {
      if (event && event.body) {
        let isUrlException: boolean = false;
        exceptionList.forEach((exception) => {
          if (url.includes(exception)) {
            isUrlException = true;
          }
        });
        if (!isUrlException) {
          this.popNotification('با موفقیت انجام شد', event.body.message);
        }
      }
      return event;
    });
  }

  popNotification(title: string, body: string) {
    const toast: Toast = {
      type: 'success',
      title: title,
      body: body,
      timeout: 3000,
      showCloseButton: true,
      bodyOutputType: BodyOutputType.TrustedHtml,
    };
    this.toasterService.popAsync(toast);
  }
}

results matching ""

    No results matching ""