diff options
Diffstat (limited to 'packages/taler-wallet-webextension/src/wallet/DestinationSelection/state.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/wallet/DestinationSelection/state.ts | 178 |
1 files changed, 72 insertions, 106 deletions
diff --git a/packages/taler-wallet-webextension/src/wallet/DestinationSelection/state.ts b/packages/taler-wallet-webextension/src/wallet/DestinationSelection/state.ts index d4e270a6c..de2d439b6 100644 --- a/packages/taler-wallet-webextension/src/wallet/DestinationSelection/state.ts +++ b/packages/taler-wallet-webextension/src/wallet/DestinationSelection/state.ts @@ -14,7 +14,14 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { Amounts } from "@gnu-taler/taler-util"; +import { + ExchangeUpdateStatus, + KnownBankAccountsInfo, + PaytoUri, + ScopeType, + parseScopeInfoShort, + stringifyScopeInfoShort +} from "@gnu-taler/taler-util"; import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; import { useTranslationContext } from "@gnu-taler/web-util/browser"; import { useState } from "preact/hooks"; @@ -22,61 +29,46 @@ import { alertFromError, useAlertContext } from "../../context/alert.js"; import { useBackendContext } from "../../context/backend.js"; import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js"; import { RecursiveState, assertUnreachable } from "../../utils/index.js"; -import { Contact, Props, State } from "./index.js"; +import { Props, State } from "./index.js"; export function useComponentState(props: Props): RecursiveState<State> { const api = useBackendContext(); const { pushAlertOnError } = useAlertContext(); + const { i18n } = useTranslationContext(); - const parsedInitialAmount = !props.amount - ? undefined - : Amounts.parse(props.amount); + const [scope, setScope] = useState(props.scope); const hook = useAsyncAsHook(async () => { - if (!parsedInitialAmount) return undefined; - const balance = await api.wallet.call(WalletApiOperation.GetBalanceDetail, { - currency: parsedInitialAmount.currency, - }); - return { balance }; + const resp = await api.wallet.call( + WalletApiOperation.ListKnownBankAccounts, + {}, + ); + return resp }); - const info = hook && !hook.hasError ? hook.response : undefined; + const previous: KnownBankAccountsInfo[] = props.type === "send" && hook && !hook.hasError ? hook.response.accounts : []; - // const initialCurrency = parsedInitialAmount?.currency; - - const [amount, setAmount] = useState( - !parsedInitialAmount ? undefined : parsedInitialAmount, - ); - //FIXME: get this information from wallet - // eslint-disable-next-line no-constant-condition - const previous: Contact[] = true - ? [] - : [ - { - name: "International Bank", - icon_type: "bank", - description: "account ending with 3454", - }, - { - name: "Max", - icon_type: "bank", - description: "account ending with 3454", - }, - { - name: "Alex", - icon_type: "bank", - description: "account ending with 3454", - }, - ]; - - if (!amount) { + if (!scope) { return () => { - // eslint-disable-next-line react-hooks/rules-of-hooks const { i18n } = useTranslationContext(); - // eslint-disable-next-line react-hooks/rules-of-hooks - const hook = useAsyncAsHook(() => - api.wallet.call(WalletApiOperation.ListExchanges, {}), - ); + const hook = useAsyncAsHook(async () => { + const resp = await api.wallet.call( + WalletApiOperation.ListExchanges, + {}, + ); + + const unknownIndex = resp.exchanges.findIndex( + (d) => d.exchangeUpdateStatus === ExchangeUpdateStatus.Initial, + ); + if (unknownIndex === -1) return resp; + + await api.wallet.call(WalletApiOperation.UpdateExchangeEntry, { + exchangeBaseUrl: resp.exchanges[unknownIndex].exchangeBaseUrl, + force: true, + }); + + return await api.wallet.call(WalletApiOperation.ListExchanges, {}); + }); if (!hook) { return { @@ -87,14 +79,30 @@ export function useComponentState(props: Props): RecursiveState<State> { if (hook.hasError) { return { status: "error", - error: alertFromError(i18n, - i18n.str`Could not load exchanges`, hook), + error: alertFromError(i18n, i18n.str`Could not load exchanges`, hook), }; } const currencies: Record<string, string> = {}; - hook.response.exchanges.forEach((e) => { - if (e.currency) { - currencies[e.currency] = e.currency; + hook.response.exchanges.forEach((b) => { + switch (b.scopeInfo.type) { + case ScopeType.Global: { + currencies[stringifyScopeInfoShort(b.scopeInfo)] = + b.scopeInfo.currency; + break; + } + case ScopeType.Exchange: { + currencies[stringifyScopeInfoShort(b.scopeInfo)] = + `${b.scopeInfo.currency} ${b.scopeInfo.url}`; + break; + } + case ScopeType.Auditor: { + currencies[stringifyScopeInfoShort(b.scopeInfo)] = + `${b.scopeInfo.currency} ${b.scopeInfo.url}`; + break; + } + default: { + assertUnreachable(b.scopeInfo); + } } }); currencies[""] = "Select a currency"; @@ -103,55 +111,32 @@ export function useComponentState(props: Props): RecursiveState<State> { status: "select-currency", error: undefined, onCurrencySelected: (c: string) => { - setAmount(Amounts.zeroOfCurrency(c)); + const scope = parseScopeInfoShort(c); + setScope(scope); }, currencies, }; }; } - const currencyAndAmount = Amounts.stringify(amount); - const invalid = Amounts.isZero(amount); - switch (props.type) { case "send": return { status: "ready", error: undefined, previous, - selectCurrency: { - onClick: pushAlertOnError(async () => { - setAmount(undefined); - }), - }, + onSelectAccount: pushAlertOnError(async (account: PaytoUri) => { + props.goToWalletKnownBankDeposit(scope, account); + }), goToBank: { - onClick: invalid - ? undefined - : pushAlertOnError(async () => { - props.goToWalletBankDeposit(currencyAndAmount); - }), - }, - selectMax: { onClick: pushAlertOnError(async () => { - const resp = await api.wallet.call( - WalletApiOperation.GetMaxDepositAmount, - { - currency: amount.currency, - }, - ); - setAmount(Amounts.parseOrThrow(resp.effectiveAmount)); + props.goToWalletNewBankDeposit(scope); }), }, goToWallet: { - onClick: invalid - ? undefined - : pushAlertOnError(async () => { - props.goToWalletWalletSend(currencyAndAmount); - }), - }, - amountHandler: { - onInput: pushAlertOnError(async (s) => setAmount(s)), - value: amount, + onClick: pushAlertOnError(async () => { + props.goToWalletWalletSend(scope); + }), }, type: props.type, }; @@ -160,35 +145,16 @@ export function useComponentState(props: Props): RecursiveState<State> { status: "ready", error: undefined, previous, - selectCurrency: { + goToBank: { onClick: pushAlertOnError(async () => { - setAmount(undefined); + props.goToWalletManualWithdraw(scope); }), }, - selectMax: { - onClick: invalid - ? undefined - : pushAlertOnError(async () => { - props.goToWalletManualWithdraw(currencyAndAmount); - }), - }, - goToBank: { - onClick: invalid - ? undefined - : pushAlertOnError(async () => { - props.goToWalletManualWithdraw(currencyAndAmount); - }), - }, + onSelectAccount: () => { }, goToWallet: { - onClick: invalid - ? undefined - : pushAlertOnError(async () => { - props.goToWalletWalletInvoice(currencyAndAmount); - }), - }, - amountHandler: { - onInput: pushAlertOnError(async (s) => setAmount(s)), - value: amount, + onClick: pushAlertOnError(async () => { + props.goToWalletWalletInvoice(scope); + }), }, type: props.type, }; |