From 9922192b0dba2e479b5af3e29c1d44b98e4d29d7 Mon Sep 17 00:00:00 2001
From: Sebastian <sebasjm@gmail.com>
Date: Tue, 28 Feb 2023 19:03:43 -0300
Subject: fix #7729

---
 .../demobank-ui/src/pages/WalletWithdrawForm.tsx   | 111 ++++-----------------
 1 file changed, 17 insertions(+), 94 deletions(-)

(limited to 'packages/demobank-ui/src/pages/WalletWithdrawForm.tsx')

diff --git a/packages/demobank-ui/src/pages/WalletWithdrawForm.tsx b/packages/demobank-ui/src/pages/WalletWithdrawForm.tsx
index 02b389c6c..c1ad2f0cf 100644
--- a/packages/demobank-ui/src/pages/WalletWithdrawForm.tsx
+++ b/packages/demobank-ui/src/pages/WalletWithdrawForm.tsx
@@ -14,16 +14,16 @@
  GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
  */
 
-import { Amounts, Logger } from "@gnu-taler/taler-util";
+import { Amounts, HttpStatusCode, Logger } from "@gnu-taler/taler-util";
 import {
   RequestError,
   useTranslationContext,
 } from "@gnu-taler/web-util/lib/index.browser";
 import { h, VNode } from "preact";
 import { useEffect, useRef, useState } from "preact/hooks";
-import { PageStateType, usePageContext } from "../context/pageState.js";
+import { PageStateType } from "../context/pageState.js";
 import { useAccessAPI } from "../hooks/access.js";
-import { undefinedIfEmpty } from "../utils.js";
+import { buildRequestErrorMessage, undefinedIfEmpty } from "../utils.js";
 import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
 
 const logger = new Logger("WalletWithdrawForm");
@@ -127,16 +127,21 @@ export function WalletWithdrawForm({
                 onSuccess(result.data);
               } catch (error) {
                 if (error instanceof RequestError) {
+                  onError(
+                    buildRequestErrorMessage(i18n, error.cause, {
+                      onClientError: (status) =>
+                        status === HttpStatusCode.Forbidden
+                          ? i18n.str`The operation was rejected due to insufficient funds`
+                          : undefined,
+                    }),
+                  );
+                } else {
                   onError({
-                    title: i18n.str`Could not create withdrawal operation`,
-                    description: (error as any).error.description,
-                    debug: JSON.stringify(error),
-                  });
-                }
-                if (error instanceof Error) {
-                  onError({
-                    title: i18n.str`Something when wrong trying to start the withdrawal`,
-                    description: error.message,
+                    title: i18n.str`Operation failed, please report`,
+                    description:
+                      error instanceof Error
+                        ? error.message
+                        : JSON.stringify(error),
                   });
                 }
               }
@@ -147,85 +152,3 @@ export function WalletWithdrawForm({
     </form>
   );
 }
-
-// /**
-//  * This function creates a withdrawal operation via the Access API.
-//  *
-//  * After having successfully created the withdrawal operation, the
-//  * user should receive a QR code of the "taler://withdraw/" type and
-//  * supposed to scan it with their phone.
-//  *
-//  * TODO: (1) after the scan, the page should refresh itself and inform
-//  * the user about the operation's outcome.  (2) use POST helper.  */
-// async function createWithdrawalCall(
-//   amount: string,
-//   backendState: BackendState,
-//   pageStateSetter: StateUpdater<PageStateType>,
-//   i18n: InternationalizationAPI,
-// ): Promise<void> {
-//   if (backendState?.status === "loggedOut") {
-//     logger.error("Page has a problem: no credentials found in the state.");
-//     pageStateSetter((prevState) => ({
-//       ...prevState,
-
-//       error: {
-//         title: i18n.str`No credentials given.`,
-//       },
-//     }));
-//     return;
-//   }
-
-//   let res: Response;
-//   try {
-//     const { username, password } = backendState;
-//     const headers = prepareHeaders(username, password);
-
-//     // Let bank generate withdraw URI:
-//     const url = new URL(
-//       `access-api/accounts/${backendState.username}/withdrawals`,
-//       backendState.url,
-//     );
-//     res = await fetch(url.href, {
-//       method: "POST",
-//       headers,
-//       body: JSON.stringify({ amount }),
-//     });
-//   } catch (error) {
-//     logger.trace("Could not POST withdrawal request to the bank", error);
-//     pageStateSetter((prevState) => ({
-//       ...prevState,
-
-//       error: {
-//         title: i18n.str`Could not create withdrawal operation`,
-//         description: (error as any).error.description,
-//         debug: JSON.stringify(error),
-//       },
-//     }));
-//     return;
-//   }
-//   if (!res.ok) {
-//     const response = await res.json();
-//     logger.error(
-//       `Withdrawal creation gave response error: ${response} (${res.status})`,
-//     );
-//     pageStateSetter((prevState) => ({
-//       ...prevState,
-
-//       error: {
-//         title: i18n.str`Withdrawal creation gave response error`,
-//         description: response.error.description,
-//         debug: JSON.stringify(response),
-//       },
-//     }));
-//     return;
-//   }
-
-//   logger.trace("Withdrawal operation created!");
-//   const resp = await res.json();
-//   pageStateSetter((prevState: PageStateType) => ({
-//     ...prevState,
-//     withdrawalInProgress: true,
-//     talerWithdrawUri: resp.taler_withdraw_uri,
-//     withdrawalId: resp.withdrawal_id,
-//   }));
-// }
-- 
cgit v1.2.3

