2022-05-01 03:01:27 +04:00
|
|
|
import { ChangeDetectionStrategy, Component, Input, OnChanges, OnInit } from '@angular/core';
|
2022-05-16 01:36:59 +04:00
|
|
|
import { FormBuilder, FormGroup } from '@angular/forms';
|
|
|
|
import { BehaviorSubject, combineLatest, merge, Observable, of } from 'rxjs';
|
|
|
|
import { map, startWith, switchMap } from 'rxjs/operators';
|
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;
|
2022-05-16 01:36:59 +04:00
|
|
|
channels$: Observable<any>;
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
paginationSize: 'sm' | 'lg' = 'md';
|
|
|
|
paginationMaxSize = 10;
|
|
|
|
itemsPerPage = 25;
|
|
|
|
page = 1;
|
|
|
|
channelsPage$ = new BehaviorSubject<number>(1);
|
|
|
|
channelStatusForm: FormGroup;
|
|
|
|
defaultStatus = 'open';
|
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],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
if (document.body.clientWidth < 670) {
|
|
|
|
this.paginationSize = 'sm';
|
|
|
|
this.paginationMaxSize = 3;
|
|
|
|
}
|
|
|
|
}
|
2022-05-01 03:01:27 +04:00
|
|
|
|
|
|
|
ngOnChanges(): void {
|
2022-05-16 01:36:59 +04:00
|
|
|
this.channels$ = combineLatest([
|
|
|
|
this.channelsPage$,
|
|
|
|
this.channelStatusForm.get('status').valueChanges.pipe(startWith(this.defaultStatus))
|
|
|
|
])
|
|
|
|
.pipe(
|
|
|
|
switchMap(([page, status]) =>this.lightningApiService.getChannelsByNodeId$(this.publicKey, (page -1) * this.itemsPerPage, status)),
|
|
|
|
map((response) => {
|
|
|
|
return {
|
|
|
|
channels: response.body,
|
|
|
|
totalItems: parseInt(response.headers.get('x-total-count'), 10)
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
pageChange(page: number) {
|
|
|
|
this.channelsPage$.next(page);
|
2022-05-01 03:01:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|