/* This file is part of GNU Taler (C) 2022 Taler Systems S.A. GNU Taler is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Taler; see the file COPYING. If not, see */ import { AmountJson, Amounts, HttpStatusCode, Logger, TranslatedString, parseWithdrawUri } from "@gnu-taler/taler-util"; import { notifyError, useLocalNotification, useTranslationContext } from "@gnu-taler/web-util/browser"; import { Fragment, VNode, h } from "preact"; import { forwardRef } from "preact/compat"; import { useState } from "preact/hooks"; import { Attention } from "@gnu-taler/web-util/browser"; import { useBankCoreApiContext } from "../context/config.js"; import { useBackendState } from "../hooks/backend.js"; import { usePreferences } from "../hooks/preferences.js"; import { undefinedIfEmpty, withRuntimeErrorHandling } from "../utils.js"; import { OperationState } from "./OperationState/index.js"; import { InputAmount, doAutoFocus } from "./PaytoWireTransferForm.js"; import { assertUnreachable } from "./WithdrawalOperationPage.js"; import { LocalNotificationBanner } from "@gnu-taler/web-util/browser"; const logger = new Logger("WalletWithdrawForm"); const RefAmount = forwardRef(InputAmount); function OldWithdrawalForm({ goToConfirmOperation, limit, onCancel, focus }: { limit: AmountJson; focus?: boolean; goToConfirmOperation: (operationId: string) => void; onCancel: () => void; }): VNode { const { i18n } = useTranslationContext(); const [settings, updateSettings] = usePreferences() const { state: credentials } = useBackendState(); const creds = credentials.status !== "loggedIn" ? undefined : credentials const { api } = useBankCoreApiContext() const [amountStr, setAmountStr] = useState(`${settings.maxWithdrawalAmount}`); const [notification, notify, handleError] = useLocalNotification() if (!!settings.currentWithdrawalOperationId) { return To complete or cancel the operation click here } const trimmedAmountStr = amountStr?.trim(); const parsedAmount = trimmedAmountStr ? Amounts.parse(`${limit.currency}:${trimmedAmountStr}`) : undefined; const errors = undefinedIfEmpty({ amount: trimmedAmountStr == null ? i18n.str`required` : !parsedAmount ? i18n.str`invalid` : Amounts.cmp(limit, parsedAmount) === -1 ? i18n.str`balance is not enough` : undefined, }); async function doStart() { if (!parsedAmount || !creds) return; await handleError(async () => { const resp = await api.createWithdrawal(creds, { amount: Amounts.stringify(parsedAmount), }); if (resp.type === "ok") { const uri = parseWithdrawUri(resp.body.taler_withdraw_uri); if (!uri) { return notifyError( i18n.str`Server responded with an invalid withdraw URI`, i18n.str`Withdraw URI: ${resp.body.taler_withdraw_uri}`); } else { updateSettings("currentWithdrawalOperationId", uri.withdrawalOperationId) goToConfirmOperation(uri.withdrawalOperationId); } } else { switch (resp.case) { case HttpStatusCode.Conflict: { notify({ type: "error", title: i18n.str`The operation was rejected due to insufficient funds`, description: resp.detail.hint as TranslatedString, debug: resp.detail, }) break; } case HttpStatusCode.Unauthorized: { notify({ type: "error", title: i18n.str`The operation was rejected due to insufficient funds`, description: resp.detail.hint as TranslatedString, debug: resp.detail, }) break; } case HttpStatusCode.NotFound: { notify({ type: "error", title: i18n.str`Account not found`, description: resp.detail.hint as TranslatedString, debug: resp.detail, }) break; } default: assertUnreachable(resp) } } }) } return
{ e.preventDefault() }} >
{ setAmountStr(v); }} error={errors?.amount} ref={focus ? doAutoFocus : undefined} />
} export function WalletWithdrawForm({ focus, limit, onCancel, goToConfirmOperation, }: { limit: AmountJson; focus?: boolean; goToConfirmOperation: (operationId: string) => void; onCancel: () => void; }): VNode { const { i18n } = useTranslationContext(); const [settings, updateSettings] = usePreferences() return (

Prepare your wallet

After using your wallet you will need to confirm or cancel the operation on this site.

{settings.showInstallWallet && { updateSettings("showInstallWallet", false); }}> If you don't have one yet you can follow the instruction here } {!settings.fastWithdrawal ? : }
); }