mempool/frontend/src/app/lightning/channels-list/channels-list.component.ts

90 lines
2.6 KiB
TypeScript
Raw Normal View History

import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core';
2022-05-16 01:36:59 +04:00
import { FormBuilder, FormGroup } from '@angular/forms';
import { BehaviorSubject, merge, Observable } from 'rxjs';
import { map, switchMap, tap } from 'rxjs/operators';
import { isMobile } from 'src/app/shared/common.utils';
2022-05-01 03:01:27 +04:00
import { LightningApiService } from '../lightning-api.service';
@Component({
selector: 'app-channels-list',
templateUrl: './channels-list.component.html',
styleUrls: ['./channels-list.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
2022-05-16 01:36:59 +04:00
export class ChannelsListComponent implements OnInit, OnChanges {
2022-05-01 03:01:27 +04:00
@Input() publicKey: string;
@Output() channelsStatusChangedEvent = new EventEmitter<string>();
2022-08-29 22:25:43 +02:00
@Output() loadingEvent = new EventEmitter<boolean>(false);
2022-05-16 01:36:59 +04:00
channels$: Observable<any>;
// @ts-ignore
paginationSize: 'sm' | 'lg' = 'md';
paginationMaxSize = 10;
itemsPerPage = 10;
2022-05-16 01:36:59 +04:00
page = 1;
channelsPage$ = new BehaviorSubject<number>(1);
channelStatusForm: FormGroup;
defaultStatus = 'open';
status = 'open';
publicKeySize = 25;
2022-08-29 22:25:43 +02:00
isLoading = false;
2022-05-01 03:01:27 +04:00
constructor(
private lightningApiService: LightningApiService,
2022-05-16 01:36:59 +04:00
private formBuilder: FormBuilder,
) {
this.channelStatusForm = this.formBuilder.group({
status: [this.defaultStatus],
});
if (isMobile()) {
this.publicKeySize = 12;
}
2022-05-16 01:36:59 +04:00
}
ngOnInit(): void {
2022-05-16 01:36:59 +04:00
if (document.body.clientWidth < 670) {
this.paginationSize = 'sm';
this.paginationMaxSize = 3;
}
}
2022-05-01 03:01:27 +04:00
ngOnChanges(): void {
2022-08-17 10:23:14 +02:00
this.channelStatusForm.get('status').setValue(this.defaultStatus, { emitEvent: true });
this.channelsPage$.next(1);
2022-07-01 16:50:53 +02:00
this.channels$ = merge(
2022-05-16 01:36:59 +04:00
this.channelsPage$,
this.channelStatusForm.get('status').valueChanges,
)
2022-05-16 01:36:59 +04:00
.pipe(
tap((val) => {
2022-08-29 22:25:43 +02:00
this.isLoading = true;
this.loadingEvent.emit(true);
if (typeof val === 'string') {
this.status = val;
this.page = 1;
} else if (typeof val === 'number') {
this.page = val;
}
}),
switchMap(() => {
2022-08-29 22:25:43 +02:00
this.channelsStatusChangedEvent.emit(this.status);
return this.lightningApiService.getChannelsByNodeId$(this.publicKey, (this.page - 1) * this.itemsPerPage, this.status);
}),
2022-05-16 01:36:59 +04:00
map((response) => {
2022-08-29 22:25:43 +02:00
this.isLoading = false;
this.loadingEvent.emit(false);
2022-05-16 01:36:59 +04:00
return {
channels: response.body,
2022-08-17 10:23:14 +02:00
totalItems: parseInt(response.headers.get('x-total-count'), 10)
2022-05-16 01:36:59 +04:00
};
}),
);
}
pageChange(page: number): void {
2022-05-16 01:36:59 +04:00
this.channelsPage$.next(page);
2022-05-01 03:01:27 +04:00
}
}