diff options
author | Sebastian <sebasjm@gmail.com> | 2024-01-15 17:34:19 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-01-15 17:36:48 -0300 |
commit | 2e2cf4049a771c82fcc520686de3ace7603baa05 (patch) | |
tree | 620ab22d4fc0f621d0a574c8f98d1c49f1d67804 /packages/taler-wallet-webextension | |
parent | ef0bb60f23c0c755814f648b8d71a29a843e066c (diff) | |
download | wallet-core-2e2cf4049a771c82fcc520686de3ace7603baa05.tar.xz |
fixes #8083
Diffstat (limited to 'packages/taler-wallet-webextension')
7 files changed, 134 insertions, 16 deletions
diff --git a/packages/taler-wallet-webextension/src/cta/Withdraw/index.ts b/packages/taler-wallet-webextension/src/cta/Withdraw/index.ts index 04713f3c4..1f8745a5d 100644 --- a/packages/taler-wallet-webextension/src/cta/Withdraw/index.ts +++ b/packages/taler-wallet-webextension/src/cta/Withdraw/index.ts @@ -38,7 +38,7 @@ import { ErrorAlertView } from "../../components/CurrentAlerts.js"; import { ErrorAlert } from "../../context/alert.js"; import { ExchangeSelectionPage } from "../../wallet/ExchangeSelection/index.js"; import { NoExchangesView } from "../../wallet/ExchangeSelection/views.js"; -import { SelectAmountView, SuccessView } from "./views.js"; +import { FinalStateOperation, SelectAmountView, SuccessView } from "./views.js"; export interface PropsFromURI { talerWithdrawUri: string | undefined; @@ -60,6 +60,7 @@ export type State = | SelectExchangeState.NoExchangeFound | SelectExchangeState.Selecting | State.SelectAmount + | State.AlreadyCompleted | State.Success; export namespace State { @@ -80,6 +81,12 @@ export namespace State { amount: AmountFieldHandler; currency: string; } + export interface AlreadyCompleted { + status: "already-completed"; + operationState: "confirmed" | "aborted" | "selected"; + confirmTransferUrl?: string, + error: undefined; + } export type Success = { status: "success"; @@ -116,6 +123,7 @@ const viewMapping: StateViewMap<State> = { "no-exchange-found": NoExchangesView, "selecting-exchange": ExchangeSelectionPage, success: SuccessView, + "already-completed": FinalStateOperation, }; export const WithdrawPageFromURI = compose( 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, }; - }; + }, []); } diff --git a/packages/taler-wallet-webextension/src/cta/Withdraw/stories.tsx b/packages/taler-wallet-webextension/src/cta/Withdraw/stories.tsx index a3127fafc..29f39054f 100644 --- a/packages/taler-wallet-webextension/src/cta/Withdraw/stories.tsx +++ b/packages/taler-wallet-webextension/src/cta/Withdraw/stories.tsx @@ -23,7 +23,7 @@ import { CurrencySpecification, ExchangeListItem } from "@gnu-taler/taler-util"; import * as tests from "@gnu-taler/web-util/testing"; import { nullFunction } from "../../mui/handlers.js"; // import { TermsState } from "../../utils/index.js"; -import { SuccessView } from "./views.js"; +import { SuccessView, FinalStateOperation } from "./views.js"; export default { title: "withdraw", @@ -67,6 +67,23 @@ export const TermsOfServiceNotYetLoaded = tests.createExample(SuccessView, { chooseCurrencies: [], }); +export const AlreadyAborted = tests.createExample(FinalStateOperation, { + error: undefined, + status: "already-completed", + operationState: "aborted" +}); +export const AlreadySelected = tests.createExample(FinalStateOperation, { + error: undefined, + status: "already-completed", + operationState: "selected" +}); +export const AlreadyConfirmed = tests.createExample(FinalStateOperation, { + error: undefined, + status: "already-completed", + operationState: "confirmed" +}); + + export const WithSomeFee = tests.createExample(SuccessView, { error: undefined, status: "success", diff --git a/packages/taler-wallet-webextension/src/cta/Withdraw/test.ts b/packages/taler-wallet-webextension/src/cta/Withdraw/test.ts index 3493415d9..f90f7bed7 100644 --- a/packages/taler-wallet-webextension/src/cta/Withdraw/test.ts +++ b/packages/taler-wallet-webextension/src/cta/Withdraw/test.ts @@ -111,10 +111,19 @@ describe("Withdraw CTA states", () => { WalletApiOperation.GetWithdrawalDetailsForUri, undefined, { + status: "pending", + operationId: "123", amount: "EUR:2" as AmountString, possibleExchanges: [], }, ); + handler.addWalletCallResponse( + WalletApiOperation.GetWithdrawalTransactionByUri, + undefined, + { + transactionId: "123" + } as any, + ); const hookBehavior = await tests.hookBehaveLikeThis( useComponentStateFromURI, @@ -147,12 +156,21 @@ describe("Withdraw CTA states", () => { WalletApiOperation.GetWithdrawalDetailsForUri, undefined, { + status: "pending", + operationId: "123", amount: "ARS:2" as AmountString, possibleExchanges: exchanges, defaultExchangeBaseUrl: exchanges[0].exchangeBaseUrl, }, ); handler.addWalletCallResponse( + WalletApiOperation.GetWithdrawalTransactionByUri, + undefined, + { + transactionId: "123" + } as any, + ); + handler.addWalletCallResponse( WalletApiOperation.GetWithdrawalDetailsForAmount, undefined, { @@ -217,6 +235,8 @@ describe("Withdraw CTA states", () => { WalletApiOperation.GetWithdrawalDetailsForUri, undefined, { + status: "pending", + operationId: "123", amount: "ARS:2" as AmountString, possibleExchanges: exchangeWithNewTos, defaultExchangeBaseUrl: exchangeWithNewTos[0].exchangeBaseUrl, @@ -245,6 +265,8 @@ describe("Withdraw CTA states", () => { WalletApiOperation.GetWithdrawalDetailsForUri, undefined, { + status: "pending", + operationId: "123", amount: "ARS:2" as AmountString, possibleExchanges: exchanges, defaultExchangeBaseUrl: exchanges[0].exchangeBaseUrl, diff --git a/packages/taler-wallet-webextension/src/cta/Withdraw/views.tsx b/packages/taler-wallet-webextension/src/cta/Withdraw/views.tsx index 748b65817..bd9f75696 100644 --- a/packages/taler-wallet-webextension/src/cta/Withdraw/views.tsx +++ b/packages/taler-wallet-webextension/src/cta/Withdraw/views.tsx @@ -21,7 +21,7 @@ import { Amount } from "../../components/Amount.js"; import { Part } from "../../components/Part.js"; import { QR } from "../../components/QR.js"; import { SelectList } from "../../components/SelectList.js"; -import { Input, LinkSuccess, SvgIcon } from "../../components/styled/index.js"; +import { Input, LinkSuccess, SvgIcon, WarningBox } from "../../components/styled/index.js"; import { TermsOfService } from "../../components/TermsOfService/index.js"; import { useTranslationContext } from "@gnu-taler/web-util/browser"; import { Button } from "../../mui/Button.js"; @@ -35,6 +35,33 @@ import { State } from "./index.js"; import { Grid } from "../../mui/Grid.js"; import { AmountField } from "../../components/AmountField.js"; +export function FinalStateOperation(state: State.AlreadyCompleted): VNode { + const { i18n } = useTranslationContext(); + + switch (state.operationState) { + case "confirmed": return <WarningBox> + <div style={{ justifyContent: "center", lineHeight: "25px" }}> + <i18n.Translate>This operation has already been completed by another wallet.</i18n.Translate> + </div> + </WarningBox> + case "aborted": return <WarningBox> + <div style={{ justifyContent: "center", lineHeight: "25px" }}> + <i18n.Translate>This operation has already been aborted</i18n.Translate> + </div> + </WarningBox> + case "selected": return <WarningBox> + <div style={{ justifyContent: "center", lineHeight: "25px" }}> + <i18n.Translate>This operation has already been used by another wallet.</i18n.Translate> + </div> + <div style={{ justifyContent: "center", lineHeight: "25px" }}> + <i18n.Translate>It can be confirmed in</i18n.Translate> <a target="_bank" rel="noreferrer" href={state.confirmTransferUrl}> + <i18n.Translate>this page</i18n.Translate> + </a> + </div> + </WarningBox> + } +} + export function SuccessView(state: State.Success): VNode { const { i18n } = useTranslationContext(); // const currentTosVersionIsAccepted = diff --git a/packages/taler-wallet-webextension/src/utils/index.ts b/packages/taler-wallet-webextension/src/utils/index.ts index ad4eabf15..d83e6f472 100644 --- a/packages/taler-wallet-webextension/src/utils/index.ts +++ b/packages/taler-wallet-webextension/src/utils/index.ts @@ -15,6 +15,7 @@ */ import { createElement, VNode } from "preact"; +import { useCallback, useMemo } from "preact/hooks"; function getJsonIfOk(r: Response): Promise<any> { if (r.ok) { @@ -26,8 +27,7 @@ function getJsonIfOk(r: Response): Promise<any> { } throw new Error( - `Try another server: (${r.status}) ${ - r.statusText || "internal server error" + `Try another server: (${r.status}) ${r.statusText || "internal server error" }`, ); } @@ -89,6 +89,7 @@ export function compose<SType extends { status: string }, PType>( ): (p: PType) => VNode { function withHook(stateHook: () => RecursiveState<SType>): () => VNode { function TheComponent(): VNode { + //if the function is the same, do not compute const state = stateHook(); if (typeof state === "function") { @@ -102,7 +103,9 @@ export function compose<SType extends { status: string }, PType>( } // TheComponent.name = `${name}`; - return TheComponent; + return useMemo(() => { + return TheComponent + }, [stateHook]); } return (p: PType) => { diff --git a/packages/taler-wallet-webextension/src/wxBackend.ts b/packages/taler-wallet-webextension/src/wxBackend.ts index a194de0ff..1ecd66f05 100644 --- a/packages/taler-wallet-webextension/src/wxBackend.ts +++ b/packages/taler-wallet-webextension/src/wxBackend.ts @@ -272,11 +272,15 @@ async function reinitWallet(): Promise<void> { let timer; if (platform.useServiceWorkerAsBackgroundProcess()) { - httpLib = new ServiceWorkerHttpLib(); + httpLib = new ServiceWorkerHttpLib({ + // enableThrottling: false, + }); cryptoWorker = new SynchronousCryptoWorkerFactoryPlain(); timer = new SetTimeoutTimerAPI(); } else { - httpLib = new BrowserHttpLib(); + httpLib = new BrowserHttpLib({ + // enableThrottling: false, + }); // We could (should?) use the BrowserCryptoWorkerFactory here, // but right now we don't, to have less platform differences. // cryptoWorker = new BrowserCryptoWorkerFactory(); @@ -409,9 +413,9 @@ async function toggleHeaderListener( platform.registerTalerHeaderListener(); return { newValue: true }; } catch (e) { - logger.error("FAIL to toggle",e) + logger.error("FAIL to toggle", e) } - return { newValue: false } + return { newValue: false } } const rem = await platform.getPermissionsApi().removeHostPermissions(); |