From a3aa7d95d09c83794067c47df4a455c0e3f21806 Mon Sep 17 00:00:00 2001
From: Sebastian <sebasjm@gmail.com>
Date: Fri, 7 Apr 2023 17:30:01 -0300
Subject: anon withdrawal confirmation, and fix error with infinity loop

---
 packages/demobank-ui/src/pages/LoginForm.tsx | 82 ++++++++++++++++++----------
 1 file changed, 54 insertions(+), 28 deletions(-)

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

diff --git a/packages/demobank-ui/src/pages/LoginForm.tsx b/packages/demobank-ui/src/pages/LoginForm.tsx
index 16d2373da..7116e724e 100644
--- a/packages/demobank-ui/src/pages/LoginForm.tsx
+++ b/packages/demobank-ui/src/pages/LoginForm.tsx
@@ -14,6 +14,7 @@
  GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
  */
 
+import { HttpStatusCode } from "@gnu-taler/taler-util";
 import {
   ErrorType,
   useTranslationContext,
@@ -32,7 +33,7 @@ import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
 /**
  * Collect and submit login data.
  */
-export function LoginForm({ onRegister }: { onRegister: () => void }): VNode {
+export function LoginForm({ onRegister }: { onRegister?: () => void }): VNode {
   const backend = useBackendContext();
   const [username, setUsername] = useState<string | undefined>();
   const [password, setPassword] = useState<string | undefined>();
@@ -119,35 +120,60 @@ export function LoginForm({ onRegister }: { onRegister: () => void }): VNode {
               onClick={async (e) => {
                 e.preventDefault();
                 if (!username || !password) return;
-                const { valid, cause } = await testLogin(username, password);
-                if (valid) {
+                const testResult = await testLogin(username, password);
+                if (testResult.valid) {
                   backend.logIn({ username, password });
                 } else {
-                  switch (cause) {
-                    case ErrorType.CLIENT: {
-                      saveError({
-                        title: i18n.str`Wrong credentials or username`,
-                      });
-                      break;
-                    }
-                    case ErrorType.SERVER: {
-                      saveError({
-                        title: i18n.str`Server had a problem, try again later or report.`,
-                      });
-                      break;
-                    }
-                    case ErrorType.TIMEOUT: {
-                      saveError({
-                        title: i18n.str`Could not reach the server, please report.`,
-                      });
-                      break;
-                    }
-                    default: {
-                      saveError({
-                        title: i18n.str`Unexpected error, please report.`,
-                      });
-                      break;
+                  if (testResult.requestError) {
+                    const { cause } = testResult;
+                    switch (cause.type) {
+                      case ErrorType.CLIENT: {
+                        if (cause.status === HttpStatusCode.Unauthorized) {
+                          saveError({
+                            title: i18n.str`Wrong credentials for "${username}"`,
+                          });
+                        }
+                        if (cause.status === HttpStatusCode.NotFound) {
+                          saveError({
+                            title: i18n.str`Account not found`,
+                          });
+                        } else {
+                          saveError({
+                            title: i18n.str`Could not load due to a client error`,
+                            description: cause.payload.error.description,
+                            debug: JSON.stringify(cause.payload),
+                          });
+                        }
+                        break;
+                      }
+                      case ErrorType.SERVER: {
+                        saveError({
+                          title: i18n.str`Server had a problem, try again later or report.`,
+                          description: cause.payload.error.description,
+                          debug: JSON.stringify(cause.payload),
+                        });
+                        break;
+                      }
+                      case ErrorType.TIMEOUT: {
+                        saveError({
+                          title: i18n.str`Request timeout, try again later.`,
+                        });
+                        break;
+                      }
+                      default: {
+                        saveError({
+                          title: i18n.str`Unexpected error, please report.`,
+                          description: `Diagnostic from ${cause.info?.url} is "${cause.message}"`,
+                          debug: JSON.stringify(cause),
+                        });
+                        break;
+                      }
                     }
+                  } else {
+                    saveError({
+                      title: i18n.str`Unexpected error, please report.`,
+                      debug: JSON.stringify(testResult.error),
+                    });
                   }
                   backend.logOut();
                 }
@@ -158,7 +184,7 @@ export function LoginForm({ onRegister }: { onRegister: () => void }): VNode {
               {i18n.str`Login`}
             </button>
 
-            {bankUiSettings.allowRegistrations ? (
+            {bankUiSettings.allowRegistrations && onRegister ? (
               <button
                 class="pure-button pure-button-secondary btn-cancel"
                 onClick={(e) => {
-- 
cgit v1.2.3

