Spaces:
Paused
Paused
Create loading.interceptor.ts
Browse files
flare-ui/src/app/interceptors/loading.interceptor.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Injectable } from '@angular/core';
|
| 2 |
+
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
|
| 3 |
+
import { Observable } from 'rxjs';
|
| 4 |
+
import { finalize } from 'rxjs/operators';
|
| 5 |
+
import { LoadingService } from '../services/loading.service';
|
| 6 |
+
|
| 7 |
+
@Injectable()
|
| 8 |
+
export class LoadingInterceptor implements HttpInterceptor {
|
| 9 |
+
constructor(private loadingService: LoadingService) {}
|
| 10 |
+
|
| 11 |
+
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
| 12 |
+
// Skip loading for certain requests
|
| 13 |
+
const skipLoading = req.headers.has('X-Skip-Loading');
|
| 14 |
+
|
| 15 |
+
if (!skipLoading) {
|
| 16 |
+
this.loadingService.show();
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
return next.handle(req).pipe(
|
| 20 |
+
finalize(() => {
|
| 21 |
+
if (!skipLoading) {
|
| 22 |
+
this.loadingService.hide();
|
| 23 |
+
}
|
| 24 |
+
})
|
| 25 |
+
);
|
| 26 |
+
}
|
| 27 |
+
}
|