From cfaf9f65354037bd01156ae121cb865619e1e32c Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 6 Jun 2024 16:51:24 +0200 Subject: [PATCH] loading service --- .../src/app/_helpers/error.interceptor.ts | 9 ++++-- .../_helpers/loading-interceptor.service.ts | 31 +++++++++++++++++++ .../src/app/_services/loading.service.ts | 20 ++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 matsen-tool/src/app/_helpers/loading-interceptor.service.ts create mode 100644 matsen-tool/src/app/_services/loading.service.ts diff --git a/matsen-tool/src/app/_helpers/error.interceptor.ts b/matsen-tool/src/app/_helpers/error.interceptor.ts index 42763b2..758b4a7 100644 --- a/matsen-tool/src/app/_helpers/error.interceptor.ts +++ b/matsen-tool/src/app/_helpers/error.interceptor.ts @@ -3,11 +3,14 @@ import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/c import { Observable, throwError } from 'rxjs'; import { catchError } from 'rxjs/operators'; -import { AccountService } from '@app/_services'; +import {AccountService, AlertService} from '@app/_services'; @Injectable() export class ErrorInterceptor implements HttpInterceptor { - constructor(private accountService: AccountService) {} + constructor( + private accountService: AccountService, + private alertService: AlertService + ) {} intercept(request: HttpRequest, next: HttpHandler): Observable> { return next.handle(request).pipe(catchError(err => { @@ -16,6 +19,8 @@ export class ErrorInterceptor implements HttpInterceptor { this.accountService.logout(); } + this.alertService.error(err.message); + const error = err.error?.message || err.statusText; return throwError(() => error); })) diff --git a/matsen-tool/src/app/_helpers/loading-interceptor.service.ts b/matsen-tool/src/app/_helpers/loading-interceptor.service.ts new file mode 100644 index 0000000..8e06418 --- /dev/null +++ b/matsen-tool/src/app/_helpers/loading-interceptor.service.ts @@ -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, next: HttpHandler): Observable> { + + this.loadingService.setLoading(true); + return next.handle(request).pipe( + tap({ + next: event => { + this.loadingService.setLoading(false); + }, + error: error => { + this.loadingService.setLoading(false); + } + } + ) + ); + } +} \ No newline at end of file diff --git a/matsen-tool/src/app/_services/loading.service.ts b/matsen-tool/src/app/_services/loading.service.ts new file mode 100644 index 0000000..61517ea --- /dev/null +++ b/matsen-tool/src/app/_services/loading.service.ts @@ -0,0 +1,20 @@ +import { Injectable } from '@angular/core'; +import { BehaviorSubject, Observable } from 'rxjs'; + +@Injectable({ + providedIn: 'root' +}) +export class LoadingService { + private loadingSubject: BehaviorSubject = new BehaviorSubject(false); + public loading: Observable = this.loadingSubject.asObservable(); + + constructor() { } + + setLoading(loading: boolean): void { + this.loadingSubject.next(loading); + } + + getLoading(): Observable { + return this.loading; + } +} \ No newline at end of file