calculation async

This commit is contained in:
Sebastian 2023-03-31 18:00:00 -03:00
parent 0f3b38745b
commit 8701ae100e
No known key found for this signature in database
GPG Key ID: 173909D1A5F66069

View File

@ -26,7 +26,7 @@ import {
useTranslationContext, useTranslationContext,
} from "@gnu-taler/web-util/lib/index.browser"; } from "@gnu-taler/web-util/lib/index.browser";
import { Fragment, h, VNode } from "preact"; import { Fragment, h, VNode } from "preact";
import { useEffect, useState } from "preact/hooks"; import { useEffect, useMemo, useState } from "preact/hooks";
import { Cashouts } from "../components/Cashouts/index.js"; import { Cashouts } from "../components/Cashouts/index.js";
import { useBackendContext } from "../context/backend.js"; import { useBackendContext } from "../context/backend.js";
import { ErrorMessage, usePageContext } from "../context/pageState.js"; import { ErrorMessage, usePageContext } from "../context/pageState.js";
@ -246,6 +246,8 @@ function CreateCashout({
? Amounts.sub(debitThreshold, balance).amount ? Amounts.sub(debitThreshold, balance).amount
: Amounts.add(balance, debitThreshold).amount; : Amounts.add(balance, debitThreshold).amount;
const zeroCalc = { debit: zero, credit: zero, beforeFee: zero };
const [calc, setCalc] = useState(zeroCalc);
const sellRate = config.ratios_and_fees.sell_at_ratio; const sellRate = config.ratios_and_fees.sell_at_ratio;
const sellFee = !config.ratios_and_fees.sell_out_fee const sellFee = !config.ratios_and_fees.sell_out_fee
? zero ? zero
@ -256,11 +258,21 @@ function CreateCashout({
const amount = Amounts.parse(`${balance.currency}:${form.amount}`); const amount = Amounts.parse(`${balance.currency}:${form.amount}`);
const calc = !amount useEffect(() => {
? { debit: zero, credit: zero, beforeFee: zero } if (!amount) {
: !form.isDebit setCalc(zeroCalc);
? calculateFromCredit(amount, sellFee, sellRate) } else {
: calculateFromDebit(amount, sellFee, sellRate); if (form.isDebit) {
calculateFromDebit(amount, sellFee, sellRate).then((r) => {
setCalc(r);
});
} else {
calculateFromCredit(amount, sellFee, sellRate).then((r) => {
setCalc(r);
});
}
}
}, [form.amount, form.isDebit]);
const balanceAfter = Amounts.sub(balance, calc.debit).amount; const balanceAfter = Amounts.sub(balance, calc.debit).amount;
@ -836,11 +848,11 @@ type TransferCalculation = {
beforeFee: AmountJson; beforeFee: AmountJson;
}; };
function calculateFromDebit( async function calculateFromDebit(
amount: AmountJson, amount: AmountJson,
sellFee: AmountJson, sellFee: AmountJson,
sellRate: number, sellRate: number,
): TransferCalculation { ): Promise<TransferCalculation> {
const debit = amount; const debit = amount;
const beforeFee = truncate(Amounts.divide(debit, 1 / sellRate)); const beforeFee = truncate(Amounts.divide(debit, 1 / sellRate));
@ -849,11 +861,11 @@ function calculateFromDebit(
return { debit, credit, beforeFee }; return { debit, credit, beforeFee };
} }
function calculateFromCredit( async function calculateFromCredit(
amount: AmountJson, amount: AmountJson,
sellFee: AmountJson, sellFee: AmountJson,
sellRate: number, sellRate: number,
): TransferCalculation { ): Promise<TransferCalculation> {
const credit = amount; const credit = amount;
const beforeFee = Amounts.add(credit, sellFee).amount; const beforeFee = Amounts.add(credit, sellFee).amount;