| @@ -3,11 +3,14 @@ import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/c | |||||
| import { Observable, throwError } from 'rxjs'; | import { Observable, throwError } from 'rxjs'; | ||||
| import { catchError } from 'rxjs/operators'; | import { catchError } from 'rxjs/operators'; | ||||
| import { AccountService } from '@app/_services'; | |||||
| import {AccountService, AlertService} from '@app/_services'; | |||||
| @Injectable() | @Injectable() | ||||
| export class ErrorInterceptor implements HttpInterceptor { | export class ErrorInterceptor implements HttpInterceptor { | ||||
| constructor(private accountService: AccountService) {} | |||||
| constructor( | |||||
| private accountService: AccountService, | |||||
| private alertService: AlertService | |||||
| ) {} | |||||
| intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||||
| return next.handle(request).pipe(catchError(err => { | return next.handle(request).pipe(catchError(err => { | ||||
| @@ -16,6 +19,8 @@ export class ErrorInterceptor implements HttpInterceptor { | |||||
| this.accountService.logout(); | this.accountService.logout(); | ||||
| } | } | ||||
| this.alertService.error(err.message); | |||||
| const error = err.error?.message || err.statusText; | const error = err.error?.message || err.statusText; | ||||
| return throwError(() => error); | return throwError(() => error); | ||||
| })) | })) | ||||
| @@ -0,0 +1,31 @@ | |||||
| import { Injectable } from '@angular/core'; | |||||
| import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http'; | |||||
| import {Observable, tap} from 'rxjs'; | |||||
| import { environment } from '@environments/environment'; | |||||
| import { AccountService } from '@app/_services'; | |||||
| import {LoadingService} from "@app/_services/loading.service"; | |||||
| @Injectable() | |||||
| export class LoadingInterceptor implements HttpInterceptor { | |||||
| constructor( | |||||
| private accountService: AccountService, | |||||
| private loadingService: LoadingService | |||||
| ) { } | |||||
| intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |||||
| this.loadingService.setLoading(true); | |||||
| return next.handle(request).pipe( | |||||
| tap({ | |||||
| next: event => { | |||||
| this.loadingService.setLoading(false); | |||||
| }, | |||||
| error: error => { | |||||
| this.loadingService.setLoading(false); | |||||
| } | |||||
| } | |||||
| ) | |||||
| ); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,20 @@ | |||||
| import { Injectable } from '@angular/core'; | |||||
| import { BehaviorSubject, Observable } from 'rxjs'; | |||||
| @Injectable({ | |||||
| providedIn: 'root' | |||||
| }) | |||||
| export class LoadingService { | |||||
| private loadingSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false); | |||||
| public loading: Observable<boolean> = this.loadingSubject.asObservable(); | |||||
| constructor() { } | |||||
| setLoading(loading: boolean): void { | |||||
| this.loadingSubject.next(loading); | |||||
| } | |||||
| getLoading(): Observable<boolean> { | |||||
| return this.loading; | |||||
| } | |||||
| } | |||||