Main view improvements
This commit is contained in:
42
Frontend/src/app/interceptors/loader.interceptor.ts
Normal file
42
Frontend/src/app/interceptors/loader.interceptor.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {
|
||||
HttpRequest,
|
||||
HttpHandler,
|
||||
HttpEvent,
|
||||
HttpInterceptor
|
||||
} from '@angular/common/http';
|
||||
import { finalize, Observable } from 'rxjs';
|
||||
import { DataService } from '../services/data.service';
|
||||
|
||||
@Injectable()
|
||||
export class LoaderInterceptor implements HttpInterceptor {
|
||||
|
||||
private tasks: number = 0;
|
||||
|
||||
constructor(
|
||||
private _data: DataService
|
||||
) { }
|
||||
|
||||
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
||||
this.addTask();
|
||||
return next.handle(request).pipe(
|
||||
finalize(() => {
|
||||
this.removeTask();
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
addTask() {
|
||||
this.tasks++;
|
||||
if (this.tasks === 1) {
|
||||
this._data.showLoader.next(true);
|
||||
}
|
||||
}
|
||||
|
||||
removeTask() {
|
||||
this.tasks--;
|
||||
if (this.tasks === 0) {
|
||||
this._data.showLoader.next(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user