You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RTL/src/app/clightning/lookups/lookups.component.ts

115 lines
3.6 KiB
TypeScript

import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core';
5 years ago
import { Subject } from 'rxjs';
import { takeUntil, filter } from 'rxjs/operators';
import { Store } from '@ngrx/store';
import { Actions } from '@ngrx/effects';
import { faSearch } from '@fortawesome/free-solid-svg-icons';
5 years ago
import { LoggerService } from '../../shared/services/logger.service';
import * as RTLActions from '../../store/rtl.actions';
import * as fromRTLReducer from '../../store/rtl.reducers';
import { ScreenSizeEnum } from '../../shared/services/consts-enums-functions';
import { CommonService } from '../../shared/services/common.service';
5 years ago
@Component({
selector: 'rtl-cl-lookups',
templateUrl: './lookups.component.html',
styleUrls: ['./lookups.component.scss']
})
export class CLLookupsComponent implements OnInit, OnDestroy {
@ViewChild('form', { static: false }) form: any;
5 years ago
public lookupKey = '';
public lookupValue = {};
public flgSetLookupValue = false;
public temp: any;
public messageObj = [];
public selectedFieldId = 0;
5 years ago
public lookupFields = [
{ id: 0, name: 'Node', placeholder: 'Pubkey'},
{ id: 1, name: 'Channel', placeholder: 'Short Channel ID'}
5 years ago
];
public flgLoading: Array<Boolean | 'error'> = [true];
public faSearch = faSearch;
public screenSize = '';
public screenSizeEnum = ScreenSizeEnum;
5 years ago
private unSubs: Array<Subject<void>> = [new Subject()];
constructor(private logger: LoggerService, private commonService: CommonService, private store: Store<fromRTLReducer.RTLState>, private actions$: Actions) {
this.screenSize = this.commonService.getScreenSize();
}
5 years ago
ngOnInit() {
this.actions$
.pipe(
takeUntil(this.unSubs[0]),
5 years ago
filter((action) => (action.type === RTLActions.SET_LOOKUP_CL || action.type === RTLActions.EFFECT_ERROR_CL))
).subscribe((resLookup: RTLActions.SetLookupCL | RTLActions.EffectErrorCl) => {
if(resLookup.type === RTLActions.SET_LOOKUP_CL) {
5 years ago
this.flgLoading[0] = true;
switch (this.selectedFieldId) {
case 0:
this.lookupValue = JSON.parse(JSON.stringify(resLookup.payload[0]));
break;
case 1:
this.lookupValue = JSON.parse(JSON.stringify(resLookup.payload));
break;
default:
break;
}
5 years ago
this.flgSetLookupValue = true;
this.logger.info(this.lookupValue);
}
if (resLookup.type === RTLActions.EFFECT_ERROR_CL && resLookup.payload.action === 'LookupCL') {
this.flgLoading[0] = 'error';
}
5 years ago
});
}
onLookup() {
if(!this.lookupKey) { return true; }
5 years ago
this.flgSetLookupValue = false;
this.lookupValue = {};
this.store.dispatch(new RTLActions.OpenSpinner('Searching ' + this.lookupFields[this.selectedFieldId].name + '...'));
switch (this.selectedFieldId) {
case 0:
5 years ago
this.store.dispatch(new RTLActions.PeerLookupCL(this.lookupKey.trim()));
5 years ago
break;
case 1:
5 years ago
this.store.dispatch(new RTLActions.ChannelLookupCL(this.lookupKey.trim()));
5 years ago
break;
default:
break;
}
}
onSelectChange(event: any) {
this.resetData();
this.selectedFieldId = event.value;
5 years ago
}
resetData() {
this.form.resetForm();
5 years ago
this.flgSetLookupValue = false;
this.selectedFieldId = 0;
5 years ago
this.lookupKey = '';
this.lookupValue = {};
this.flgLoading.forEach((flg, i) => {
this.flgLoading[i] = true;
});
}
clearLookupValue() {
this.lookupValue = {};
this.flgSetLookupValue = false;
}
ngOnDestroy() {
this.unSubs.forEach(completeSub => {
completeSub.next();
completeSub.complete();
});
}
}