From 88618df7b870732f4f29a80686dd4f4cf20887f8 Mon Sep 17 00:00:00 2001
From: Sebastian
Date: Tue, 22 Nov 2022 15:43:39 -0300
Subject: [PATCH] amount field
---
.../src/components/AmountField.stories.tsx | 65 ++++
.../src/components/AmountField.tsx | 185 ++++++++++--
.../src/components/TransactionItem.tsx | 6 +-
.../src/components/index.stories.tsx | 3 +-
.../src/mui/TextField.tsx | 1 +
.../src/mui/handlers.ts | 7 +
.../wallet/CreateManualWithdraw.stories.tsx | 58 ----
.../src/wallet/CreateManualWithdraw.test.ts | 232 --------------
.../src/wallet/CreateManualWithdraw.tsx | 282 ------------------
.../src/wallet/DepositPage/index.ts | 3 +-
.../src/wallet/DepositPage/state.ts | 55 ++--
.../src/wallet/DepositPage/stories.tsx | 6 +-
.../src/wallet/DepositPage/test.ts | 14 +-
.../src/wallet/DepositPage/views.tsx | 7 +-
.../src/wallet/DestinationSelection.tsx | 33 +-
.../src/wallet/ExchangeSelection/state.ts | 13 +-
.../src/wallet/ManualWithdrawPage.tsx | 141 ---------
.../src/wallet/index.stories.tsx | 2 -
18 files changed, 298 insertions(+), 815 deletions(-)
create mode 100644 packages/taler-wallet-webextension/src/components/AmountField.stories.tsx
delete mode 100644 packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.stories.tsx
delete mode 100644 packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.test.ts
delete mode 100644 packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.tsx
delete mode 100644 packages/taler-wallet-webextension/src/wallet/ManualWithdrawPage.tsx
diff --git a/packages/taler-wallet-webextension/src/components/AmountField.stories.tsx b/packages/taler-wallet-webextension/src/components/AmountField.stories.tsx
new file mode 100644
index 000000000..3183364a8
--- /dev/null
+++ b/packages/taler-wallet-webextension/src/components/AmountField.stories.tsx
@@ -0,0 +1,65 @@
+/*
+ This file is part of GNU Taler
+ (C) 2022 Taler Systems S.A.
+
+ GNU Taler is free software; you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with
+ GNU Taler; see the file COPYING. If not, see
+ */
+
+/**
+ *
+ * @author Sebastian Javier Marchano (sebasjm)
+ */
+
+import { AmountJson, Amounts } from "@gnu-taler/taler-util";
+import { styled } from "@linaria/react";
+import { Fragment, h, VNode } from "preact";
+import { useState } from "preact/hooks";
+import { useTranslationContext } from "../context/translation.js";
+import { Grid } from "../mui/Grid.js";
+import { AmountFieldHandler, TextFieldHandler } from "../mui/handlers.js";
+import { AmountField } from "./AmountField.js";
+
+export default {
+ title: "components/amountField",
+};
+
+function RenderAmount(): VNode {
+ const [value, setValue] = useState(undefined);
+
+ const error = value === undefined ? undefined : undefined;
+
+ const handler: AmountFieldHandler = {
+ value: value ?? Amounts.zeroOfCurrency("USD"),
+ onInput: async (e) => {
+ setValue(e);
+ },
+ error,
+ };
+ const { i18n } = useTranslationContext();
+ return (
+
+ Amount}
+ currency="USD"
+ highestDenom={2000000}
+ lowestDenom={0.01}
+ handler={handler}
+ />
+
+
{JSON.stringify(value, undefined, 2)}
+
+
+ );
+}
+
+export const AmountFieldExample = (): VNode => RenderAmount();
diff --git a/packages/taler-wallet-webextension/src/components/AmountField.tsx b/packages/taler-wallet-webextension/src/components/AmountField.tsx
index 1c57be0df..6081e70ff 100644
--- a/packages/taler-wallet-webextension/src/components/AmountField.tsx
+++ b/packages/taler-wallet-webextension/src/components/AmountField.tsx
@@ -14,51 +14,182 @@
GNU Taler; see the file COPYING. If not, see
*/
+import {
+ amountFractionalBase,
+ amountFractionalLength,
+ AmountJson,
+ amountMaxValue,
+ Amounts,
+ Result,
+} from "@gnu-taler/taler-util";
import { Fragment, h, VNode } from "preact";
-import { TextFieldHandler } from "../mui/handlers.js";
+import { useState } from "preact/hooks";
+import { AmountFieldHandler } from "../mui/handlers.js";
import { TextField } from "../mui/TextField.js";
-import { ErrorText } from "./styled/index.js";
+
+const HIGH_DENOM_SYMBOL = ["", "K", "M", "G", "T", "P"];
+const LOW_DENOM_SYMBOL = ["", "m", "mm", "n", "p", "f"];
export function AmountField({
label,
handler,
- currency,
+ lowestDenom = 1,
+ highestDenom = 1,
required,
}: {
label: VNode;
+ lowestDenom?: number;
+ highestDenom?: number;
required?: boolean;
- currency: string;
- handler: TextFieldHandler;
+ handler: AmountFieldHandler;
}): VNode {
+ const [unit, setUnit] = useState(1);
+ const [dotAtTheEnd, setDotAtTheEnd] = useState(false);
+ const currency = handler.value.currency;
+
+ let hd = Math.floor(Math.log10(highestDenom || 1) / 3);
+ let ld = Math.ceil((-1 * Math.log10(lowestDenom || 1)) / 3);
+
+ const currencyLabels: Array<{ name: string; unit: number }> = [
+ {
+ name: currency,
+ unit: 1,
+ },
+ ];
+
+ while (hd > 0) {
+ currencyLabels.push({
+ name: `${HIGH_DENOM_SYMBOL[hd]}${currency}`,
+ unit: Math.pow(10, hd * 3),
+ });
+ hd--;
+ }
+ while (ld > 0) {
+ currencyLabels.push({
+ name: `${LOW_DENOM_SYMBOL[ld]}${currency}`,
+ unit: Math.pow(10, -1 * ld * 3),
+ });
+ ld--;
+ }
+
+ const prev = Amounts.stringifyValue(handler.value);
+
function positiveAmount(value: string): string {
- if (!value) return "";
- try {
- const num = Number.parseFloat(value);
- if (Number.isNaN(num) || num < 0) return handler.value;
+ setDotAtTheEnd(value.endsWith("."));
+ if (!value) {
if (handler.onInput) {
- handler.onInput(value);
+ handler.onInput(Amounts.zeroOfCurrency(currency));
}
- return value;
+ return "";
+ }
+ try {
+ //remove all but last dot
+ const parsed = value.replace(/(\.)(?=.*\1)/g, "");
+ const real = parseValue(currency, parsed);
+
+ if (!real || real.value < 0) {
+ return prev;
+ }
+
+ const normal = normalize(real, unit);
+
+ console.log(real, unit, normal);
+ if (normal && handler.onInput) {
+ handler.onInput(normal);
+ }
+ return parsed;
} catch (e) {
// do nothing
}
- return handler.value;
+ return prev;
}
+
+ const normal = denormalize(handler.value, unit) ?? handler.value;
+
+ const textValue = Amounts.stringifyValue(normal) + (dotAtTheEnd ? "." : "");
return (
- {currency}
- }
- value={handler.value}
- disabled={!handler.onInput}
- onInput={positiveAmount}
- />
+
+
+ {currency}
+
+ ) : (
+
+ )
+ }
+ value={textValue}
+ disabled={!handler.onInput}
+ onInput={positiveAmount}
+ />
+
);
}
+
+function parseValue(currency: string, s: string): AmountJson | undefined {
+ const [intPart, fractPart] = s.split(".");
+ const tail = "." + (fractPart || "0");
+ if (tail.length > amountFractionalLength + 1) {
+ return undefined;
+ }
+ const value = Number.parseInt(intPart, 10);
+ if (Number.isNaN(value) || value > amountMaxValue) {
+ return undefined;
+ }
+ return {
+ currency,
+ fraction: Math.round(amountFractionalBase * Number.parseFloat(tail)),
+ value,
+ };
+}
+
+function normalize(amount: AmountJson, unit: number): AmountJson | undefined {
+ if (unit === 1 || Amounts.isZero(amount)) return amount;
+ const result =
+ unit < 1
+ ? Amounts.divide(amount, 1 / unit)
+ : Amounts.mult(amount, unit).amount;
+ return result;
+}
+
+function denormalize(amount: AmountJson, unit: number): AmountJson | undefined {
+ if (unit === 1 || Amounts.isZero(amount)) return amount;
+ const result =
+ unit < 1
+ ? Amounts.mult(amount, 1 / unit).amount
+ : Amounts.divide(amount, unit);
+ return result;
+}
diff --git a/packages/taler-wallet-webextension/src/components/TransactionItem.tsx b/packages/taler-wallet-webextension/src/components/TransactionItem.tsx
index e5ce4140f..f8b23081d 100644
--- a/packages/taler-wallet-webextension/src/components/TransactionItem.tsx
+++ b/packages/taler-wallet-webextension/src/components/TransactionItem.tsx
@@ -57,9 +57,9 @@ export function TransactionItem(props: { tx: Transaction }): VNode {
? !tx.withdrawalDetails.confirmed
? i18n.str`Need approval in the Bank`
: i18n.str`Exchange is waiting the wire transfer`
- : undefined
- : tx.withdrawalDetails.type === WithdrawalType.ManualTransfer
- ? i18n.str`Exchange is waiting the wire transfer`
+ : tx.withdrawalDetails.type === WithdrawalType.ManualTransfer
+ ? i18n.str`Exchange is waiting the wire transfer`
+ : "" //pending but no message
: undefined
}
/>
diff --git a/packages/taler-wallet-webextension/src/components/index.stories.tsx b/packages/taler-wallet-webextension/src/components/index.stories.tsx
index d71adf689..2e4e7fa2e 100644
--- a/packages/taler-wallet-webextension/src/components/index.stories.tsx
+++ b/packages/taler-wallet-webextension/src/components/index.stories.tsx
@@ -25,5 +25,6 @@ import * as a3 from "./Amount.stories.js";
import * as a4 from "./ShowFullContractTermPopup.stories.js";
import * as a5 from "./TermsOfService/stories.js";
import * as a6 from "./QR.stories";
+import * as a7 from "./AmountField.stories.js";
-export default [a1, a2, a3, a4, a5, a6];
+export default [a1, a2, a3, a4, a5, a6, a7];
diff --git a/packages/taler-wallet-webextension/src/mui/TextField.tsx b/packages/taler-wallet-webextension/src/mui/TextField.tsx
index ba05158fa..42ac49a00 100644
--- a/packages/taler-wallet-webextension/src/mui/TextField.tsx
+++ b/packages/taler-wallet-webextension/src/mui/TextField.tsx
@@ -41,6 +41,7 @@ export interface Props {
multiline?: boolean;
onChange?: (s: string) => void;
onInput?: (s: string) => string;
+ inputmode?: string;
min?: string;
step?: string;
placeholder?: string;
diff --git a/packages/taler-wallet-webextension/src/mui/handlers.ts b/packages/taler-wallet-webextension/src/mui/handlers.ts
index 9d393e5b7..655fceef9 100644
--- a/packages/taler-wallet-webextension/src/mui/handlers.ts
+++ b/packages/taler-wallet-webextension/src/mui/handlers.ts
@@ -13,6 +13,7 @@
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see
*/
+import { AmountJson } from "@gnu-taler/taler-util";
import { TalerError } from "@gnu-taler/taler-wallet-core";
export interface TextFieldHandler {
@@ -21,6 +22,12 @@ export interface TextFieldHandler {
error?: string;
}
+export interface AmountFieldHandler {
+ onInput?: (value: AmountJson) => Promise;
+ value: AmountJson;
+ error?: string;
+}
+
export interface ButtonHandler {
onClick?: () => Promise;
error?: TalerError;
diff --git a/packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.stories.tsx b/packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.stories.tsx
deleted file mode 100644
index 2154d35de..000000000
--- a/packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.stories.tsx
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- This file is part of GNU Taler
- (C) 2022 Taler Systems S.A.
-
- GNU Taler is free software; you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation; either version 3, or (at your option) any later version.
-
- GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License along with
- GNU Taler; see the file COPYING. If not, see
- */
-
-/**
- *
- * @author Sebastian Javier Marchano (sebasjm)
- */
-
-import { createExample } from "../test-utils.js";
-import { CreateManualWithdraw as TestedComponent } from "./CreateManualWithdraw.js";
-
-export default {
- title: "wallet/manual withdraw/creation",
- component: TestedComponent,
- argTypes: {},
-};
-
-// ,
-const exchangeUrlWithCurrency = {
- "http://exchange.taler:8081": "COL",
- "http://exchange.tal": "EUR",
-};
-
-export const WithoutAnyExchangeKnown = createExample(TestedComponent, {
- exchangeUrlWithCurrency: {},
-});
-
-export const InitialState = createExample(TestedComponent, {
- exchangeUrlWithCurrency,
-});
-
-export const WithAmountInitialized = createExample(TestedComponent, {
- initialAmount: "10",
- exchangeUrlWithCurrency,
-});
-
-export const WithExchangeError = createExample(TestedComponent, {
- error: "The exchange url seems invalid",
- exchangeUrlWithCurrency,
-});
-
-export const WithAmountError = createExample(TestedComponent, {
- initialAmount: "e",
- exchangeUrlWithCurrency,
-});
diff --git a/packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.test.ts b/packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.test.ts
deleted file mode 100644
index 37c50285b..000000000
--- a/packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.test.ts
+++ /dev/null
@@ -1,232 +0,0 @@
-/*
- This file is part of GNU Taler
- (C) 2022 Taler Systems S.A.
-
- GNU Taler is free software; you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation; either version 3, or (at your option) any later version.
-
- GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License along with
- GNU Taler; see the file COPYING. If not, see
- */
-
-/**
- *
- * @author Sebastian Javier Marchano (sebasjm)
- */
-
-import { expect } from "chai";
-import { SelectFieldHandler, TextFieldHandler } from "../mui/handlers.js";
-import { mountHook } from "../test-utils.js";
-import { useComponentState } from "./CreateManualWithdraw.js";
-
-const exchangeListWithARSandUSD = {
- url1: "USD",
- url2: "ARS",
- url3: "ARS",
-};
-
-const exchangeListEmpty = {};
-
-describe("CreateManualWithdraw states", () => {
- it("should set noExchangeFound when exchange list is empty", () => {
- const { pullLastResultOrThrow } = mountHook(() =>
- useComponentState(exchangeListEmpty, undefined, undefined),
- );
-
- const { noExchangeFound } = pullLastResultOrThrow();
-
- expect(noExchangeFound).equal(true);
- });
-
- it("should set noExchangeFound when exchange list doesn't include selected currency", () => {
- const { pullLastResultOrThrow } = mountHook(() =>
- useComponentState(exchangeListWithARSandUSD, undefined, "COL"),
- );
-
- const { noExchangeFound } = pullLastResultOrThrow();
-
- expect(noExchangeFound).equal(true);
- });
-
- it("should select the first exchange from the list", () => {
- const { pullLastResultOrThrow } = mountHook(() =>
- useComponentState(exchangeListWithARSandUSD, undefined, undefined),
- );
-
- const { exchange } = pullLastResultOrThrow();
-
- expect(exchange.value).equal("url1");
- });
-
- it("should select the first exchange with the selected currency", () => {
- const { pullLastResultOrThrow } = mountHook(() =>
- useComponentState(exchangeListWithARSandUSD, undefined, "ARS"),
- );
-
- const { exchange } = pullLastResultOrThrow();
-
- expect(exchange.value).equal("url2");
- });
-
- it("should change the exchange when currency change", async () => {
- const { pullLastResultOrThrow, waitForStateUpdate } = mountHook(() =>
- useComponentState(exchangeListWithARSandUSD, undefined, "ARS"),
- );
-
- {
- const { exchange, currency } = pullLastResultOrThrow();
-
- expect(exchange.value).equal("url2");
- if (currency.onChange === undefined) expect.fail();
- currency.onChange("USD");
- }
-
- expect(await waitForStateUpdate()).true;
-
- {
- const { exchange } = pullLastResultOrThrow();
- expect(exchange.value).equal("url1");
- }
- });
-
- it("should change the currency when exchange change", async () => {
- const { pullLastResultOrThrow, waitForStateUpdate } = mountHook(() =>
- useComponentState(exchangeListWithARSandUSD, undefined, "ARS"),
- );
-
- {
- const { exchange, currency } = pullLastResultOrThrow();
-
- expect(exchange.value).equal("url2");
- expect(currency.value).equal("ARS");
-
- if (exchange.onChange === undefined) expect.fail();
- exchange.onChange("url1");
- }
-
- expect(await waitForStateUpdate()).true;
-
- {
- const { exchange, currency } = pullLastResultOrThrow();
-
- expect(exchange.value).equal("url1");
- expect(currency.value).equal("USD");
- }
- });
-
- it("should update parsed amount when amount change", async () => {
- const { pullLastResultOrThrow, waitForStateUpdate } = mountHook(() =>
- useComponentState(exchangeListWithARSandUSD, undefined, "ARS"),
- );
-
- {
- const { amount, parsedAmount } = pullLastResultOrThrow();
-
- expect(parsedAmount).equal(undefined);
-
- expect(amount.onInput).not.undefined;
- if (!amount.onInput) return;
- amount.onInput("12");
- }
-
- expect(await waitForStateUpdate()).true;
-
- {
- const { parsedAmount } = pullLastResultOrThrow();
-
- expect(parsedAmount).deep.equals({
- value: 12,
- fraction: 0,
- currency: "ARS",
- });
- }
- });
-
- it("should have an amount field", async () => {
- const { pullLastResultOrThrow, waitForStateUpdate } = mountHook(() =>
- useComponentState(exchangeListWithARSandUSD, undefined, "ARS"),
- );
-
- await defaultTestForInputText(
- waitForStateUpdate,
- () => pullLastResultOrThrow().amount,
- );
- });
-
- it("should have an exchange selector ", async () => {
- const { pullLastResultOrThrow, waitForStateUpdate } = mountHook(() =>
- useComponentState(exchangeListWithARSandUSD, undefined, "ARS"),
- );
-
- await defaultTestForInputSelect(
- waitForStateUpdate,
- () => pullLastResultOrThrow().exchange,
- );
- });
-
- it("should have a currency selector ", async () => {
- const { pullLastResultOrThrow, waitForStateUpdate } = mountHook(() =>
- useComponentState(exchangeListWithARSandUSD, undefined, "ARS"),
- );
-
- await defaultTestForInputSelect(
- waitForStateUpdate,
- () => pullLastResultOrThrow().currency,
- );
- });
-});
-
-async function defaultTestForInputText(
- awaiter: () => Promise,
- getField: () => TextFieldHandler,
-): Promise {
- let nextValue = "";
- {
- const field = getField();
- const initialValue = field.value;
- nextValue = `${initialValue} something else`;
- expect(field.onInput).not.undefined;
- if (!field.onInput) return;
- field.onInput(nextValue);
- }
-
- expect(await awaiter()).true;
-
- {
- const field = getField();
- expect(field.value).equal(nextValue);
- }
-}
-
-async function defaultTestForInputSelect(
- awaiter: () => Promise,
- getField: () => SelectFieldHandler,
-): Promise {
- let nextValue = "";
-
- {
- const field = getField();
- const initialValue = field.value;
- const keys = Object.keys(field.list);
- const nextIdx = keys.indexOf(initialValue) + 1;
- if (keys.length < nextIdx) {
- throw new Error("no enough values");
- }
- nextValue = keys[nextIdx];
- if (field.onChange === undefined) expect.fail();
- field.onChange(nextValue);
- }
-
- expect(await awaiter()).true;
-
- {
- const field = getField();
-
- expect(field.value).equal(nextValue);
- }
-}
diff --git a/packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.tsx b/packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.tsx
deleted file mode 100644
index dd80faccd..000000000
--- a/packages/taler-wallet-webextension/src/wallet/CreateManualWithdraw.tsx
+++ /dev/null
@@ -1,282 +0,0 @@
-/*
- This file is part of GNU Taler
- (C) 2022 Taler Systems S.A.
-
- GNU Taler is free software; you can redistribute it and/or modify it under the
- terms of the GNU General Public License as published by the Free Software
- Foundation; either version 3, or (at your option) any later version.
-
- GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License along with
- GNU Taler; see the file COPYING. If not, see
- */
-
-/**
- *
- * @author Sebastian Javier Marchano (sebasjm)
- */
-
-import { AmountJson, Amounts } from "@gnu-taler/taler-util";
-import { Fragment, h, VNode } from "preact";
-import { useState } from "preact/hooks";
-import { ErrorMessage } from "../components/ErrorMessage.js";
-import { SelectList } from "../components/SelectList.js";
-import {
- BoldLight,
- Centered,
- Input,
- InputWithLabel,
- LightText,
- LinkPrimary,
- SubTitle,
-} from "../components/styled/index.js";
-import { useTranslationContext } from "../context/translation.js";
-import { Button } from "../mui/Button.js";
-import { SelectFieldHandler, TextFieldHandler } from "../mui/handlers.js";
-import { Pages } from "../NavigationBar.js";
-
-export interface Props {
- error: string | undefined;
- initialAmount?: string;
- exchangeUrlWithCurrency: Record;
- onCreate: (exchangeBaseUrl: string, amount: AmountJson) => Promise;
- initialCurrency?: string;
-}
-
-export interface State {
- noExchangeFound: boolean;
- parsedAmount: AmountJson | undefined;
- amount: TextFieldHandler;
- currency: SelectFieldHandler;
- exchange: SelectFieldHandler;
-}
-
-export function useComponentState(
- exchangeUrlWithCurrency: Record,
- initialAmount: string | undefined,
- initialCurrency: string | undefined,
-): State {
- const exchangeSelectList = Object.keys(exchangeUrlWithCurrency);
- const currencySelectList = Object.values(exchangeUrlWithCurrency);
- const exchangeMap = exchangeSelectList.reduce(
- (p, c) => ({ ...p, [c]: `${c} (${exchangeUrlWithCurrency[c]})` }),
- {} as Record,
- );
- const currencyMap = currencySelectList.reduce(
- (p, c) => ({ ...p, [c]: c }),
- {} as Record,
- );
-
- const foundExchangeForCurrency = exchangeSelectList.findIndex(
- (e) => exchangeUrlWithCurrency[e] === initialCurrency,
- );
-
- const initialExchange =
- foundExchangeForCurrency !== -1
- ? exchangeSelectList[foundExchangeForCurrency]
- : !initialCurrency && exchangeSelectList.length > 0
- ? exchangeSelectList[0]
- : undefined;
-
- const [exchange, setExchange] = useState(initialExchange || "");
- const [currency, setCurrency] = useState(
- initialExchange ? exchangeUrlWithCurrency[initialExchange] : "",
- );
-
- const [amount, setAmount] = useState(initialAmount || "");
- const parsedAmount = Amounts.parse(`${currency}:${amount}`);
-
- async function changeExchange(exchange: string): Promise {
- setExchange(exchange);
- setCurrency(exchangeUrlWithCurrency[exchange]);
- }
-
- async function changeCurrency(currency: string): Promise {
- setCurrency(currency);
- const found = Object.entries(exchangeUrlWithCurrency).find(
- (e) => e[1] === currency,
- );
-
- if (found) {
- setExchange(found[0]);
- } else {
- setExchange("");
- }
- }
- return {
- noExchangeFound: initialExchange === undefined,
- currency: {
- list: currencyMap,
- value: currency,
- onChange: changeCurrency,
- },
- exchange: {
- list: exchangeMap,
- value: exchange,
- onChange: changeExchange,
- },
- amount: {
- value: amount,
- onInput: async (e: string) => setAmount(e),
- },
- parsedAmount,
- };
-}
-
-export function CreateManualWithdraw({
- initialAmount,
- exchangeUrlWithCurrency,
- error,
- initialCurrency,
- onCreate,
-}: Props): VNode {
- const { i18n } = useTranslationContext();
-
- const state = useComponentState(
- exchangeUrlWithCurrency,
- initialAmount,
- initialCurrency,
- );
-
- if (state.noExchangeFound) {
- if (initialCurrency) {
- return (
-
-
-
- Manual Withdrawal for {initialCurrency}
-
-
-
-
- Choose a exchange from where the coins will be withdrawn. The
- exchange will send the coins to this wallet after receiving a wire
- transfer with the correct subject.
-
-
-
-
-
- No exchange found for {initialCurrency}
-
-
-
- Add Exchange
-
-
-
- );
- }
- return (
-
-
-
- Manual Withdrawal for {state.currency.value}
-
-
-
-
- Choose a exchange from where the coins will be withdrawn. The
- exchange will send the coins to this wallet after receiving a wire
- transfer with the correct subject.
-
-
-
-
- No exchange configured
-
-
- Add Exchange
-
-
-
- );
- }
-
- return (
-
-
- {error && (
- Can't create the reserve
- }
- description={error}
- />
- )}
-
-
- Manual Withdrawal for {state.currency.value}
-
-
-
-
- Choose a exchange from where the coins will be withdrawn. The
- exchange will send the coins to this wallet after receiving a wire
- transfer with the correct subject.
-
-
-