UI: Fixed load counter - requests were not counted when cancel them

This commit is contained in:
Vladyslav_Prykhodko 2021-09-01 12:44:15 +03:00
parent db48d900ae
commit 5f60c5514c

View File

@ -14,21 +14,14 @@
/// limitations under the License.
///
import {
HttpErrorResponse,
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest,
HttpResponseBase
} from '@angular/common/http';
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs/internal/Observable';
import { Inject, Injectable } from '@angular/core';
import { AuthService } from '@core/auth/auth.service';
import { Constants } from '@shared/models/constants';
import { InterceptorHttpParams } from './interceptor-http-params';
import { catchError, delay, mergeMap, switchMap, tap } from 'rxjs/operators';
import { throwError, of } from 'rxjs';
import { catchError, delay, finalize, mergeMap, switchMap } from 'rxjs/operators';
import { of, throwError } from 'rxjs';
import { InterceptorConfig } from './interceptor-config';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
@ -62,17 +55,25 @@ export class GlobalHttpInterceptor implements HttpInterceptor {
if (req.url.startsWith('/api/')) {
const config = this.getInterceptorConfig(req);
this.updateLoadingState(config, true);
let observable$: Observable<HttpEvent<any>>;
if (this.isTokenBasedAuthEntryPoint(req.url)) {
if (!AuthService.getJwtToken() && !this.authService.refreshTokenPending()) {
return this.handleResponseError(req, next, new HttpErrorResponse({error: {message: 'Unauthorized!'}, status: 401}));
observable$ = this.handleResponseError(req, next, new HttpErrorResponse({error: {message: 'Unauthorized!'}, status: 401}));
} else if (!AuthService.isJwtTokenValid()) {
return this.handleResponseError(req, next, new HttpErrorResponse({error: {refreshTokenPending: true}}));
observable$ = this.handleResponseError(req, next, new HttpErrorResponse({error: {refreshTokenPending: true}}));
} else {
return this.jwtIntercept(req, next);
observable$ = this.jwtIntercept(req, next);
}
} else {
return this.handleRequest(req, next);
observable$ = this.handleRequest(req, next);
}
return observable$.pipe(
finalize(() => {
if (req.url.startsWith('/api/')) {
this.updateLoadingState(config, false);
}
})
);
} else {
return next.handle(req);
}
@ -83,43 +84,20 @@ export class GlobalHttpInterceptor implements HttpInterceptor {
if (newReq) {
return this.handleRequest(newReq, next);
} else {
return this.handleRequestError(req, new Error('Could not get JWT token from store.'));
return throwError(new Error('Could not get JWT token from store.'));
}
}
private handleRequest(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
tap((event: HttpEvent<any>) => {
if (event instanceof HttpResponseBase) {
this.handleResponse(req, event as HttpResponseBase);
}
}),
catchError((err) => {
const errorResponse = err as HttpErrorResponse;
return this.handleResponseError(req, next, errorResponse);
}));
}
private handleRequestError(req: HttpRequest<any>, err): Observable<HttpEvent<any>> {
const config = this.getInterceptorConfig(req);
if (req.url.startsWith('/api/')) {
this.updateLoadingState(config, false);
}
return throwError(err);
}
private handleResponse(req: HttpRequest<any>, response: HttpResponseBase) {
const config = this.getInterceptorConfig(req);
if (req.url.startsWith('/api/')) {
this.updateLoadingState(config, false);
}
}
private handleResponseError(req: HttpRequest<any>, next: HttpHandler, errorResponse: HttpErrorResponse): Observable<HttpEvent<any>> {
const config = this.getInterceptorConfig(req);
if (req.url.startsWith('/api/')) {
this.updateLoadingState(config, false);
}
let unhandled = false;
const ignoreErrors = config.ignoreErrors;
const resendRequest = config.resendRequest;