From e1d86816a7c07cb8ca2d54676d5cdbbe513f2ba7 Mon Sep 17 00:00:00 2001
From: Sebastian <sebasjm@gmail.com>
Date: Mon, 4 Sep 2023 14:17:55 -0300
Subject: backoffcie new version, lot of changes

---
 .../merchant-backoffice-ui/src/InstanceRoutes.tsx  | 219 +++++++++++++++------
 1 file changed, 160 insertions(+), 59 deletions(-)

(limited to 'packages/merchant-backoffice-ui/src/InstanceRoutes.tsx')

diff --git a/packages/merchant-backoffice-ui/src/InstanceRoutes.tsx b/packages/merchant-backoffice-ui/src/InstanceRoutes.tsx
index 1547442ea..4a4b3fee4 100644
--- a/packages/merchant-backoffice-ui/src/InstanceRoutes.tsx
+++ b/packages/merchant-backoffice-ui/src/InstanceRoutes.tsx
@@ -40,6 +40,7 @@ import {
 import { useInstanceKYCDetails } from "./hooks/instance.js";
 import InstanceCreatePage from "./paths/admin/create/index.js";
 import InstanceListPage from "./paths/admin/list/index.js";
+import TokenPage from "./paths/instance/token/index.js";
 import ListKYCPage from "./paths/instance/kyc/list/index.js";
 import OrderCreatePage from "./paths/instance/orders/create/index.js";
 import OrderDetailsPage from "./paths/instance/orders/details/index.js";
@@ -47,6 +48,9 @@ import OrderListPage from "./paths/instance/orders/list/index.js";
 import ProductCreatePage from "./paths/instance/products/create/index.js";
 import ProductListPage from "./paths/instance/products/list/index.js";
 import ProductUpdatePage from "./paths/instance/products/update/index.js";
+import BankAccountCreatePage from "./paths/instance/accounts/create/index.js";
+import BankAccountListPage from "./paths/instance/accounts/list/index.js";
+import BankAccountUpdatePage from "./paths/instance/accounts/update/index.js";
 import ReservesCreatePage from "./paths/instance/reserves/create/index.js";
 import ReservesDetailsPage from "./paths/instance/reserves/details/index.js";
 import ReservesListPage from "./paths/instance/reserves/list/index.js";
@@ -58,6 +62,9 @@ import TemplateUpdatePage from "./paths/instance/templates/update/index.js";
 import WebhookCreatePage from "./paths/instance/webhooks/create/index.js";
 import WebhookListPage from "./paths/instance/webhooks/list/index.js";
 import WebhookUpdatePage from "./paths/instance/webhooks/update/index.js";
+import ValidatorCreatePage from "./paths/instance/validators/create/index.js";
+import ValidatorListPage from "./paths/instance/validators/list/index.js";
+import ValidatorUpdatePage from "./paths/instance/validators/update/index.js";
 import TransferCreatePage from "./paths/instance/transfers/create/index.js";
 import TransferListPage from "./paths/instance/transfers/list/index.js";
 import InstanceUpdatePage, {
@@ -69,11 +76,16 @@ import NotFoundPage from "./paths/notfound/index.js";
 import { Notification } from "./utils/types.js";
 import { MerchantBackend } from "./declaration.js";
 import { Settings } from "./paths/settings/index.js";
+import { dateFormatForSettings, useSettings } from "./hooks/useSettings.js";
 
 export enum InstancePaths {
-  // details = '/',
   error = "/error",
-  update = "/update",
+  server = "/server",
+  token = "/token",
+
+  bank_list = "/bank",
+  bank_update = "/bank/:bid/update",
+  bank_new = "/bank/new",
 
   product_list = "/products",
   product_update = "/product/:pid/update",
@@ -102,11 +114,15 @@ export enum InstancePaths {
   webhooks_update = "/webhooks/:tid/update",
   webhooks_new = "/webhooks/new",
 
-  settings = "/settings",
+  validators_list = "/validators",
+  validators_update = "/validators/:vid/update",
+  validators_new = "/validators/new",
+
+  settings = "/inteface",
 }
 
 // eslint-disable-next-line @typescript-eslint/no-empty-function
-const noop = () => {};
+const noop = () => { };
 
 export enum AdminPaths {
   list_instances = "/instances",
@@ -118,6 +134,8 @@ export interface Props {
   id: string;
   admin?: boolean;
   path: string;
+  onUnauthorized: () => void;
+  onLoginPass: () => void;
   setInstanceName: (s: string) => void;
 }
 
@@ -125,40 +143,29 @@ export function InstanceRoutes({
   id,
   admin,
   path,
+  onUnauthorized,
+  onLoginPass,
   setInstanceName,
 }: Props): VNode {
-  const [_, updateDefaultToken] = useBackendDefaultToken();
+  const [defaultToken, updateDefaultToken] = useBackendDefaultToken();
   const [token, updateToken] = useBackendInstanceToken(id);
-  const {
-    updateLoginStatus: changeBackend,
-    addTokenCleaner,
-    clearAllTokens,
-  } = useBackendContext();
-  const cleaner = useCallback(() => {
-    updateToken(undefined);
-  }, [id]);
   const { i18n } = useTranslationContext();
 
   type GlobalNotifState = (Notification & { to: string }) | undefined;
   const [globalNotification, setGlobalNotification] =
     useState<GlobalNotifState>(undefined);
 
-  useEffect(() => {
-    addTokenCleaner(cleaner);
-  }, [addTokenCleaner, cleaner]);
-
   const changeToken = (token?: string) => {
     if (admin) {
       updateToken(token);
     } else {
       updateDefaultToken(token);
     }
+    onLoginPass()
   };
-  const updateLoginStatus = (url: string, token?: string) => {
-    changeBackend(url);
-    if (!token) return;
-    changeToken(token);
-  };
+  // const updateLoginStatus = (url: string, token?: string) => {
+  //   changeToken(token);
+  // };
 
   const value = useMemo(
     () => ({ id, token, admin, changeToken }),
@@ -192,18 +199,17 @@ export function InstanceRoutes({
     };
   }
 
-  const LoginPageAccessDenied = () => (
-    <Fragment>
-      <NotificationCard
-        notification={{
-          message: i18n.str`Access denied`,
-          description: i18n.str`The access token provided is invalid.`,
-          type: "ERROR",
-        }}
-      />
-      <LoginPage onConfirm={updateLoginStatus} />
-    </Fragment>
-  );
+  // const LoginPageAccessDeniend = onUnauthorized
+  const LoginPageAccessDenied = () => {
+    onUnauthorized()
+    return <NotificationCard
+      notification={{
+        message: i18n.str`Access denied`,
+        description: i18n.str`Redirecting to login page.`,
+        type: "ERROR",
+      }}
+    />
+  }
 
   function IfAdminCreateDefaultOr<T>(Next: FunctionComponent<any>) {
     return function IfAdminCreateDefaultOrImpl(props?: T) {
@@ -234,8 +240,10 @@ export function InstanceRoutes({
   }
 
   const clearTokenAndGoToRoot = () => {
-    clearAllTokens();
     route("/");
+    // clear all tokens
+    updateToken(undefined)
+    updateDefaultToken(undefined)
   };
 
   return (
@@ -244,11 +252,12 @@ export function InstanceRoutes({
         instance={id}
         admin={admin}
         onShowSettings={() => {
-          route("/settings")
+          route("/inteface")
         }}
         path={path}
         onLogout={clearTokenAndGoToRoot}
         setInstanceName={setInstanceName}
+        isPasswordOk={defaultToken !== undefined}
       />
       <KycBanner />
       <NotificationCard notification={globalNotification} />
@@ -308,7 +317,7 @@ export function InstanceRoutes({
          * Update instance page
          */}
         <Route
-          path={InstancePaths.update}
+          path={InstancePaths.server}
           component={InstanceUpdatePage}
           onBack={() => {
             route(`/`);
@@ -321,6 +330,19 @@ export function InstanceRoutes({
           onUnauthorized={LoginPageAccessDenied}
           onLoadError={ServerErrorRedirectTo(InstancePaths.error)}
         />
+        {/**
+         * Update instance page
+         */}
+        <Route
+          path={InstancePaths.token}
+          component={TokenPage}
+          onChange={() => {
+            route(`/`);
+          }}
+          onNotFound={IfAdminCreateDefaultOr(NotFoundPage)}
+          onUnauthorized={LoginPageAccessDenied}
+          onLoadError={ServerErrorRedirectTo(InstancePaths.error)}
+        />
         {/**
          * Product pages
          */}
@@ -328,7 +350,7 @@ export function InstanceRoutes({
           path={InstancePaths.product_list}
           component={ProductListPage}
           onUnauthorized={LoginPageAccessDenied}
-          onLoadError={ServerErrorRedirectTo(InstancePaths.update)}
+          onLoadError={ServerErrorRedirectTo(InstancePaths.server)}
           onCreate={() => {
             route(InstancePaths.product_new);
           }}
@@ -360,6 +382,45 @@ export function InstanceRoutes({
             route(InstancePaths.product_list);
           }}
         />
+        {/**
+         * Bank pages
+         */}
+        <Route
+          path={InstancePaths.bank_list}
+          component={BankAccountListPage}
+          onUnauthorized={LoginPageAccessDenied}
+          onLoadError={ServerErrorRedirectTo(InstancePaths.server)}
+          onCreate={() => {
+            route(InstancePaths.bank_new);
+          }}
+          onSelect={(id: string) => {
+            route(InstancePaths.bank_update.replace(":bid", id));
+          }}
+          onNotFound={IfAdminCreateDefaultOr(NotFoundPage)}
+        />
+        <Route
+          path={InstancePaths.bank_update}
+          component={BankAccountUpdatePage}
+          onUnauthorized={LoginPageAccessDenied}
+          onLoadError={ServerErrorRedirectTo(InstancePaths.product_list)}
+          onConfirm={() => {
+            route(InstancePaths.bank_list);
+          }}
+          onBack={() => {
+            route(InstancePaths.bank_list);
+          }}
+          onNotFound={IfAdminCreateDefaultOr(NotFoundPage)}
+        />
+        <Route
+          path={InstancePaths.bank_new}
+          component={BankAccountCreatePage}
+          onConfirm={() => {
+            route(InstancePaths.bank_list);
+          }}
+          onBack={() => {
+            route(InstancePaths.bank_list);
+          }}
+        />
         {/**
          * Order pages
          */}
@@ -373,7 +434,7 @@ export function InstanceRoutes({
             route(InstancePaths.order_details.replace(":oid", id));
           }}
           onUnauthorized={LoginPageAccessDenied}
-          onLoadError={ServerErrorRedirectTo(InstancePaths.update)}
+          onLoadError={ServerErrorRedirectTo(InstancePaths.server)}
           onNotFound={IfAdminCreateDefaultOr(NotFoundPage)}
         />
         <Route
@@ -389,8 +450,8 @@ export function InstanceRoutes({
         <Route
           path={InstancePaths.order_new}
           component={OrderCreatePage}
-          onConfirm={() => {
-            route(InstancePaths.order_list);
+          onConfirm={(orderId: string) => {
+            route(InstancePaths.order_details.replace(":oid", orderId));
           }}
           onBack={() => {
             route(InstancePaths.order_list);
@@ -404,7 +465,7 @@ export function InstanceRoutes({
           component={TransferListPage}
           onUnauthorized={LoginPageAccessDenied}
           onNotFound={IfAdminCreateDefaultOr(NotFoundPage)}
-          onLoadError={ServerErrorRedirectTo(InstancePaths.update)}
+          onLoadError={ServerErrorRedirectTo(InstancePaths.server)}
           onCreate={() => {
             route(InstancePaths.transfers_new);
           }}
@@ -427,7 +488,7 @@ export function InstanceRoutes({
           component={WebhookListPage}
           onUnauthorized={LoginPageAccessDenied}
           onNotFound={IfAdminCreateDefaultOr(NotFoundPage)}
-          onLoadError={ServerErrorRedirectTo(InstancePaths.update)}
+          onLoadError={ServerErrorRedirectTo(InstancePaths.server)}
           onCreate={() => {
             route(InstancePaths.webhooks_new);
           }}
@@ -458,6 +519,45 @@ export function InstanceRoutes({
             route(InstancePaths.webhooks_list);
           }}
         />
+        {/**
+         * Validator pages
+         */}
+        <Route
+          path={InstancePaths.validators_list}
+          component={ValidatorListPage}
+          onUnauthorized={LoginPageAccessDenied}
+          onNotFound={IfAdminCreateDefaultOr(NotFoundPage)}
+          onLoadError={ServerErrorRedirectTo(InstancePaths.server)}
+          onCreate={() => {
+            route(InstancePaths.validators_new);
+          }}
+          onSelect={(id: string) => {
+            route(InstancePaths.validators_update.replace(":vid", id));
+          }}
+        />
+        <Route
+          path={InstancePaths.validators_update}
+          component={ValidatorUpdatePage}
+          onConfirm={() => {
+            route(InstancePaths.validators_list);
+          }}
+          onUnauthorized={LoginPageAccessDenied}
+          onLoadError={ServerErrorRedirectTo(InstancePaths.validators_list)}
+          onNotFound={IfAdminCreateDefaultOr(NotFoundPage)}
+          onBack={() => {
+            route(InstancePaths.validators_list);
+          }}
+        />
+        <Route
+          path={InstancePaths.validators_new}
+          component={ValidatorCreatePage}
+          onConfirm={() => {
+            route(InstancePaths.validators_list);
+          }}
+          onBack={() => {
+            route(InstancePaths.validators_list);
+          }}
+        />
         {/**
          * Templates pages
          */}
@@ -466,7 +566,7 @@ export function InstanceRoutes({
           component={TemplateListPage}
           onUnauthorized={LoginPageAccessDenied}
           onNotFound={IfAdminCreateDefaultOr(NotFoundPage)}
-          onLoadError={ServerErrorRedirectTo(InstancePaths.update)}
+          onLoadError={ServerErrorRedirectTo(InstancePaths.server)}
           onCreate={() => {
             route(InstancePaths.templates_new);
           }}
@@ -535,7 +635,7 @@ export function InstanceRoutes({
           component={ReservesListPage}
           onUnauthorized={LoginPageAccessDenied}
           onNotFound={IfAdminCreateDefaultOr(NotFoundPage)}
-          onLoadError={ServerErrorRedirectTo(InstancePaths.update)}
+          onLoadError={ServerErrorRedirectTo(InstancePaths.server)}
           onSelect={(id: string) => {
             route(InstancePaths.reserves_details.replace(":rid", id));
           }}
@@ -590,7 +690,7 @@ function AdminInstanceUpdatePage({
   const { updateLoginStatus: changeBackend } = useBackendContext();
   const updateLoginStatus = (url: string, token?: string): void => {
     changeBackend(url);
-    if (token) changeToken(token);
+    changeToken(token);
   };
   const value = useMemo(
     () => ({ id, token, admin: true, changeToken }),
@@ -607,20 +707,20 @@ function AdminInstanceUpdatePage({
           const notif =
             error.type === ErrorType.TIMEOUT
               ? {
-                  message: i18n.str`The request to the backend take too long and was cancelled`,
-                  description: i18n.str`Diagnostic from ${error.info.url} is '${error.message}'`,
-                  type: "ERROR" as const,
-                }
+                message: i18n.str`The request to the backend take too long and was cancelled`,
+                description: i18n.str`Diagnostic from ${error.info.url} is '${error.message}'`,
+                type: "ERROR" as const,
+              }
               : {
-                  message: i18n.str`The backend reported a problem: HTTP status #${error.status}`,
-                  description: i18n.str`Diagnostic from ${error.info.url} is '${error.message}'`,
-                  details:
-                    error.type === ErrorType.CLIENT ||
+                message: i18n.str`The backend reported a problem: HTTP status #${error.status}`,
+                description: i18n.str`Diagnostic from ${error.info.url} is '${error.message}'`,
+                details:
+                  error.type === ErrorType.CLIENT ||
                     error.type === ErrorType.SERVER
-                      ? error.payload.detail
-                      : undefined,
-                  type: "ERROR" as const,
-                };
+                    ? error.payload.detail
+                    : undefined,
+                type: "ERROR" as const,
+              };
           return (
             <Fragment>
               <NotificationCard notification={notif} />
@@ -650,7 +750,8 @@ function AdminInstanceUpdatePage({
 function KycBanner(): VNode {
   const kycStatus = useInstanceKYCDetails();
   const { i18n } = useTranslationContext();
-  const today = format(new Date(), "yyyy-MM-dd");
+  const [settings] = useSettings();
+  const today = format(new Date(), dateFormatForSettings(settings));
   const [lastHide, setLastHide] = useLocalStorage("kyc-last-hide");
   const hasBeenHidden = today === lastHide;
   const needsToBeShown = kycStatus.ok && kycStatus.data.type === "redirect";
-- 
cgit v1.2.3

