Selaa lähdekoodia

loading service

master
Daniel 1 vuosi sitten
vanhempi
commit
cfaf9f6535
3 muutettua tiedostoa jossa 58 lisäystä ja 2 poistoa
  1. +7
    -2
      matsen-tool/src/app/_helpers/error.interceptor.ts
  2. +31
    -0
      matsen-tool/src/app/_helpers/loading-interceptor.service.ts
  3. +20
    -0
      matsen-tool/src/app/_services/loading.service.ts

+ 7
- 2
matsen-tool/src/app/_helpers/error.interceptor.ts Näytä tiedosto

@@ -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<any>, next: HttpHandler): Observable<HttpEvent<any>> {
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);
}))


+ 31
- 0
matsen-tool/src/app/_helpers/loading-interceptor.service.ts Näytä tiedosto

@@ -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);
}
}
)
);
}
}

+ 20
- 0
matsen-tool/src/app/_services/loading.service.ts Näytä tiedosto

@@ -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;
}
}

Ladataan…
Peruuta
Tallenna