I get a transaction from API service, the condition is if the transaction status is 'pending', keep reloading and subscribing the transaction until the transaction status is 'completed' or 'rejected'. My code only works for the first time, then next time visit, the page is blank but the data still runs in console even though I unsubscribed it.
Here is my code:
export class TransactionSummaryComponent implements OnInit, OnDestroy {
transaction: Models.Transaction = <Models.Transaction>{};
cancelling: boolean = false;
goToPayment: boolean = false;
private dataRefreshSub: Subscription;
private subscribeToDataSub: Subscription;
private timer: Observable<any>;
constructor(
private route: ActivatedRoute,
private router: Router,
private apiService: ApiService,
private zone: NgZone,
@Inject(PLATFORM_ID) private platformId: Object) { }
ngOnInit() {
if (isPlatformBrowser(this.platformId)) {
this.getTransaction();
}
}
getTransaction() {
this.route.paramMap
.switchMap((params: ParamMap) => this.apiService.getTransaction(params.get('id')))
.subscribe((transaction: Models.Transaction) => {
this.transaction = transaction;
if (this.transaction.status === 'Pending') {
this.refreshData();
}
});
}
refreshData() {
this.dataRefreshSub = this.route.paramMap
.switchMap((params: ParamMap) => this.apiService.getTransaction(params.get('id')))
.subscribe((transaction: Models.Transaction) => {
this.transaction = transaction;
this.subscribeToData();
});
}
subscribeToData() {
this.zone.runOutsideAngular(() => {
NgZone.assertNotInAngularZone();
this.timer = Observable.timer(1, 5000);
this.subscribeToDataSub = this.timer
.subscribe(() => {
this.refreshData();
});
});
}
ngOnDestroy() {
if (this.dataRefreshSub !== undefined) {
this.dataRefreshSub.unsubscribe();
}
if (this.subscribeToDataSub !== undefined) {
this.subscribeToDataSub.unsubscribe();
}
}
}