Created
March 20, 2021 10:07
-
-
Save muhammadawaisshaikh/578b047ca6734dfcf9c63ce5f92bf7ae to your computer and use it in GitHub Desktop.
Implementing global loader Interceptor for Http Calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BrowserModule } from '@angular/platform-browser'; | |
import { NgModule } from '@angular/core'; | |
import { AppRoutingModule } from './app-routing.module'; | |
import { HttpClientModule } from '@angular/common/http'; | |
... | |
// for token interceptor | |
import { HTTP_INTERCEPTORS } from '@angular/common/http'; | |
import { LoaderInterceptorService } from './core/interceptors/loader-interceptor/loader-interceptor.service'; | |
... | |
@NgModule({ | |
declarations: [ | |
... | |
], | |
imports: [ | |
... | |
], | |
providers: [ | |
{ | |
provide: HTTP_INTERCEPTORS, | |
useClass: LoaderInterceptorService, | |
multi: true | |
} | |
], | |
bootstrap: [AppComponent] | |
}) | |
export class AppModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { | |
HttpRequest, | |
HttpHandler, | |
HttpEvent, | |
HttpInterceptor, | |
HttpResponse | |
} from '@angular/common/http'; | |
import { Observable } from 'rxjs'; | |
import { LoaderService } from '../../services/loader/loader.service'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class LoaderInterceptorService implements HttpInterceptor { | |
private requests: HttpRequest<any>[] = []; | |
constructor( | |
private loaderService: LoaderService | |
) { } | |
removeRequest(req: HttpRequest<any>) { | |
const i = this.requests.indexOf(req); | |
if (i >= 0) { | |
this.requests.splice(i, 1); | |
} | |
this.loaderService.isLoading.next(this.requests.length > 0); | |
} | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
this.requests.push(req); | |
console.log("No of requests--->" + this.requests.length); | |
this.loaderService.isLoading.next(true); | |
return Observable.create(observer => { | |
const subscription = next.handle(req) | |
.subscribe( | |
event => { | |
if (event instanceof HttpResponse) { | |
this.removeRequest(req); | |
observer.next(event); | |
} | |
}, | |
err => { | |
alert('error' + err); | |
this.removeRequest(req); | |
observer.error(err); | |
}, | |
() => { | |
this.removeRequest(req); | |
observer.complete(); | |
}); | |
// remove request from queue when cancelled | |
return () => { | |
this.removeRequest(req); | |
subscription.unsubscribe(); | |
}; | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div *ngIf="loading"> | |
<img class="loading all-center" src={{loader}} /> | |
<div class="loader-bg"></div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import '../../../styles.scss'; | |
.loader-bg{ | |
background: #ffffff; | |
opacity: 0.9; | |
position: fixed; | |
top: 0; | |
width: 100%; | |
height: 100%; | |
} | |
.loading { | |
position: absolute; | |
z-index: 1; | |
width: 100px; | |
height: auto; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { LoaderService } from '../../core/services/loader/loader.service'; | |
@Component({ | |
selector: 'app-loader', | |
templateUrl: './loader.component.html', | |
styleUrls: ['./loader.component.scss'] | |
}) | |
export class LoaderComponent implements OnInit { | |
loading: boolean; | |
loader: any = "../../../../assets/img/loading.gif"; | |
constructor( | |
private loaderService: LoaderService | |
) { } | |
ngOnInit(): void { | |
this.loaderService.isLoading.subscribe((v) => { | |
console.log(v); | |
this.loading = v; | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { BehaviorSubject } from 'rxjs'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class LoaderService { | |
public isLoading = new BehaviorSubject(false); | |
constructor() { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would be available all day tomorrow (saturday)... Maybe that would work best?