I\'ve a checkout page where the user has to login before he can proceed. The user can be logged on already. In every scenario I want to show a spinner when the component detects
When we want to use a spinner, there are three "components" implicated
A service
export enum loaderCommand { "Begin","End" };
export class LoaderService {
private loaderSource = new Subject();
loaderEvent = this.loaderSource.asObservable();
sendEvent(value: loaderCommand,message?:string) {
this.loaderSource.next({command:value,message:message});
}
}
A component loader
export class LoadingComponent implements OnInit, OnDestroy {
private isAlive: boolean = true;
constructor(private loaderService: LoaderService ) { }
ngOnInit() {
this.dbsService.dbsEvent.pipe(takeWhile(() => this.isAlive)).subscribe((res: any) => {
if (res.command == loaderCommand.Begin) {
this.message = res.message ? res.message : "Loading...";
//do something to show the spinner
}
if (res.command == loaderCommand.End)
//do something to hide the spinner
})
}
ngOnDestroy() {
this.isAlive = false;
}
}
A component, service or interceptor that need show/hide the loading
constructor(private loaderService: loaderService ) { }
//when we want to show the spinner
this.loaderService.sendEvent(loaderCommand.Begin,"Hello word");
//when we want to hide the spinner
this.loaderService.sendEvent(loaderCommand.End);