mempool/frontend/src/app/components/transaction/transaction.component.ts

94 lines
2.6 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, OnDestroy } from '@angular/core';
import { ElectrsApiService } from '../../services/electrs-api.service';
import { ActivatedRoute, ParamMap } from '@angular/router';
import { switchMap } from 'rxjs/operators';
import { Transaction, Block } from '../../interfaces/electrs.interface';
import { of } from 'rxjs';
import { StateService } from '../../services/state.service';
import { WebsocketService } from '../../services/websocket.service';
import { AudioService } from 'src/app/services/audio.service';
import { ApiService } from 'src/app/services/api.service';
@Component({
selector: 'app-transaction',
templateUrl: './transaction.component.html',
styleUrls: ['./transaction.component.scss']
})
export class TransactionComponent implements OnInit, OnDestroy {
tx: Transaction;
txId: string;
isLoadingTx = true;
error: any = undefined;
latestBlock: Block;
transactionTime = -1;
rightPosition = 0;
blockDepth = 0;
constructor(
private route: ActivatedRoute,
private electrsApiService: ElectrsApiService,
private stateService: StateService,
private websocketService: WebsocketService,
private audioService: AudioService,
private apiService: ApiService,
) { }
ngOnInit() {
2020-02-24 22:51:27 +07:00
this.websocketService.want(['blocks', 'stats', 'mempool-blocks']);
2020-02-17 20:39:20 +07:00
this.route.paramMap.pipe(
switchMap((params: ParamMap) => {
this.txId = params.get('id') || '';
this.error = undefined;
this.isLoadingTx = true;
2020-02-24 03:42:29 +07:00
document.body.scrollTo(0, 0);
if (history.state.data) {
return of(history.state.data);
} else {
return this.electrsApiService.getTransaction$(this.txId);
}
})
)
.subscribe((tx: Transaction) => {
this.tx = tx;
this.isLoadingTx = false;
if (!tx.status.confirmed) {
this.websocketService.startTrackTransaction(tx.txid);
}
this.getTransactionTime();
},
(error) => {
this.error = error;
this.isLoadingTx = false;
});
this.stateService.blocks$
.subscribe((block) => this.latestBlock = block);
2020-02-23 19:16:50 +07:00
this.stateService.txConfirmed$
.subscribe((block) => {
this.tx.status = {
confirmed: true,
block_height: block.height,
block_hash: block.id,
block_time: block.timestamp,
};
this.audioService.playSound('magic');
});
}
getTransactionTime() {
this.apiService.getTransactionTimes$([this.tx.txid])
.subscribe((transactionTimes) => {
this.transactionTime = transactionTimes[0];
});
}
ngOnDestroy() {
this.websocketService.startTrackTransaction('stop');
}
}