From a8c5a9696c1735a178158cbc9ac4f9bb4b6f013d Mon Sep 17 00:00:00 2001
From: Sebastian <sebasjm@gmail.com>
Date: Wed, 8 Feb 2023 17:41:19 -0300
Subject: impl accout management and refactor

---
 .../demobank-ui/src/pages/PublicHistoriesPage.tsx  | 93 +++++-----------------
 1 file changed, 21 insertions(+), 72 deletions(-)

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

diff --git a/packages/demobank-ui/src/pages/PublicHistoriesPage.tsx b/packages/demobank-ui/src/pages/PublicHistoriesPage.tsx
index 7bf5c41c7..54a77b42a 100644
--- a/packages/demobank-ui/src/pages/PublicHistoriesPage.tsx
+++ b/packages/demobank-ui/src/pages/PublicHistoriesPage.tsx
@@ -15,91 +15,42 @@
  */
 
 import { Logger } from "@gnu-taler/taler-util";
-import { useLocalStorage } from "@gnu-taler/web-util/lib/index.browser";
-import { ComponentChildren, Fragment, h, VNode } from "preact";
-import { route } from "preact-router";
+import {
+  HttpResponsePaginated,
+  useLocalStorage,
+  useTranslationContext,
+} from "@gnu-taler/web-util/lib/index.browser";
+import { Fragment, h, VNode } from "preact";
 import { StateUpdater } from "preact/hooks";
-import useSWR, { SWRConfig } from "swr";
-import { PageStateType, usePageContext } from "../context/pageState.js";
-import { useTranslationContext } from "@gnu-taler/web-util/lib/index.browser";
-import { getBankBackendBaseUrl } from "../utils.js";
-import { BankFrame } from "./BankFrame.js";
 import { Transactions } from "../components/Transactions/index.js";
+import { usePublicAccounts } from "../hooks/access.js";
 
 const logger = new Logger("PublicHistoriesPage");
 
-export function PublicHistoriesPage(): VNode {
-  return (
-    <SWRWithoutCredentials baseUrl={getBankBackendBaseUrl()}>
-      <BankFrame>
-        <PublicHistories />
-      </BankFrame>
-    </SWRWithoutCredentials>
-  );
-}
-
-function SWRWithoutCredentials({
-  baseUrl,
-  children,
-}: {
-  children: ComponentChildren;
-  baseUrl: string;
-}): VNode {
-  logger.trace("Base URL", baseUrl);
-  return (
-    <SWRConfig
-      value={{
-        fetcher: (url: string) =>
-          fetch(baseUrl + url || "").then((r) => {
-            if (!r.ok) throw { status: r.status, json: r.json() };
+// export function PublicHistoriesPage2(): VNode {
+//   return (
+//     <BankFrame>
+//       <PublicHistories />
+//     </BankFrame>
+//   );
+// }
 
-            return r.json();
-          }),
-      }}
-    >
-      {children as any}
-    </SWRConfig>
-  );
+interface Props {
+  onLoadNotOk: <T, E>(error: HttpResponsePaginated<T, E>) => VNode;
 }
 
 /**
  * Show histories of public accounts.
  */
-function PublicHistories(): VNode {
-  const { pageState, pageStateSetter } = usePageContext();
+export function PublicHistoriesPage({ onLoadNotOk }: Props): VNode {
   const [showAccount, setShowAccount] = useShowPublicAccount();
-  const { data, error } = useSWR("access-api/public-accounts");
   const { i18n } = useTranslationContext();
 
-  if (typeof error !== "undefined") {
-    switch (error.status) {
-      case 404:
-        logger.error("public accounts: 404", error);
-        route("/account");
-        pageStateSetter((prevState: PageStateType) => ({
-          ...prevState,
+  const result = usePublicAccounts();
+  if (!result.ok) return onLoadNotOk(result);
 
-          error: {
-            title: i18n.str`List of public accounts was not found.`,
-            debug: JSON.stringify(error),
-          },
-        }));
-        break;
-      default:
-        logger.error("public accounts: non-404 error", error);
-        route("/account");
-        pageStateSetter((prevState: PageStateType) => ({
-          ...prevState,
+  const { data } = result;
 
-          error: {
-            title: i18n.str`List of public accounts could not be retrieved.`,
-            debug: JSON.stringify(error),
-          },
-        }));
-        break;
-    }
-  }
-  if (!data) return <p>Waiting public accounts list...</p>;
   const txs: Record<string, h.JSX.Element> = {};
   const accountsBar = [];
 
@@ -133,9 +84,7 @@ function PublicHistories(): VNode {
         </a>
       </li>,
     );
-    txs[account.accountLabel] = (
-      <Transactions accountLabel={account.accountLabel} pageNumber={0} />
-    );
+    txs[account.accountLabel] = <Transactions account={account.accountLabel} />;
   }
 
   return (
-- 
cgit v1.2.3

