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

---
 .../demobank-ui/src/pages/WithdrawalQRCode.tsx     | 75 +++++++++++++---------
 1 file changed, 44 insertions(+), 31 deletions(-)

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

diff --git a/packages/demobank-ui/src/pages/WithdrawalQRCode.tsx b/packages/demobank-ui/src/pages/WithdrawalQRCode.tsx
index 1a4157d06..9c5f83eca 100644
--- a/packages/demobank-ui/src/pages/WithdrawalQRCode.tsx
+++ b/packages/demobank-ui/src/pages/WithdrawalQRCode.tsx
@@ -14,30 +14,35 @@
  GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
  */
 
-import { Logger, parseWithdrawUri } from "@gnu-taler/taler-util";
 import {
+  HttpStatusCode,
+  Logger,
+  WithdrawUriResult,
+} from "@gnu-taler/taler-util";
+import {
+  ErrorType,
   HttpResponsePaginated,
   useTranslationContext,
 } from "@gnu-taler/web-util/lib/index.browser";
 import { Fragment, h, VNode } from "preact";
 import { Loading } from "../components/Loading.js";
-import { PageStateType } from "../context/pageState.js";
+import {
+  ObservedStateType,
+  notifyError,
+  notifyInfo,
+} from "../context/pageState.js";
 import { useWithdrawalDetails } from "../hooks/access.js";
 import { QrCodeSection } from "./QrCodeSection.js";
 import { WithdrawalConfirmationQuestion } from "./WithdrawalConfirmationQuestion.js";
+import { handleNotOkResult } from "./HomePage.js";
 
 const logger = new Logger("WithdrawalQRCode");
 
 interface Props {
-  account: string;
-  withdrawalId: string;
-  talerWithdrawUri: string;
-  onError: (e: PageStateType["error"]) => void;
+  withdrawUri: WithdrawUriResult;
   onAborted: () => void;
   onConfirmed: () => void;
-  onLoadNotOk: <T>(
-    error: HttpResponsePaginated<T, SandboxBackend.SandboxError>,
-  ) => VNode;
+  onLoadNotOk: () => void;
 }
 /**
  * Offer the QR code (and a clickable taler://-link) to
@@ -45,43 +50,46 @@ interface Props {
  * the bank.  Poll the backend until such operation is done.
  */
 export function WithdrawalQRCode({
-  account,
-  withdrawalId,
-  talerWithdrawUri,
+  withdrawUri,
   onConfirmed,
   onAborted,
-  onError,
   onLoadNotOk,
 }: Props): VNode {
   const { i18n } = useTranslationContext();
-
-  const result = useWithdrawalDetails(account, withdrawalId);
+  const result = useWithdrawalDetails(withdrawUri.withdrawalOperationId);
   if (!result.ok) {
-    return onLoadNotOk(result);
+    if (result.loading) {
+      return <Loading />;
+    }
+    if (
+      result.type === ErrorType.CLIENT &&
+      result.status === HttpStatusCode.NotFound
+    ) {
+      return <div>operation not found</div>;
+    }
+    console.log("result", result);
+    onLoadNotOk();
+    return handleNotOkResult(i18n)(result);
   }
   const { data } = result;
 
   logger.trace("withdrawal status", data);
-  if (data.aborted) {
+  if (data.aborted || data.confirmation_done) {
     // signal that this withdrawal is aborted
     // will redirect to account info
+    notifyInfo(i18n.str`Operation was completed from other session`);
     onAborted();
     return <Loading />;
   }
 
-  const parsedUri = parseWithdrawUri(talerWithdrawUri);
-  if (!parsedUri) {
-    onError({
-      title: i18n.str`The Withdrawal URI is not valid: "${talerWithdrawUri}"`,
-    });
-    return <Loading />;
-  }
-
   if (!data.selection_done) {
     return (
       <QrCodeSection
-        talerWithdrawUri={talerWithdrawUri}
-        onAborted={onAborted}
+        withdrawUri={withdrawUri}
+        onAborted={() => {
+          notifyInfo(i18n.str`Operation canceled`);
+          onAborted();
+        }}
       />
     );
   }
@@ -90,10 +98,15 @@ export function WithdrawalQRCode({
   // user to authorize the operation (here CAPTCHA).
   return (
     <WithdrawalConfirmationQuestion
-      withdrawalId={parsedUri.withdrawalOperationId}
-      onError={onError}
-      onConfirmed={onConfirmed}
-      onAborted={onAborted}
+      withdrawUri={withdrawUri}
+      onConfirmed={() => {
+        notifyInfo(i18n.str`Operation confirmed`);
+        onConfirmed();
+      }}
+      onAborted={() => {
+        notifyInfo(i18n.str`Operation canceled`);
+        onAborted();
+      }}
     />
   );
 }
-- 
cgit v1.2.3

