mempool/frontend/src/app/components/blocks-list/blocks-list.component.ts

67 lines
2.2 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
2022-03-11 14:54:34 +01:00
import { BehaviorSubject, Observable, timer } from 'rxjs';
import { delayWhen, map, retryWhen, switchMap, tap } from 'rxjs/operators';
import { BlockExtended } from 'src/app/interfaces/node-api.interface';
import { ApiService } from 'src/app/services/api.service';
import { StateService } from 'src/app/services/state.service';
@Component({
selector: 'app-blocks-list',
templateUrl: './blocks-list.component.html',
styleUrls: ['./blocks-list.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class BlocksList implements OnInit {
blocks$: Observable<BlockExtended[]> = undefined
2022-03-11 14:54:34 +01:00
isLoading = true;
2022-03-11 14:54:34 +01:00
fromBlockHeight = undefined;
paginationMaxSize: number;
page = 1;
blocksCount: number;
fromHeightSubject: BehaviorSubject<number> = new BehaviorSubject(this.fromBlockHeight);
constructor(
private apiService: ApiService,
2022-03-11 14:54:34 +01:00
public stateService: StateService,
) {
}
ngOnInit(): void {
2022-03-11 14:54:34 +01:00
this.paginationMaxSize = window.matchMedia('(max-width: 670px)').matches ? 3 : 5;
this.blocks$ = this.fromHeightSubject.pipe(
switchMap(() => {
this.isLoading = true;
return this.apiService.getBlocks$(this.fromBlockHeight)
.pipe(
tap(blocks => {
if (this.blocksCount === undefined) {
this.blocksCount = blocks[0].height;
}
this.isLoading = false;
}),
map(blocks => {
for (const block of blocks) {
// @ts-ignore: Need to add an extra field for the template
block.extras.pool.logo = `./resources/mining-pools/` +
block.extras.pool.name.toLowerCase().replace(' ', '').replace('.', '') + '.svg';
}
return blocks;
}),
retryWhen(errors => errors.pipe(delayWhen(() => timer(1000))))
)
})
);
}
pageChange(page: number) {
this.fromBlockHeight = this.blocksCount - (page - 1) * 15;
this.fromHeightSubject.next(this.fromBlockHeight);
}
trackByBlock(index: number, block: BlockExtended) {
return block.height;
}
}