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

---
 .../src/pages/PaytoWireTransferForm.tsx            | 64 ++++++++++++++++------
 1 file changed, 47 insertions(+), 17 deletions(-)

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

diff --git a/packages/demobank-ui/src/pages/PaytoWireTransferForm.tsx b/packages/demobank-ui/src/pages/PaytoWireTransferForm.tsx
index 07b011a00..9698d5b98 100644
--- a/packages/demobank-ui/src/pages/PaytoWireTransferForm.tsx
+++ b/packages/demobank-ui/src/pages/PaytoWireTransferForm.tsx
@@ -17,22 +17,20 @@
 import {
   Amounts,
   buildPayto,
+  HttpStatusCode,
   Logger,
   parsePaytoUri,
   stringifyPaytoUri,
 } from "@gnu-taler/taler-util";
 import {
-  InternationalizationAPI,
   RequestError,
   useTranslationContext,
 } from "@gnu-taler/web-util/lib/index.browser";
 import { h, VNode } from "preact";
-import { StateUpdater, useEffect, useRef, useState } from "preact/hooks";
-import { useBackendContext } from "../context/backend.js";
-import { PageStateType, usePageContext } from "../context/pageState.js";
+import { useEffect, useRef, useState } from "preact/hooks";
+import { PageStateType } from "../context/pageState.js";
 import { useAccessAPI } from "../hooks/access.js";
-import { BackendState } from "../hooks/backend.js";
-import { undefinedIfEmpty } from "../utils.js";
+import { buildRequestErrorMessage, undefinedIfEmpty } from "../utils.js";
 import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
 
 const logger = new Logger("PaytoWireTransferForm");
@@ -184,11 +182,35 @@ export function PaytoWireTransferForm({
                 ibanPayto.params.message = encodeURIComponent(subject);
                 const paytoUri = stringifyPaytoUri(ibanPayto);
 
-                await createTransaction({
-                  paytoUri,
-                  amount: `${currency}:${amount}`,
-                });
-                onSuccess();
+                try {
+                  await createTransaction({
+                    paytoUri,
+                    amount: `${currency}:${amount}`,
+                  });
+                  onSuccess();
+                  setAmount(undefined);
+                  setIban(undefined);
+                  setSubject(undefined);
+                } catch (error) {
+                  if (error instanceof RequestError) {
+                    onError(
+                      buildRequestErrorMessage(i18n, error.cause, {
+                        onClientError: (status) =>
+                          status === HttpStatusCode.BadRequest
+                            ? i18n.str`The request was invalid or the payto://-URI used unacceptable features.`
+                            : undefined,
+                      }),
+                    );
+                  } else {
+                    onError({
+                      title: i18n.str`Operation failed, please report`,
+                      description:
+                        error instanceof Error
+                          ? error.message
+                          : JSON.stringify(error),
+                    });
+                  }
+                }
               }}
             />
             <input
@@ -298,13 +320,21 @@ export function PaytoWireTransferForm({
                 rawPaytoInputSetter(undefined);
               } catch (error) {
                 if (error instanceof RequestError) {
-                  const errorData: SandboxBackend.SandboxError =
-                    error.info.error;
-
+                  onError(
+                    buildRequestErrorMessage(i18n, error.cause, {
+                      onClientError: (status) =>
+                        status === HttpStatusCode.BadRequest
+                          ? i18n.str`The request was invalid or the payto://-URI used unacceptable features.`
+                          : undefined,
+                    }),
+                  );
+                } else {
                   onError({
-                    title: i18n.str`Transfer creation gave response error`,
-                    description: errorData.error.description,
-                    debug: JSON.stringify(errorData),
+                    title: i18n.str`Operation failed, please report`,
+                    description:
+                      error instanceof Error
+                        ? error.message
+                        : JSON.stringify(error),
                   });
                 }
               }
-- 
cgit v1.2.3

