merchant-backoffice

ZZZ: Inactive/Deprecated
Log | Files | Refs | Submodules | README

commit 6aa49378d152c4b099263a272bd574b85508ce5d
parent 838ec0ff5e7331fa4c969837189702bbe6d8d3e4
Author: MS <ms@taler.net>
Date:   Mon, 21 Feb 2022 09:46:54 +0100

fix wire transfer submission

Diffstat:
Mpackages/bank/src/pages/home/index.tsx | 68++++++++++++++++++++++++++++++++++++++++++--------------------------
1 file changed, 42 insertions(+), 26 deletions(-)

diff --git a/packages/bank/src/pages/home/index.tsx b/packages/bank/src/pages/home/index.tsx @@ -57,7 +57,7 @@ interface BackendStateType { */ interface TransactionRequestType { paytoUri: string; - amount?: string; + amount?: string; // with currency. } /** @@ -106,6 +106,26 @@ interface AccountStateType { ***********/ /** + * Validate (the number part of) an amount. If needed, + * replace comma with a dot. Returns 'false' whenever + * the input is invalid, the valid amount otherwise. + */ +function validateAmount(maybeAmount: string): string { + const amountRegex = "^[0-9]+(\.[0-9]+)?$"; + if (typeof maybeAmount !== "undefined" || maybeAmount !== "") { + console.log("Maybe valid amount", maybeAmount); + // tolerating comma instead of point. + maybeAmount = maybeAmount.replace(",", "."); + const re = RegExp(amountRegex) + if (!re.test(maybeAmount)) { + console.log(`Not withdrawing invalid amount '${maybeAmount}'.`); + return false; + } + } + return maybeAmount; +} + +/** * Extract IBAN from a Payto URI. */ function getIbanFromPayto(url: string): string { @@ -118,7 +138,7 @@ function getIbanFromPayto(url: string): string { } /** - * Parse amount. + * Extract value and currency from a $currency:x.y amount. */ function parseAmount(val: string): Amount { const format = /^[A-Z]+:[0-9]+(\.[0-9]+)?$/; @@ -154,17 +174,6 @@ async function postToBackend( } const { username, password } = backendState; let headers = prepareHeaders(username, password); - /** - * NOTE: tests show that when a same object is being - * POSTed, caching might prevent same requests from being - * made. Hence, trying to POST twice the same amount might - * get silently ignored. - * - * headers.append("cache-control", "no-store"); - * headers.append("cache-control", "no-cache"); - * headers.append("pragma", "no-cache"); - * */ - // Backend URL must have been stored _with_ a final slash. const url = new URL(uri, backendState.url) return await fetch(url.href, { @@ -176,7 +185,6 @@ async function postToBackend( } function useTransactionPageNumber(): [number, StateUpdater<number>] { - const ret = useNotNullLocalStorage("transaction-page", "0"); const retObj = JSON.parse(ret[0]); const retSetter: StateUpdater<number> = function(val) { @@ -653,6 +661,7 @@ function PaytoWireTransfer(Props: any): VNode { const currency = useContext(CurrencyContext); const i18n = useTranslator(); const amountRegex = "^[0-9]+(\.[0-9]+)?$"; + var amountInput = ""; var transactionData: TransactionRequestType; return <div> @@ -661,10 +670,7 @@ function PaytoWireTransfer(Props: any): VNode { placeholder="amount" pattern={amountRegex} onInput={(e): void => { - transactionData = { - ...transactionData, - amount: e.currentTarget.value, - }; + amountInput = e.currentTarget.value; }} /> <label>{currency}</label> <input @@ -678,6 +684,17 @@ function PaytoWireTransfer(Props: any): VNode { }; }} /> <button onClick={() => { + amountInput = validateAmount(amountInput); + /** + * By invalid amounts, the validator prints error messages + * on the console, and the browser colourizes the amount input + * box to indicate a error. + */ + if (!amountInput) return; + transactionData = { + ...transactionData, + amount: `${currency}:${amountInput}` + }; createTransactionCall( transactionData, backendState, @@ -699,14 +716,13 @@ function TalerWithdrawal(Props: any): VNode { var submitButton = <button onClick={() => { - console.log("Maybe valid amount", submitAmount); - submitAmount = submitAmount.replace(",", "."); // tolerating comma instead of point. - const re = RegExp(amountRegex) - if (!re.test(submitAmount)) { - console.log(`Not withdrawing invalid amount '${submitAmount}'.`); - return; - } - console.log("Valid amount", submitAmount); + submitAmount = validateAmount(submitAmount); + /** + * By invalid amounts, the validator prints error messages + * on the console, and the browser colourizes the amount input + * box to indicate a error. + */ + if (!submitAmount) return; createWithdrawalCall( `${currency}:${submitAmount}`, backendState,