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