From 7ea8321ddd2d56f43dceaa18340f1d1c39a83e76 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 20 Jan 2023 15:41:08 -0300 Subject: introducing getBalanceDetail for getting all depositable/transferable amount for a currency --- .../src/wallet/DestinationSelection/index.ts | 1 + .../src/wallet/DestinationSelection/state.ts | 20 ++++++++++++++++++++ .../src/wallet/DestinationSelection/stories.tsx | 2 ++ .../src/wallet/DestinationSelection/test.ts | 10 ++++++++++ .../src/wallet/DestinationSelection/views.tsx | 11 +++++++++-- .../taler-wallet-webextension/src/wallet/History.tsx | 4 +++- 6 files changed, 45 insertions(+), 3 deletions(-) (limited to 'packages/taler-wallet-webextension/src/wallet') diff --git a/packages/taler-wallet-webextension/src/wallet/DestinationSelection/index.ts b/packages/taler-wallet-webextension/src/wallet/DestinationSelection/index.ts index bd6b32e78..9a0ba1d88 100644 --- a/packages/taler-wallet-webextension/src/wallet/DestinationSelection/index.ts +++ b/packages/taler-wallet-webextension/src/wallet/DestinationSelection/index.ts @@ -66,6 +66,7 @@ export namespace State { error: undefined; type: Props["type"]; selectCurrency: ButtonHandler; + sendAll: ButtonHandler; previous: Contact[]; goToBank: ButtonHandler; goToWallet: ButtonHandler; diff --git a/packages/taler-wallet-webextension/src/wallet/DestinationSelection/state.ts b/packages/taler-wallet-webextension/src/wallet/DestinationSelection/state.ts index d5015ae1d..a921d32cb 100644 --- a/packages/taler-wallet-webextension/src/wallet/DestinationSelection/state.ts +++ b/packages/taler-wallet-webextension/src/wallet/DestinationSelection/state.ts @@ -27,10 +27,21 @@ import { Contact, Props, State } from "./index.js"; export function useComponentState(props: Props): RecursiveState { const api = useBackendContext(); const { pushAlertOnError } = useAlertContext(); + const parsedInitialAmount = !props.amount ? undefined : Amounts.parse(props.amount); + const hook = useAsyncAsHook(async () => { + if (!parsedInitialAmount) return undefined; + const resp = await api.wallet.call(WalletApiOperation.GetBalanceDetail, { + currency: parsedInitialAmount.currency, + }); + return resp; + }); + + const total = hook && !hook.hasError ? hook.response : undefined; + // const initialCurrency = parsedInitialAmount?.currency; const [amount, setAmount] = useState( @@ -120,6 +131,14 @@ export function useComponentState(props: Props): RecursiveState { props.goToWalletBankDeposit(currencyAndAmount); }), }, + sendAll: { + onClick: + total === undefined + ? undefined + : pushAlertOnError(async () => { + setAmount(total.balanceMerchantDepositable); + }), + }, goToWallet: { onClick: invalid ? undefined @@ -143,6 +162,7 @@ export function useComponentState(props: Props): RecursiveState { setAmount(undefined); }), }, + sendAll: {}, goToBank: { onClick: invalid ? undefined diff --git a/packages/taler-wallet-webextension/src/wallet/DestinationSelection/stories.tsx b/packages/taler-wallet-webextension/src/wallet/DestinationSelection/stories.tsx index 111f47776..247affec6 100644 --- a/packages/taler-wallet-webextension/src/wallet/DestinationSelection/stories.tsx +++ b/packages/taler-wallet-webextension/src/wallet/DestinationSelection/stories.tsx @@ -35,6 +35,7 @@ export const GetCash = tests.createExample(ReadyView, { }, }, goToBank: {}, + sendAll: {}, goToWallet: {}, previous: [], selectCurrency: {}, @@ -49,6 +50,7 @@ export const SendCash = tests.createExample(ReadyView, { }, }, goToBank: {}, + sendAll: {}, goToWallet: {}, previous: [], selectCurrency: {}, diff --git a/packages/taler-wallet-webextension/src/wallet/DestinationSelection/test.ts b/packages/taler-wallet-webextension/src/wallet/DestinationSelection/test.ts index b079ef0e8..c6a57270b 100644 --- a/packages/taler-wallet-webextension/src/wallet/DestinationSelection/test.ts +++ b/packages/taler-wallet-webextension/src/wallet/DestinationSelection/test.ts @@ -118,6 +118,16 @@ describe("Destination selection states", () => { expect(state.goToBank.onClick).not.eq(undefined); expect(state.goToWallet.onClick).not.eq(undefined); + expect(state.amountHandler.value).deep.eq( + Amounts.parseOrThrow("ARS:2"), + ); + }, + (state) => { + if (state.status !== "ready") expect.fail(); + if (state.error) expect.fail(); + expect(state.goToBank.onClick).not.eq(undefined); + expect(state.goToWallet.onClick).not.eq(undefined); + expect(state.amountHandler.value).deep.eq( Amounts.parseOrThrow("ARS:2"), ); diff --git a/packages/taler-wallet-webextension/src/wallet/DestinationSelection/views.tsx b/packages/taler-wallet-webextension/src/wallet/DestinationSelection/views.tsx index 8a7a1fa97..0649fd12f 100644 --- a/packages/taler-wallet-webextension/src/wallet/DestinationSelection/views.tsx +++ b/packages/taler-wallet-webextension/src/wallet/DestinationSelection/views.tsx @@ -17,6 +17,7 @@ import { styled } from "@linaria/react"; import { Fragment, h, VNode } from "preact"; import { AmountField } from "../../components/AmountField.js"; +import { JustInDevMode } from "../../components/JustInDevMode.js"; import { SelectList } from "../../components/SelectList.js"; import { Input, @@ -283,6 +284,7 @@ export function ReadySendView({ goToBank, goToWallet, previous, + sendAll, }: State.Ready): VNode { const { i18n } = useTranslationContext(); @@ -292,13 +294,18 @@ export function ReadySendView({ Specify the amount and the destination -
+ -
+ + + + {previous.length > 0 ? ( diff --git a/packages/taler-wallet-webextension/src/wallet/History.tsx b/packages/taler-wallet-webextension/src/wallet/History.tsx index 1d51f835a..f2a239f83 100644 --- a/packages/taler-wallet-webextension/src/wallet/History.tsx +++ b/packages/taler-wallet-webextension/src/wallet/History.tsx @@ -111,8 +111,10 @@ export function HistoryView({ balances: Balance[]; }): VNode { const { i18n } = useTranslationContext(); - const currencies = balances.map((b) => b.available.split(":")[0]); const { pushAlertOnError } = useAlertContext(); + const currencies = balances + .filter((b) => Amounts.isNonZero(b.available)) + .map((b) => b.available.split(":")[0]); const defaultCurrencyIndex = currencies.findIndex( (c) => c === defaultCurrency, -- cgit v1.2.3