2
0
mirror of https://github.com/Ride-The-Lightning/RTL synced 2024-10-31 09:20:27 +00:00
RTL/src/app/eclair/transactions/create-invoice-modal/create-invoice.component.ts

129 lines
5.2 KiB
TypeScript
Raw Normal View History

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';
import { TimeUnitEnum, CurrencyUnitEnum, TIME_UNITS, CURRENCY_UNIT_FORMATS, PAGE_SIZE, APICallStatusEnum, ECLActions, DEFAULT_INVOICE_EXPIRY } from '../../../shared/services/consts-enums-functions';
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';
@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
public faExclamationTriangle = faExclamationTriangle;
public selNode: SelNodeChild | null = {};
public description = '';
public expiry: number | null;
public invoiceValue: number | null = null;
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) { }
ngOnInit() {
this.pageSize = this.data.pageSize;
2022-10-20 17:14:08 +00:00
this.store.select(eclNodeSettings).pipe(takeUntil(this.unSubs[0])).
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
}
});
}
2021-12-29 23:08:41 +00:00
onAddInvoice(form: any): boolean | void {
this.invoiceError = '';
2021-08-28 21:03:18 +00:00
if (!this.description) {
return true;
}
let expiryInSecs = (this.expiry ? this.expiry : DEFAULT_INVOICE_EXPIRY);
if (this.expiry && this.selTimeUnit !== TimeUnitEnum.SECS) {
expiryInSecs = this.commonService.convertTime(this.expiry, this.selTimeUnit, TimeUnitEnum.SECS);
}
let invoicePayload: any = null;
if (this.invoiceValue) {
2021-08-28 21:03:18 +00:00
invoicePayload = { description: this.description, expireIn: expiryInSecs, amountMsat: this.invoiceValue * 1000 };
} else {
invoicePayload = { description: this.description, expireIn: expiryInSecs };
}
2021-12-29 23:08:41 +00:00
this.store.dispatch(createInvoice({ payload: invoicePayload }));
}
resetData() {
this.description = '';
this.invoiceValue = null;
this.private = false;
2021-08-28 21:03:18 +00:00
this.expiry = null;
this.invoiceValueHint = '';
this.selTimeUnit = TimeUnitEnum.SECS;
this.invoiceError = '';
}
onInvoiceValueChange() {
if (this.selNode && this.selNode.fiatConversion && this.invoiceValue && this.invoiceValue > 99) {
this.invoiceValueHint = '';
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;
}
});
}
}
onTimeUnitChange(event: any) {
2021-08-28 21:03:18 +00:00
if (this.expiry && this.selTimeUnit !== event.value) {
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) => {
completeSub.next(<any>null);
completeSub.complete();
});
}
}