mempool/frontend/src/app/explorer/block/block.component.ts

72 lines
2.0 KiB
TypeScript
Raw Normal View History

2019-11-06 15:35:02 +08:00
import { Component, OnInit } from '@angular/core';
2019-11-12 16:39:59 +08:00
import { ActivatedRoute, ParamMap } from '@angular/router';
import { ApiService } from 'src/app/services/api.service';
import { MemPoolService } from 'src/app/services/mem-pool.service';
import { switchMap } from 'rxjs/operators';
import { ChangeDetectorRef } from '@angular/core';
2019-11-06 15:35:02 +08:00
@Component({
selector: 'app-block',
templateUrl: './block.component.html',
styleUrls: ['./block.component.scss']
})
export class BlockComponent implements OnInit {
2019-11-12 16:39:59 +08:00
block: any;
isLoadingBlock = true;
latestBlockHeight: number;
transactions: any[];
isLoadingTransactions = true;
2019-11-13 14:51:44 +08:00
error: any;
2019-11-06 15:35:02 +08:00
2019-11-12 16:39:59 +08:00
constructor(
private route: ActivatedRoute,
private apiService: ApiService,
private memPoolService: MemPoolService,
private ref: ChangeDetectorRef,
) { }
2019-11-06 15:35:02 +08:00
ngOnInit() {
2019-11-12 16:39:59 +08:00
this.route.paramMap.pipe(
switchMap((params: ParamMap) => {
2019-11-13 14:51:44 +08:00
this.error = undefined;
2019-11-12 16:39:59 +08:00
this.isLoadingBlock = true;
const blockHash: string = params.get('id') || '';
return this.apiService.getBlock$(blockHash);
})
)
.subscribe((block) => {
this.block = block;
this.isLoadingBlock = false;
2019-11-13 14:51:44 +08:00
this.getBlockTransactions(block.id);
2019-11-12 16:39:59 +08:00
this.ref.markForCheck();
2019-11-13 14:51:44 +08:00
},
(error) => {
this.error = error;
this.isLoadingBlock = false;
2019-11-12 16:39:59 +08:00
});
this.memPoolService.blocks$
.subscribe((block) => {
this.latestBlockHeight = block.height;
});
}
getBlockTransactions(hash: string) {
this.apiService.getBlockTransactions$(hash)
.subscribe((transactions: any) => {
this.transactions = transactions;
this.isLoadingTransactions = false;
});
}
loadMore() {
this.isLoadingTransactions = true;
this.apiService.getBlockTransactions$(this.block.id, this.transactions.length)
.subscribe((transactions) => {
this.transactions = this.transactions.concat(transactions);
this.isLoadingTransactions = false;
});
2019-11-06 15:35:02 +08:00
}
}