summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-03-31 18:00:00 -0300
committerSebastian <sebasjm@gmail.com>2023-03-31 18:00:00 -0300
commit8701ae100ec1e2733b8e9b7006a706d1c9fe32a8 (patch)
treef1a0eb72d59f6cf5d357065bad9d5718d0533932
parent0f3b38745b7214abbbe638410a74fe5405ca24bb (diff)
downloadwallet-core-8701ae100ec1e2733b8e9b7006a706d1c9fe32a8.tar.gz
wallet-core-8701ae100ec1e2733b8e9b7006a706d1c9fe32a8.tar.bz2
wallet-core-8701ae100ec1e2733b8e9b7006a706d1c9fe32a8.zip
calculation async
-rw-r--r--packages/demobank-ui/src/pages/BusinessAccount.tsx32
1 files changed, 22 insertions, 10 deletions
diff --git a/packages/demobank-ui/src/pages/BusinessAccount.tsx b/packages/demobank-ui/src/pages/BusinessAccount.tsx
index 258802e58..4c322764e 100644
--- a/packages/demobank-ui/src/pages/BusinessAccount.tsx
+++ b/packages/demobank-ui/src/pages/BusinessAccount.tsx
@@ -26,7 +26,7 @@ import {
useTranslationContext,
} from "@gnu-taler/web-util/lib/index.browser";
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 { useBackendContext } from "../context/backend.js";
import { ErrorMessage, usePageContext } from "../context/pageState.js";
@@ -246,6 +246,8 @@ function CreateCashout({
? Amounts.sub(debitThreshold, balance).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 sellFee = !config.ratios_and_fees.sell_out_fee
? zero
@@ -256,11 +258,21 @@ function CreateCashout({
const amount = Amounts.parse(`${balance.currency}:${form.amount}`);
- const calc = !amount
- ? { debit: zero, credit: zero, beforeFee: zero }
- : !form.isDebit
- ? calculateFromCredit(amount, sellFee, sellRate)
- : calculateFromDebit(amount, sellFee, sellRate);
+ useEffect(() => {
+ if (!amount) {
+ setCalc(zeroCalc);
+ } else {
+ 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;
@@ -836,11 +848,11 @@ type TransferCalculation = {
beforeFee: AmountJson;
};
-function calculateFromDebit(
+async function calculateFromDebit(
amount: AmountJson,
sellFee: AmountJson,
sellRate: number,
-): TransferCalculation {
+): Promise<TransferCalculation> {
const debit = amount;
const beforeFee = truncate(Amounts.divide(debit, 1 / sellRate));
@@ -849,11 +861,11 @@ function calculateFromDebit(
return { debit, credit, beforeFee };
}
-function calculateFromCredit(
+async function calculateFromCredit(
amount: AmountJson,
sellFee: AmountJson,
sellRate: number,
-): TransferCalculation {
+): Promise<TransferCalculation> {
const credit = amount;
const beforeFee = Amounts.add(credit, sellFee).amount;