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

---
 .../src/ApplicationReadyRoutes.tsx                 | 86 ++++++++++++++--------
 1 file changed, 56 insertions(+), 30 deletions(-)

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

diff --git a/packages/merchant-backoffice-ui/src/ApplicationReadyRoutes.tsx b/packages/merchant-backoffice-ui/src/ApplicationReadyRoutes.tsx
index 277c2b176..46dea98e3 100644
--- a/packages/merchant-backoffice-ui/src/ApplicationReadyRoutes.tsx
+++ b/packages/merchant-backoffice-ui/src/ApplicationReadyRoutes.tsx
@@ -22,7 +22,7 @@ import { ErrorType, useTranslationContext } from "@gnu-taler/web-util/browser";
 import { createHashHistory } from "history";
 import { Fragment, h, VNode } from "preact";
 import { Router, Route, route } from "preact-router";
-import { useState } from "preact/hooks";
+import { useEffect, useState } from "preact/hooks";
 import {
   NotificationCard,
   NotYetReadyAppMenu,
@@ -35,52 +35,55 @@ import { INSTANCE_ID_LOOKUP } from "./utils/constants.js";
 import { HttpStatusCode } from "@gnu-taler/taler-util";
 import { Settings } from "./paths/settings/index.js";
 
+/**
+ * Check if admin against /management/instances
+ * @returns 
+ */
 export function ApplicationReadyRoutes(): VNode {
   const { i18n } = useTranslationContext();
+  const [unauthorized, setUnauthorized] = useState(false)
   const {
     url: backendURL,
-    updateLoginStatus,
-    clearAllTokens,
+    updateLoginStatus: updateLoginStatus2,
   } = useBackendContext();
 
+  function updateLoginStatus(url: string, token: string | undefined) {
+    console.log("updateing", url, token)
+    updateLoginStatus2(url, token)
+    setUnauthorized(false)
+  }
+
   const result = useBackendInstancesTestForAdmin();
 
   const clearTokenAndGoToRoot = () => {
-    clearAllTokens();
     route("/");
   };
   const [showSettings, setShowSettings] = useState(false)
+  // useEffect(() => {
+  //   setUnauthorized(FF)
+  // }, [FF])
+  const unauthorizedAdmin = !result.loading && !result.ok && result.type === ErrorType.CLIENT && result.status === HttpStatusCode.Unauthorized
 
   if (showSettings) {
     return <Fragment>
-      <NotYetReadyAppMenu onShowSettings={() => setShowSettings(true)} title="UI Settings" onLogout={clearTokenAndGoToRoot} />
-      <Settings/>
+      <NotYetReadyAppMenu onShowSettings={() => setShowSettings(true)} title="UI Settings" onLogout={clearTokenAndGoToRoot} isPasswordOk={false} />
+      <Settings />
     </Fragment>
   }
-  if (result.loading) return <NotYetReadyAppMenu onShowSettings={() => setShowSettings(true)} title="Loading..." />;
 
-  let admin = true;
-  let instanceNameByBackendURL;
+  if (result.loading) {
+    return <NotYetReadyAppMenu onShowSettings={() => setShowSettings(true)} title="Loading..." isPasswordOk={false} />;
+  }
 
-  if (!result.ok) {
-    if (
-      result.type === ErrorType.CLIENT &&
-      result.status === HttpStatusCode.Unauthorized
-    ) {
-      return (
-        <Fragment>
-          <NotYetReadyAppMenu onShowSettings={() => setShowSettings(true)} title="Login" onLogout={clearTokenAndGoToRoot} />
-          <NotificationCard
-            notification={{
-              message: i18n.str`Access denied`,
-              description: i18n.str`Check your token is valid`,
-              type: "ERROR",
-            }}
-          />
-          <LoginPage onConfirm={updateLoginStatus} />
-        </Fragment>
-      );
-    }
+  let admin = result.ok || unauthorizedAdmin;
+  let instanceNameByBackendURL: string | undefined;
+
+  if (!admin) {
+    // * the testing against admin endpoint failed and it's not 
+    // an authorization problem
+    // * merchant backend will return this SPA under the main
+    // endpoint or /instance/<id> endpoint
+    // => trying to infer the instance id
     const path = new URL(backendURL).pathname;
     const match = INSTANCE_ID_LOOKUP.exec(path);
     if (!match || !match[1]) {
@@ -89,7 +92,7 @@ export function ApplicationReadyRoutes(): VNode {
       // does not match our pattern
       return (
         <Fragment>
-          <NotYetReadyAppMenu onShowSettings={() => setShowSettings(true)} title="Error" onLogout={clearTokenAndGoToRoot} />
+          <NotYetReadyAppMenu onShowSettings={() => setShowSettings(true)} title="Error" onLogout={clearTokenAndGoToRoot} isPasswordOk={false} />
           <NotificationCard
             notification={{
               message: i18n.str`Couldn't access the server.`,
@@ -102,10 +105,24 @@ export function ApplicationReadyRoutes(): VNode {
       );
     }
 
-    admin = false;
     instanceNameByBackendURL = match[1];
   }
 
+  console.log(unauthorized, unauthorizedAdmin)
+  if (unauthorized || unauthorizedAdmin) {
+    return <Fragment>
+      <NotYetReadyAppMenu onShowSettings={() => setShowSettings(true)} title="Login" onLogout={clearTokenAndGoToRoot} isPasswordOk={false} />
+      <NotificationCard
+        notification={{
+          message: i18n.str`Access denied`,
+          description: i18n.str`Check your token is valid`,
+          type: "ERROR",
+        }}
+      />
+      <LoginPage onConfirm={updateLoginStatus} />
+    </Fragment>
+  }
+
   const history = createHashHistory();
   return (
     <Router history={history}>
@@ -113,6 +130,11 @@ export function ApplicationReadyRoutes(): VNode {
         default
         component={DefaultMainRoute}
         admin={admin}
+        onUnauthorized={() => setUnauthorized(true)}
+        onLoginPass={() => {
+          console.log("ahora si")
+          setUnauthorized(false)
+        }}
         instanceNameByBackendURL={instanceNameByBackendURL}
       />
     </Router>
@@ -122,6 +144,8 @@ export function ApplicationReadyRoutes(): VNode {
 function DefaultMainRoute({
   instance,
   admin,
+  onUnauthorized,
+  onLoginPass,
   instanceNameByBackendURL,
   url, //from preact-router
 }: any): VNode {
@@ -133,6 +157,8 @@ function DefaultMainRoute({
     <InstanceRoutes
       admin={admin}
       path={url}
+      onUnauthorized={onUnauthorized}
+      onLoginPass={onLoginPass}
       id={instanceName}
       setInstanceName={setInstanceName}
     />
-- 
cgit v1.2.3

