2020-07-07 17:57:15 +00:00
|
|
|
import { Component, OnInit, OnDestroy, Inject } from '@angular/core';
|
|
|
|
import { DecimalPipe } from '@angular/common';
|
|
|
|
import { Subject } from 'rxjs';
|
|
|
|
import { filter, takeUntil } from 'rxjs/operators';
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
import { Actions } from '@ngrx/effects';
|
|
|
|
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
|
|
|
|
import { faExclamationTriangle } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
|
|
|
|
import { ECLInvoiceInformation } from '../../../shared/models/alertData';
|
2023-02-18 02:25:26 +00:00
|
|
|
import { TimeUnitEnum, CurrencyUnitEnum, TIME_UNITS, CURRENCY_UNIT_FORMATS, PAGE_SIZE, APICallStatusEnum, ECLActions, DEFAULT_INVOICE_EXPIRY } from '../../../shared/services/consts-enums-functions';
|
2020-07-07 17:57:15 +00:00
|
|
|
import { SelNodeChild } from '../../../shared/models/RTLconfig';
|
|
|
|
import { GetInfo } from '../../../shared/models/eclModels';
|
|
|
|
import { CommonService } from '../../../shared/services/common.service';
|
|
|
|
|
2021-12-29 23:08:41 +00:00
|
|
|
import { RTLState } from '../../../store/rtl.state';
|
|
|
|
import { createInvoice } from '../../store/ecl.actions';
|
2022-10-20 17:14:08 +00:00
|
|
|
import { eclNodeInformation, eclNodeSettings } from '../../store/ecl.selector';
|
2020-07-07 17:57:15 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'rtl-ecl-create-invoices',
|
|
|
|
templateUrl: './create-invoice.component.html',
|
|
|
|
styleUrls: ['./create-invoice.component.scss']
|
|
|
|
})
|
|
|
|
export class ECLCreateInvoiceComponent implements OnInit, OnDestroy {
|
2021-08-28 21:03:18 +00:00
|
|
|
|
2020-07-07 17:57:15 +00:00
|
|
|
public faExclamationTriangle = faExclamationTriangle;
|
2022-08-19 04:41:59 +00:00
|
|
|
public selNode: SelNodeChild | null = {};
|
2020-07-07 17:57:15 +00:00
|
|
|
public description = '';
|
2022-08-19 04:41:59 +00:00
|
|
|
public expiry: number | null;
|
|
|
|
public invoiceValue: number | null = null;
|
2020-07-07 17:57:15 +00:00
|
|
|
public invoiceValueHint = '';
|
|
|
|
public invoicePaymentReq = '';
|
|
|
|
public information: GetInfo = {};
|
|
|
|
public private = false;
|
|
|
|
public expiryStep = 100;
|
|
|
|
public pageSize = PAGE_SIZE;
|
|
|
|
public timeUnitEnum = TimeUnitEnum;
|
|
|
|
public timeUnits = TIME_UNITS;
|
|
|
|
public selTimeUnit = TimeUnitEnum.SECS;
|
|
|
|
public invoiceError = '';
|
|
|
|
private unSubs: Array<Subject<void>> = [new Subject(), new Subject(), new Subject(), new Subject(), new Subject()];
|
|
|
|
|
2021-12-29 23:08:41 +00:00
|
|
|
constructor(public dialogRef: MatDialogRef<ECLCreateInvoiceComponent>, @Inject(MAT_DIALOG_DATA) public data: ECLInvoiceInformation, private store: Store<RTLState>, private decimalPipe: DecimalPipe, private commonService: CommonService, private actions: Actions) { }
|
2020-07-07 17:57:15 +00:00
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.pageSize = this.data.pageSize;
|
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;
|
2021-08-28 21:03:18 +00:00
|
|
|
});
|
|
|
|
this.actions.pipe(
|
2021-12-29 23:08:41 +00:00
|
|
|
takeUntil(this.unSubs[2]),
|
|
|
|
filter((action) => action.type === ECLActions.UPDATE_API_CALL_STATUS_ECL)).
|
|
|
|
subscribe((action: any) => {
|
|
|
|
if (action.type === ECLActions.UPDATE_API_CALL_STATUS_ECL && action.payload.action === 'CreateInvoice') {
|
|
|
|
if (action.payload.status === APICallStatusEnum.ERROR) {
|
|
|
|
this.invoiceError = action.payload.message;
|
|
|
|
}
|
|
|
|
if (action.payload.status === APICallStatusEnum.COMPLETED) {
|
|
|
|
this.dialogRef.close();
|
|
|
|
}
|
2021-08-28 21:03:18 +00:00
|
|
|
}
|
|
|
|
});
|
2020-07-07 17:57:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 23:08:41 +00:00
|
|
|
onAddInvoice(form: any): boolean | void {
|
2020-07-07 17:57:15 +00:00
|
|
|
this.invoiceError = '';
|
2021-08-28 21:03:18 +00:00
|
|
|
if (!this.description) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-02-18 02:25:26 +00:00
|
|
|
let expiryInSecs = (this.expiry ? this.expiry : DEFAULT_INVOICE_EXPIRY);
|
2022-08-19 04:41:59 +00:00
|
|
|
if (this.expiry && this.selTimeUnit !== TimeUnitEnum.SECS) {
|
2020-07-07 17:57:15 +00:00
|
|
|
expiryInSecs = this.commonService.convertTime(this.expiry, this.selTimeUnit, TimeUnitEnum.SECS);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
resetData() {
|
|
|
|
this.description = '';
|
2020-12-20 23:36:04 +00:00
|
|
|
this.invoiceValue = null;
|
2020-07-07 17:57:15 +00:00
|
|
|
this.private = false;
|
2021-08-28 21:03:18 +00:00
|
|
|
this.expiry = null;
|
2020-07-07 17:57:15 +00:00
|
|
|
this.invoiceValueHint = '';
|
|
|
|
this.selTimeUnit = TimeUnitEnum.SECS;
|
|
|
|
this.invoiceError = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
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[3])).
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onTimeUnitChange(event: any) {
|
2021-08-28 21:03:18 +00:00
|
|
|
if (this.expiry && this.selTimeUnit !== event.value) {
|
2020-07-07 17:57:15 +00:00
|
|
|
this.expiry = this.commonService.convertTime(this.expiry, this.selTimeUnit, event.value);
|
|
|
|
}
|
|
|
|
this.selTimeUnit = event.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|