I am trying to create an Http interceptor to display loading in all the pages. But I am not able to dismiss the loading controller
I have followed this https://www.youtube.com/watch?v=IJWCpa_-MeU But the problem is loading is displayed infinitely.
Actually I am a bit new to these concepts. So please help me out.
Thank you very much
@Injectable()
export class HttpRequestInterceptor implements HttpInterceptor {
constructor(
private loadingCtrl: LoadingController,
) {}
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
this.loadingCtrl.getTop().then(hasLoading => {
if (!hasLoading) {
this.loadingCtrl.create({
spinner: 'circular',
translucent: true
}).then(loading => loading.present());
}
});
return next.handle(request).pipe(
catchError(err => {
if (err instanceof HttpErrorResponse) {
switch ((<HttpErrorResponse>err).status) {
case 401:
console.log("401")
default:
return throwError(err);
}
} else {
return throwError(err);
}
}),
retryWhen(err => {
let retries = 1;
return err.pipe(
delay(1000),
tap(() => {
// this.showRetryToast(retries);
}),
map(error => {
if (retries++ === 3) {
throw error; // Now retryWhen completes
}
return error;
})
);
}),
catchError(err => {
return EMPTY;
}),
finalize(() => {
this.loadingCtrl.getTop().then(hasLoading => {
if (hasLoading) {
this.loadingCtrl.dismiss();
}
});
})
);
}
}
Here is a simple Http request I made, but data is displayed in console.log but loading is not getting dismissed
ngOnInit() {
this.cartService.getProductDetails().subscribe(data =>{
this.products = data;
this.filter_products = this.products;
console.log(this.products)
})