From 2e2cf4049a771c82fcc520686de3ace7603baa05 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 15 Jan 2024 17:34:19 -0300 Subject: fixes #8083 --- .../src/cta/Withdraw/state.ts | 49 +++++++++++++++++++--- 1 file changed, 43 insertions(+), 6 deletions(-) (limited to 'packages/taler-wallet-webextension/src/cta/Withdraw/state.ts') diff --git a/packages/taler-wallet-webextension/src/cta/Withdraw/state.ts b/packages/taler-wallet-webextension/src/cta/Withdraw/state.ts index 7bff13e51..bf460834d 100644 --- a/packages/taler-wallet-webextension/src/cta/Withdraw/state.ts +++ b/packages/taler-wallet-webextension/src/cta/Withdraw/state.ts @@ -21,11 +21,12 @@ import { ExchangeFullDetails, ExchangeListItem, ExchangeTosStatus, + NotificationType, TalerError, parseWithdrawExchangeUri, } from "@gnu-taler/taler-util"; import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; -import { useEffect, useState } from "preact/hooks"; +import { useEffect, useState, useMemo, useCallback } from "preact/hooks"; import { alertFromError, useAlertContext } from "../../context/alert.js"; import { useBackendContext } from "../../context/backend.js"; import { useTranslationContext } from "@gnu-taler/web-util/browser"; @@ -208,17 +209,40 @@ export function useComponentStateFromURI({ WalletApiOperation.GetWithdrawalDetailsForUri, { talerWithdrawUri, + notifyChangeFromPendingTimeoutMs: 30 * 1000 }, ); - const { amount, defaultExchangeBaseUrl, possibleExchanges } = uriInfo; + const { amount, defaultExchangeBaseUrl, possibleExchanges, operationId, confirmTransferUrl, status } = uriInfo; + const transaction = await api.wallet.call( + WalletApiOperation.GetWithdrawalTransactionByUri, + { talerWithdrawUri }, + ); return { talerWithdrawUri, + operationId, + status, + transaction, + confirmTransferUrl, amount: Amounts.parseOrThrow(amount), thisExchange: defaultExchangeBaseUrl, exchanges: possibleExchanges, }; }); + const readyToListen = uriInfoHook && !uriInfoHook.hasError + + useEffect(() => { + if (!uriInfoHook) { + return; + } + return api.listener.onUpdateNotification( + [NotificationType.WithdrawalOperationTransition], + () => { + uriInfoHook.retry() + }, + ); + }, [readyToListen]); + if (!uriInfoHook) return { status: "loading", error: undefined }; if (uriInfoHook.hasError) { @@ -257,8 +281,20 @@ export function useComponentStateFromURI({ }; } - return () => - exchangeSelectionState( + if (uriInfoHook.response.status !== "pending") { + if (uriInfoHook.response.transaction) { + onSuccess(uriInfoHook.response.transaction.transactionId) + } + return { + status: "already-completed", + operationState: uriInfoHook.response.status, + confirmTransferUrl: uriInfoHook.response.confirmTransferUrl, + error: undefined, + } + } + + return useCallback(() => { + return exchangeSelectionState( doManagedWithdraw, cancel, onSuccess, @@ -267,6 +303,7 @@ export function useComponentStateFromURI({ exchangeList, defaultExchange, ); + }, []) } type ManualOrManagedWithdrawFunction = ( @@ -294,7 +331,7 @@ function exchangeSelectionState( return selectedExchange; } - return (): State.Success | State.LoadingUriError | State.Loading => { + return useCallback((): State.Success | State.LoadingUriError | State.Loading => { const { i18n } = useTranslationContext(); const { pushAlertOnError } = useAlertContext(); const [ageRestricted, setAgeRestricted] = useState(0); @@ -428,5 +465,5 @@ function exchangeSelectionState( }, cancel, }; - }; + }, []); } -- cgit v1.2.3