2021-06-20 20:27:08 +00:00
|
|
|
import { Component, OnInit, OnDestroy, ViewChild, Input, AfterViewInit } from '@angular/core';
|
|
|
|
import { DecimalPipe, DatePipe } from '@angular/common';
|
2020-07-07 17:57:15 +00:00
|
|
|
import { Subject } from 'rxjs';
|
2021-08-28 21:03:18 +00:00
|
|
|
import { filter, takeUntil } from 'rxjs/operators';
|
2020-07-07 17:57:15 +00:00
|
|
|
import { Store } from '@ngrx/store';
|
2021-08-28 21:03:18 +00:00
|
|
|
import { Actions } from '@ngrx/effects';
|
2020-07-07 17:57:15 +00:00
|
|
|
import { faHistory } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
import { MatPaginator, MatPaginatorIntl } from '@angular/material/paginator';
|
|
|
|
import { MatSort } from '@angular/material/sort';
|
|
|
|
import { MatTableDataSource } from '@angular/material/table';
|
|
|
|
|
2021-12-29 23:08:41 +00:00
|
|
|
import { CurrencyUnitEnum, CURRENCY_UNIT_FORMATS, PAGE_SIZE, PAGE_SIZE_OPTIONS, getPaginatorLabel, ScreenSizeEnum, APICallStatusEnum, ECLActions } from '../../../shared/services/consts-enums-functions';
|
2020-07-07 17:57:15 +00:00
|
|
|
import { SelNodeChild } from '../../../shared/models/RTLconfig';
|
|
|
|
import { GetInfo, Invoice } from '../../../shared/models/eclModels';
|
2021-12-29 23:08:41 +00:00
|
|
|
import { ApiCallStatusPayload } from '../../../shared/models/apiCallsPayload';
|
2020-07-07 17:57:15 +00:00
|
|
|
import { LoggerService } from '../../../shared/services/logger.service';
|
|
|
|
import { CommonService } from '../../../shared/services/common.service';
|
|
|
|
|
|
|
|
import { ECLCreateInvoiceComponent } from '../create-invoice-modal/create-invoice.component';
|
|
|
|
import { ECLInvoiceInformationComponent } from '../invoice-information-modal/invoice-information.component';
|
2021-06-20 20:27:08 +00:00
|
|
|
|
2021-12-29 23:08:41 +00:00
|
|
|
import { RTLState } from '../../../store/rtl.state';
|
|
|
|
import { openAlert } from '../../../store/rtl.actions';
|
|
|
|
import { createInvoice, invoiceLookup } from '../../store/ecl.actions';
|
2022-10-20 17:14:08 +00:00
|
|
|
import { eclNodeInformation, eclNodeSettings, invoices } from '../../store/ecl.selector';
|
2020-07-07 17:57:15 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'rtl-ecl-lightning-invoices',
|
|
|
|
templateUrl: './lightning-invoices.component.html',
|
|
|
|
styleUrls: ['./lightning-invoices.component.scss'],
|
|
|
|
providers: [
|
2021-08-28 21:03:18 +00:00
|
|
|
{ provide: MatPaginatorIntl, useValue: getPaginatorLabel('Invoices') }
|
|
|
|
]
|
2020-07-07 17:57:15 +00:00
|
|
|
})
|
2021-06-20 20:27:08 +00:00
|
|
|
export class ECLLightningInvoicesComponent implements OnInit, AfterViewInit, OnDestroy {
|
2021-08-28 21:03:18 +00:00
|
|
|
|
|
|
|
@Input() calledFrom = 'transactions'; // Transactions/home
|
2021-12-29 23:08:41 +00:00
|
|
|
@ViewChild(MatSort, { static: false }) sort: MatSort | undefined;
|
|
|
|
@ViewChild(MatPaginator, { static: false }) paginator: MatPaginator | undefined;
|
2020-07-07 17:57:15 +00:00
|
|
|
faHistory = faHistory;
|
2022-08-19 04:41:59 +00:00
|
|
|
public selNode: SelNodeChild | null = {};
|
|
|
|
public newlyAddedInvoiceMemo: string | null = '';
|
|
|
|
public newlyAddedInvoiceValue: number | null = 0;
|
|
|
|
public description: string | null = '';
|
|
|
|
public expiry: number | null;
|
|
|
|
public invoiceValue: number | null = null;
|
2020-07-07 17:57:15 +00:00
|
|
|
public invoiceValueHint = '';
|
2020-12-20 23:36:04 +00:00
|
|
|
public displayedColumns: any[] = [];
|
2020-07-07 17:57:15 +00:00
|
|
|
public invoicePaymentReq = '';
|
|
|
|
public invoices: any;
|
2021-08-28 21:03:18 +00:00
|
|
|
public invoiceJSONArr: Invoice[] = [];
|
2020-07-07 17:57:15 +00:00
|
|
|
public information: GetInfo = {};
|
2021-12-29 23:08:41 +00:00
|
|
|
public selFilter = '';
|
2020-07-07 17:57:15 +00:00
|
|
|
public pageSize = PAGE_SIZE;
|
|
|
|
public pageSizeOptions = PAGE_SIZE_OPTIONS;
|
|
|
|
public screenSize = '';
|
|
|
|
public screenSizeEnum = ScreenSizeEnum;
|
2021-08-28 21:03:18 +00:00
|
|
|
public errorMessage = '';
|
2022-08-11 09:17:43 +00:00
|
|
|
public apiCallStatus: ApiCallStatusPayload | null = null;
|
2021-08-28 21:03:18 +00:00
|
|
|
public apiCallStatusEnum = APICallStatusEnum;
|
2020-07-07 17:57:15 +00:00
|
|
|
private unSubs: Array<Subject<void>> = [new Subject(), new Subject(), new Subject(), new Subject(), new Subject()];
|
|
|
|
|
2021-12-29 23:08:41 +00:00
|
|
|
constructor(private logger: LoggerService, private store: Store<RTLState>, private decimalPipe: DecimalPipe, private commonService: CommonService, private datePipe: DatePipe, private actions: Actions) {
|
2020-07-07 17:57:15 +00:00
|
|
|
this.screenSize = this.commonService.getScreenSize();
|
2021-08-28 21:03:18 +00:00
|
|
|
if (this.screenSize === ScreenSizeEnum.XS) {
|
2020-08-02 19:48:55 +00:00
|
|
|
this.displayedColumns = ['timestamp', 'amount', 'actions'];
|
2021-08-28 21:03:18 +00:00
|
|
|
} else if (this.screenSize === ScreenSizeEnum.SM) {
|
2020-10-12 22:43:16 +00:00
|
|
|
this.displayedColumns = ['timestamp', 'amount', 'amountSettled', 'actions'];
|
2021-08-28 21:03:18 +00:00
|
|
|
} else if (this.screenSize === ScreenSizeEnum.MD) {
|
2020-10-12 22:43:16 +00:00
|
|
|
this.displayedColumns = ['timestamp', 'amount', 'amountSettled', 'actions'];
|
2020-07-07 17:57:15 +00:00
|
|
|
} else {
|
2020-10-12 22:43:16 +00:00
|
|
|
this.displayedColumns = ['timestamp', 'receivedAt', 'description', 'amount', 'amountSettled', 'actions'];
|
2020-07-07 17:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2022-10-20 17:14:08 +00:00
|
|
|
this.store.select(eclNodeSettings).pipe(takeUntil(this.unSubs[0])).
|
2022-08-19 04:41:59 +00:00
|
|
|
subscribe((nodeSettings: SelNodeChild | null) => {
|
2021-12-29 23:08:41 +00:00
|
|
|
this.selNode = nodeSettings;
|
|
|
|
});
|
|
|
|
this.store.select(eclNodeInformation).pipe(takeUntil(this.unSubs[1])).
|
|
|
|
subscribe((nodeInfo: any) => {
|
|
|
|
this.information = <GetInfo>nodeInfo;
|
|
|
|
});
|
|
|
|
this.store.select(invoices).pipe(takeUntil(this.unSubs[2])).
|
|
|
|
subscribe((invoicesSelector: { invoices: Invoice[], apiCallStatus: ApiCallStatusPayload }) => {
|
2021-08-28 21:03:18 +00:00
|
|
|
this.errorMessage = '';
|
2021-12-29 23:08:41 +00:00
|
|
|
this.apiCallStatus = invoicesSelector.apiCallStatus;
|
|
|
|
if (this.apiCallStatus.status === APICallStatusEnum.ERROR) {
|
2022-08-11 09:17:43 +00:00
|
|
|
this.errorMessage = !this.apiCallStatus.message ? '' : (typeof (this.apiCallStatus.message) === 'object') ? JSON.stringify(this.apiCallStatus.message) : this.apiCallStatus.message;
|
2021-08-28 21:03:18 +00:00
|
|
|
}
|
2021-12-29 23:08:41 +00:00
|
|
|
this.invoiceJSONArr = (invoicesSelector.invoices && invoicesSelector.invoices.length > 0) ? invoicesSelector.invoices : [];
|
2021-08-28 21:03:18 +00:00
|
|
|
if (this.invoiceJSONArr && this.invoiceJSONArr.length > 0 && this.sort && this.paginator) {
|
|
|
|
this.loadInvoicesTable(this.invoiceJSONArr);
|
|
|
|
}
|
2021-12-29 23:08:41 +00:00
|
|
|
this.logger.info(invoicesSelector);
|
2021-08-28 21:03:18 +00:00
|
|
|
});
|
2021-12-29 23:08:41 +00:00
|
|
|
this.actions.pipe(takeUntil(this.unSubs[3]), filter((action) => (action.type === ECLActions.SET_LOOKUP_ECL || action.type === ECLActions.UPDATE_API_CALL_STATUS_ECL))).
|
|
|
|
subscribe((resLookup: any) => {
|
2021-08-28 21:03:18 +00:00
|
|
|
if (resLookup.type === ECLActions.SET_LOOKUP_ECL) {
|
|
|
|
if (this.invoiceJSONArr.length > 0 && this.sort && this.paginator && resLookup.payload) {
|
|
|
|
this.updateInvoicesData(JSON.parse(JSON.stringify(resLookup.payload)));
|
|
|
|
this.loadInvoicesTable(this.invoiceJSONArr);
|
|
|
|
}
|
2020-07-07 17:57:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-20 20:27:08 +00:00
|
|
|
ngAfterViewInit() {
|
|
|
|
if (this.invoiceJSONArr && this.invoiceJSONArr.length > 0 && this.sort && this.paginator) {
|
|
|
|
this.loadInvoicesTable(this.invoiceJSONArr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 17:57:15 +00:00
|
|
|
openCreateInvoiceModal() {
|
2021-12-29 23:08:41 +00:00
|
|
|
this.store.dispatch(openAlert({
|
|
|
|
payload: {
|
|
|
|
data: {
|
|
|
|
pageSize: this.pageSize,
|
|
|
|
component: ECLCreateInvoiceComponent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
2020-07-07 17:57:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 23:08:41 +00:00
|
|
|
onAddInvoice(form: any): boolean | void {
|
2021-08-28 21:03:18 +00:00
|
|
|
if (!this.description) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const expiryInSecs = (this.expiry ? this.expiry : 3600);
|
2020-07-07 17:57:15 +00:00
|
|
|
this.newlyAddedInvoiceMemo = 'ulbl' + Math.random().toString(36).slice(2) + Date.now();
|
|
|
|
this.newlyAddedInvoiceValue = this.invoiceValue;
|
2022-08-19 04:41:59 +00:00
|
|
|
let invoicePayload: any = null;
|
2020-07-07 17:57:15 +00:00
|
|
|
if (this.invoiceValue) {
|
2021-08-28 21:03:18 +00:00
|
|
|
invoicePayload = { description: this.description, expireIn: expiryInSecs, amountMsat: this.invoiceValue * 1000 };
|
2020-07-07 17:57:15 +00:00
|
|
|
} else {
|
|
|
|
invoicePayload = { description: this.description, expireIn: expiryInSecs };
|
|
|
|
}
|
2021-12-29 23:08:41 +00:00
|
|
|
this.store.dispatch(createInvoice({ payload: invoicePayload }));
|
2020-07-07 17:57:15 +00:00
|
|
|
this.resetData();
|
|
|
|
}
|
|
|
|
|
2021-08-28 21:03:18 +00:00
|
|
|
onInvoiceClick(selInvoice: Invoice) {
|
2021-12-29 23:08:41 +00:00
|
|
|
this.store.dispatch(openAlert({
|
|
|
|
payload: {
|
|
|
|
data: {
|
|
|
|
invoice: selInvoice,
|
|
|
|
newlyAdded: false,
|
|
|
|
component: ECLInvoiceInformationComponent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
2021-08-28 21:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onRefreshInvoice(selInvoice: Invoice) {
|
2022-08-19 04:41:59 +00:00
|
|
|
this.store.dispatch(invoiceLookup({ payload: selInvoice.paymentHash! }));
|
2021-08-28 21:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateInvoicesData(newInvoice: Invoice) {
|
2022-08-17 22:48:04 +00:00
|
|
|
this.invoiceJSONArr = this.invoiceJSONArr?.map((invoice) => ((invoice.paymentHash === newInvoice.paymentHash) ? newInvoice : invoice));
|
2020-07-07 17:57:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 19:02:31 +00:00
|
|
|
loadInvoicesTable(invs: Invoice[]) {
|
|
|
|
this.invoices = invs ? new MatTableDataSource<Invoice>([...invs]) : new MatTableDataSource<Invoice>([]);
|
2021-08-28 21:03:18 +00:00
|
|
|
this.invoices.sortingDataAccessor = (data: any, sortHeaderId: string) => ((data[sortHeaderId] && isNaN(data[sortHeaderId])) ? data[sortHeaderId].toLocaleLowerCase() : data[sortHeaderId] ? +data[sortHeaderId] : null);
|
2020-12-20 23:36:04 +00:00
|
|
|
this.invoices.sort = this.sort;
|
2021-06-20 20:27:08 +00:00
|
|
|
this.invoices.filterPredicate = (rowData: Invoice, fltr: string) => {
|
2022-08-19 04:41:59 +00:00
|
|
|
const newRowData = ((rowData.timestamp) ? this.datePipe.transform(new Date(rowData.timestamp * 1000), 'dd/MMM/YYYY HH:mm')?.toLowerCase() : '') + JSON.stringify(rowData).toLowerCase();
|
2021-08-28 21:03:18 +00:00
|
|
|
return newRowData.includes(fltr);
|
2021-06-20 20:27:08 +00:00
|
|
|
};
|
2020-12-20 23:36:04 +00:00
|
|
|
this.invoices.paginator = this.paginator;
|
2021-12-29 23:08:41 +00:00
|
|
|
this.applyFilter();
|
2020-12-20 23:36:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 17:57:15 +00:00
|
|
|
resetData() {
|
|
|
|
this.description = '';
|
2020-12-20 23:36:04 +00:00
|
|
|
this.invoiceValue = null;
|
|
|
|
this.expiry = null;
|
2020-07-07 17:57:15 +00:00
|
|
|
this.invoiceValueHint = '';
|
|
|
|
}
|
|
|
|
|
2021-12-29 23:08:41 +00:00
|
|
|
applyFilter() {
|
|
|
|
this.invoices.filter = this.selFilter.trim().toLowerCase();
|
2020-07-07 17:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onInvoiceValueChange() {
|
2022-08-19 04:41:59 +00:00
|
|
|
if (this.selNode && this.selNode.fiatConversion && this.invoiceValue && this.invoiceValue > 99) {
|
2020-07-07 17:57:15 +00:00
|
|
|
this.invoiceValueHint = '';
|
2022-08-18 20:33:41 +00:00
|
|
|
this.commonService.convertCurrency(this.invoiceValue, CurrencyUnitEnum.SATS, CurrencyUnitEnum.OTHER, (this.selNode.currencyUnits && this.selNode.currencyUnits.length > 2 ? this.selNode.currencyUnits[2] : ''), this.selNode.fiatConversion).
|
2021-12-29 23:08:41 +00:00
|
|
|
pipe(takeUntil(this.unSubs[4])).
|
|
|
|
subscribe({
|
|
|
|
next: (data) => {
|
|
|
|
this.invoiceValueHint = '= ' + data.symbol + this.decimalPipe.transform(data.OTHER, CURRENCY_UNIT_FORMATS.OTHER) + ' ' + data.unit;
|
|
|
|
}, error: (err) => {
|
|
|
|
this.invoiceValueHint = 'Conversion Error: ' + err;
|
|
|
|
}
|
|
|
|
});
|
2020-07-07 17:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onDownloadCSV() {
|
2021-08-28 21:03:18 +00:00
|
|
|
if (this.invoices.data && this.invoices.data.length > 0) {
|
2020-07-07 17:57:15 +00:00
|
|
|
this.commonService.downloadFile(this.invoices.data, 'Invoices');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
2021-08-28 21:03:18 +00:00
|
|
|
this.unSubs.forEach((completeSub) => {
|
2022-08-11 09:17:43 +00:00
|
|
|
completeSub.next(<any>null);
|
2020-07-07 17:57:15 +00:00
|
|
|
completeSub.complete();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|