From 366cccb8fcae6a9971a1e8a9143d821e289339d1 Mon Sep 17 00:00:00 2001
From: Sebastian <sebasjm@gmail.com>
Date: Thu, 19 Oct 2023 02:55:57 -0300
Subject: integrate bank into the new taler-util API

---
 packages/demobank-ui/src/pages/admin/Account.tsx | 37 ++++++++++++++++--------
 1 file changed, 25 insertions(+), 12 deletions(-)

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

diff --git a/packages/demobank-ui/src/pages/admin/Account.tsx b/packages/demobank-ui/src/pages/admin/Account.tsx
index 676fc43d0..bf2fa86f0 100644
--- a/packages/demobank-ui/src/pages/admin/Account.tsx
+++ b/packages/demobank-ui/src/pages/admin/Account.tsx
@@ -1,10 +1,13 @@
-import { Amounts } from "@gnu-taler/taler-util";
-import { PaytoWireTransferForm } from "../PaytoWireTransferForm.js";
-import { handleNotOkResult } from "../HomePage.js";
-import { useAccountDetails } from "../../hooks/access.js";
-import { useBackendContext } from "../../context/backend.js";
+import { Amounts, TalerError } from "@gnu-taler/taler-util";
 import { notifyInfo, useTranslationContext } from "@gnu-taler/web-util/browser";
-import { Fragment, h, VNode } from "preact";
+import { Fragment, VNode, h } from "preact";
+import { ErrorLoading } from "../../components/ErrorLoading.js";
+import { Loading } from "../../components/Loading.js";
+import { useBackendContext } from "../../context/backend.js";
+import { useAccountDetails } from "../../hooks/access.js";
+import { assertUnreachable } from "../HomePage.js";
+import { LoginForm } from "../LoginForm.js";
+import { PaytoWireTransferForm } from "../PaytoWireTransferForm.js";
 
 export function AdminAccount({ onRegister }: { onRegister: () => void }): VNode {
   const { i18n } = useTranslationContext();
@@ -12,15 +15,25 @@ export function AdminAccount({ onRegister }: { onRegister: () => void }): VNode
   const account = r.state.status !== "loggedOut" ? r.state.username : "admin";
   const result = useAccountDetails(account);
 
-  if (!result.ok) {
-    return handleNotOkResult(i18n)(result);
+  if (!result) {
+    return <Loading />
+  }
+  if (result instanceof TalerError) {
+    return <ErrorLoading error={result} />
   }
-  const { data } = result;
+  if (result.type === "fail") {
+    switch (result.case) {
+      case "unauthorized": return <LoginForm reason="forbidden" onRegister={onRegister} />
+      case "not-found": return <LoginForm reason="not-found" onRegister={onRegister} />
+      default: assertUnreachable(result)
+    }
+  }
+  const { body: data } = result;
 
   const balance = Amounts.parseOrThrow(data.balance.amount);
-  const balanceIsDebit = result.data.balance.credit_debit_indicator == "debit";
-  
-  const debitThreshold = Amounts.parseOrThrow(result.data.debit_threshold);
+  const balanceIsDebit = data.balance.credit_debit_indicator == "debit";
+
+  const debitThreshold = Amounts.parseOrThrow(data.debit_threshold);
   const limit = balanceIsDebit
     ? Amounts.sub(debitThreshold, balance).amount
     : Amounts.add(balance, debitThreshold).amount;
-- 
cgit v1.2.3

