diff options
Diffstat (limited to 'packages/taler-wallet-webextension/src/cta/Withdraw')
3 files changed, 87 insertions, 123 deletions
diff --git a/packages/taler-wallet-webextension/src/cta/Withdraw/index.ts b/packages/taler-wallet-webextension/src/cta/Withdraw/index.ts index 418fef505..91bde9369 100644 --- a/packages/taler-wallet-webextension/src/cta/Withdraw/index.ts +++ b/packages/taler-wallet-webextension/src/cta/Withdraw/index.ts @@ -16,9 +16,9 @@ import { AmountJson, - AmountString, CurrencySpecification, ExchangeListItem, + ScopeInfo } from "@gnu-taler/taler-util"; import { Loading } from "../../components/Loading.js"; import { State as SelectExchangeState } from "../../hooks/useSelectedExchange.js"; @@ -37,7 +37,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 { FinalStateOperation, SelectAmountView, SuccessView } from "./views.js"; +import { FinalStateOperation, SuccessView } from "./views.js"; export interface PropsFromURI { talerWithdrawUri: string | undefined; @@ -47,10 +47,10 @@ export interface PropsFromURI { export interface PropsFromParams { talerExchangeWithdrawUri: string | undefined; + scope: ScopeInfo; amount: string | undefined; cancel: () => Promise<void>; onSuccess: (txid: string) => Promise<void>; - onAmountChanged: (amount: AmountString) => Promise<void>; } export type State = @@ -58,7 +58,7 @@ export type State = | State.LoadingUriError | SelectExchangeState.NoExchangeFound | SelectExchangeState.Selecting - | State.SelectAmount + // | State.SelectAmount | State.AlreadyCompleted | State.Success; @@ -72,14 +72,14 @@ export namespace State { error: ErrorAlert; } - export interface SelectAmount { - status: "select-amount"; - error: undefined; - exchangeBaseUrl: string; - confirm: ButtonHandler; - amount: AmountFieldHandler; - currency: string; - } + // export interface SelectAmount { + // status: "select-amount"; + // error: undefined; + // exchangeBaseUrl: string; + // confirm: ButtonHandler; + // amount: AmountFieldHandler; + // currency: string; + // } export interface AlreadyCompleted { status: "already-completed"; operationState: "confirmed" | "aborted" | "selected"; @@ -126,7 +126,7 @@ export namespace State { const viewMapping: StateViewMap<State> = { loading: Loading, error: ErrorAlertView, - "select-amount": SelectAmountView, + // "select-amount": SelectAmountView, "no-exchange-found": NoExchangesView, "selecting-exchange": ExchangeSelectionPage, success: SuccessView, diff --git a/packages/taler-wallet-webextension/src/cta/Withdraw/state.ts b/packages/taler-wallet-webextension/src/cta/Withdraw/state.ts index 0541bbf3f..8a862d200 100644 --- a/packages/taler-wallet-webextension/src/cta/Withdraw/state.ts +++ b/packages/taler-wallet-webextension/src/cta/Withdraw/state.ts @@ -22,6 +22,7 @@ import { ExchangeListItem, NotificationType, TransactionMajorState, + TransactionMinorState, parseWithdrawExchangeUri, } from "@gnu-taler/taler-util"; import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; @@ -37,8 +38,8 @@ import { PropsFromParams, PropsFromURI, State } from "./index.js"; export function useComponentStateFromParams({ talerExchangeWithdrawUri: maybeTalerUri, amount, + scope, cancel, - onAmountChanged, onSuccess, }: PropsFromParams): RecursiveState<State> { const api = useBackendContext(); @@ -96,66 +97,66 @@ export function useComponentStateFromParams({ const maybeAmount = uriInfoHook.response.amount ?? paramsAmount; - if (!maybeAmount) { - const exchangeBaseUrl = - uriInfoHook.response.exchange?.exchangeBaseUrl ?? - (exchangeList.length > 0 ? exchangeList[0].exchangeBaseUrl : undefined); - const currency = - uriInfoHook.response.exchange?.currency ?? - (exchangeList.length > 0 ? exchangeList[0].currency : undefined); - - if (!exchangeBaseUrl) { - return { - status: "error", - error: { - message: i18n.str`Can't withdraw from exchange`, - description: i18n.str`Missing base URL`, - cause: undefined, - context: {}, - type: "error", - }, - }; - } - if (!currency) { - return { - status: "error", - error: { - message: i18n.str`Can't withdraw from exchange`, - description: i18n.str`Missing unknown currency`, - cause: undefined, - context: {}, - type: "error", - }, - }; - } - return () => { - const { pushAlertOnError } = useAlertContext(); - const [amount, setAmount] = useState<AmountJson>( - Amounts.zeroOfCurrency(currency), - ); - const isValid = Amounts.isNonZero(amount); - return { - status: "select-amount", - currency, - exchangeBaseUrl, - error: undefined, - confirm: { - onClick: isValid - ? pushAlertOnError(async () => { - onAmountChanged(Amounts.stringify(amount)); - }) - : undefined, - }, - amount: { - value: amount, - onInput: pushAlertOnError(async (e) => { - setAmount(e); - }), - }, - }; - }; - } - const chosenAmount = maybeAmount; + // if (!maybeAmount) { + // const exchangeBaseUrl = + // uriInfoHook.response.exchange?.exchangeBaseUrl ?? + // (exchangeList.length > 0 ? exchangeList[0].exchangeBaseUrl : undefined); + // const currency = + // uriInfoHook.response.exchange?.currency ?? + // (exchangeList.length > 0 ? exchangeList[0].currency : undefined); + + // if (!exchangeBaseUrl) { + // return { + // status: "error", + // error: { + // message: i18n.str`Can't withdraw from exchange`, + // description: i18n.str`Missing base URL`, + // cause: undefined, + // context: {}, + // type: "error", + // }, + // }; + // } + // if (!currency) { + // return { + // status: "error", + // error: { + // message: i18n.str`Can't withdraw from exchange`, + // description: i18n.str`Missing unknown currency`, + // cause: undefined, + // context: {}, + // type: "error", + // }, + // }; + // } + // return () => { + // const { pushAlertOnError } = useAlertContext(); + // const [amount, setAmount] = useState<AmountJson>( + // Amounts.zeroOfCurrency(currency), + // ); + // const isValid = Amounts.isNonZero(amount); + // return { + // status: "select-amount", + // currency, + // exchangeBaseUrl, + // error: undefined, + // confirm: { + // onClick: isValid + // ? pushAlertOnError(async () => { + // onAmountChanged(Amounts.stringify(amount)); + // }) + // : undefined, + // }, + // amount: { + // value: amount, + // onInput: pushAlertOnError(async (e) => { + // setAmount(e); + // }), + // }, + // }; + // }; + // } + const chosenAmount = maybeAmount ?? Amounts.zeroOfCurrency(scope.currency); async function doManualWithdraw( exchange: string, @@ -207,6 +208,7 @@ export function useComponentStateFromURI({ const api = useBackendContext(); const { i18n } = useTranslationContext(); + // eslint-disable-next-line @typescript-eslint/no-unused-vars const [updatedExchangeByUser, setUpdatedExchangeByUser] = useState<string>(); /** * Ask the wallet about the withdraw URI @@ -315,11 +317,14 @@ export function useComponentStateFromURI({ uriInfoHook.response.status !== "pending" ) { const info = uriInfoHook.response.txInfo; + const anotherWallet = + info.txState.major === TransactionMajorState.Aborted && + info.txState.minor === TransactionMinorState.CompletedByOtherWallet; return { status: "already-completed", operationState: uriInfoHook.response.status, confirmTransferUrl: bwi.confirmTransferUrl, - thisWallet: info.txState.major === TransactionMajorState.Pending, + thisWallet: !anotherWallet, redirectToTx: () => onSuccess(info.transactionId), error: undefined, }; @@ -508,10 +513,7 @@ function exchangeSelectionState( const altCurrencies = amountHook.response.accounts .filter((a) => !!a.currencySpecification) .map((a) => a.currencySpecification!.name); - const chooseCurrencies = - altCurrencies.length === 0 - ? [] - : [toBeReceived.currency, ...altCurrencies]; + const chooseCurrencies = altCurrencies.length <= 1 ? [] : altCurrencies; const convAccount = amountHook.response.accounts.find((c) => { return ( @@ -528,8 +530,11 @@ function exchangeSelectionState( const amountError = Amounts.isZero(choosenAmount) ? i18n.str`should be greater than zero` - : Amounts.cmp(choosenAmount, wInfo.maxAmount) === -1 - ? i18n.str`choose a lower value` + : Amounts.isNonZero(wInfo.maxAmount) && + Amounts.cmp(choosenAmount, wInfo.maxAmount) === 1 + ? i18n.str`can't be greater than ${Amounts.stringifyValue( + wInfo.maxAmount, + )}` : undefined; return { diff --git a/packages/taler-wallet-webextension/src/cta/Withdraw/views.tsx b/packages/taler-wallet-webextension/src/cta/Withdraw/views.tsx index b6a356de8..3283f998f 100644 --- a/packages/taler-wallet-webextension/src/cta/Withdraw/views.tsx +++ b/packages/taler-wallet-webextension/src/cta/Withdraw/views.tsx @@ -39,10 +39,10 @@ import { getAmountWithFee, } from "../../wallet/Transaction.js"; import { State } from "./index.js"; -import { Amounts } from "@gnu-taler/taler-util"; export function FinalStateOperation(state: State.AlreadyCompleted): VNode { const { i18n } = useTranslationContext(); + // document.location.href = res.confirmTransferUrl if (state.thisWallet) { switch (state.operationState) { @@ -280,44 +280,3 @@ function WithdrawWithMobile({ ); } -export function SelectAmountView({ - amount, - exchangeBaseUrl, - confirm, -}: State.SelectAmount): VNode { - const { i18n } = useTranslationContext(); - return ( - <Fragment> - <section style={{ textAlign: "left" }}> - <Part - title={ - <div - style={{ - display: "flex", - alignItems: "center", - }} - > - <i18n.Translate>Exchange</i18n.Translate> - </div> - } - text={<ExchangeDetails exchange={exchangeBaseUrl} />} - kind="neutral" - big - /> - <Grid container columns={2} justifyContent="space-between"> - <AmountField label={i18n.str`Amount`} required handler={amount} /> - </Grid> - </section> - <section> - <Button - variant="contained" - color="info" - disabled={!confirm.onClick} - onClick={confirm.onClick} - > - <i18n.Translate>See details</i18n.Translate> - </Button> - </section> - </Fragment> - ); -} |