From b72729f06535f12af974035b141a30320e75575c Mon Sep 17 00:00:00 2001
From: Sebastian <sebasjm@gmail.com>
Date: Fri, 10 Mar 2023 13:13:51 -0300
Subject: admin is allow to make wire transfers

---
 packages/demobank-ui/src/pages/AdminPage.tsx | 62 +++++++++++++++++++++++++++-
 1 file changed, 61 insertions(+), 1 deletion(-)

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

diff --git a/packages/demobank-ui/src/pages/AdminPage.tsx b/packages/demobank-ui/src/pages/AdminPage.tsx
index 58b048b83..3dd34d251 100644
--- a/packages/demobank-ui/src/pages/AdminPage.tsx
+++ b/packages/demobank-ui/src/pages/AdminPage.tsx
@@ -28,7 +28,12 @@ import {
 import { Fragment, h, VNode } from "preact";
 import { useState } from "preact/hooks";
 import { Cashouts } from "../components/Cashouts/index.js";
-import { ErrorMessage, usePageContext } from "../context/pageState.js";
+import { useBackendContext } from "../context/backend.js";
+import {
+  ErrorMessage,
+  PageStateType,
+  usePageContext,
+} from "../context/pageState.js";
 import { useAccountDetails } from "../hooks/access.js";
 import {
   useBusinessAccountDetails,
@@ -45,6 +50,8 @@ import {
 } from "../utils.js";
 import { ErrorBannerFloat } from "./BankFrame.js";
 import { ShowCashoutDetails } from "./BusinessAccount.js";
+import { PaymentOptions } from "./PaymentOptions.js";
+import { PaytoWireTransferForm } from "./PaytoWireTransferForm.js";
 import { ShowInputErrorLabel } from "./ShowInputErrorLabel.js";
 
 const charset =
@@ -89,6 +96,9 @@ export function AdminPage({ onLoadNotOk }: Props): VNode {
       info,
     }));
   }
+  function saveError(error: PageStateType["error"]): void {
+    pageStateSetter((prev) => ({ ...prev, error }));
+  }
 
   const result = useBusinessAccounts({ account });
   const { i18n } = useTranslationContext();
@@ -204,6 +214,55 @@ export function AdminPage({ onLoadNotOk }: Props): VNode {
       />
     );
   }
+
+  function AdminAccount(): VNode {
+    const r = useBackendContext();
+    const account = r.state.status === "loggedIn" ? r.state.username : "admin";
+    const result = useAccountDetails(account);
+
+    if (!result.ok) {
+      return onLoadNotOk(result);
+    }
+    const { data } = result;
+    const balance = Amounts.parse(data.balance.amount);
+    const balanceIsDebit = data.balance.credit_debit_indicator == "debit";
+    if (!balance) return <Fragment />;
+    return (
+      <Fragment>
+        <section id="assets">
+          <div class="asset-summary">
+            <h2>{i18n.str`Bank account balance`}</h2>
+            {!balance ? (
+              <div class="large-amount" style={{ color: "gray" }}>
+                Waiting server response...
+              </div>
+            ) : (
+              <div class="large-amount amount">
+                {balanceIsDebit ? <b>-</b> : null}
+                <span class="value">{`${Amounts.stringifyValue(
+                  balance,
+                )}`}</span>
+                &nbsp;
+                <span class="currency">{`${balance.currency}`}</span>
+              </div>
+            )}
+          </div>
+        </section>
+        <PaytoWireTransferForm
+          focus
+          currency={balance.currency}
+          onSuccess={() => {
+            pageStateSetter((prevState: PageStateType) => ({
+              ...prevState,
+              info: i18n.str`Wire transfer created!`,
+            }));
+          }}
+          onError={saveError}
+        />
+      </Fragment>
+    );
+  }
+
   return (
     <Fragment>
       <div>
@@ -230,6 +289,7 @@ export function AdminPage({ onLoadNotOk }: Props): VNode {
         </div>
       </p>
 
+      <AdminAccount />
       <section
         id="main"
         style={{ width: 600, marginLeft: "auto", marginRight: "auto" }}
-- 
cgit v1.2.3

