gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-merchant-backoffice] branch master updated: refactor navigation,


From: gnunet
Subject: [taler-merchant-backoffice] branch master updated: refactor navigation, part 1
Date: Fri, 12 Mar 2021 13:23:15 +0100

This is an automated email from the git hooks/post-receive script.

sebasjm pushed a commit to branch master
in repository merchant-backoffice.

The following commit(s) were added to refs/heads/master by this push:
     new 789de7f  refactor navigation, part 1
789de7f is described below

commit 789de7f8522ca03f8460df0431b4e1a7e792e25f
Author: Sebastian <sebasjm@gmail.com>
AuthorDate: Fri Mar 12 09:23:06 2021 -0300

    refactor navigation, part 1
---
 CHANGELOG.md                                     |  3 +
 packages/frontend/src/AdminRoutes.tsx            |  6 +-
 packages/frontend/src/ApplicationReadyRoutes.tsx | 77 +++++++++++-------------
 packages/frontend/src/InstanceRoutes.tsx         |  5 +-
 packages/frontend/src/components/menu/index.tsx  |  1 -
 packages/frontend/src/context/backend.ts         |  8 +--
 packages/frontend/src/hooks/index.ts             | 12 +++-
 packages/frontend/src/index.tsx                  | 12 +---
 8 files changed, 57 insertions(+), 67 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index f2e3fe2..28448f2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,9 @@ and this project adheres to [Semantic 
Versioning](https://semver.org/spec/v2.0.0
  - notifications should tale place between title and content, and not disapear
  - confirmation page when creating instances
  - if there is enough space for tables in mobile, make the scrollables
+
+ - create default instance if it is not already
+ 
  
 ## [Unreleased]
  - add order section
diff --git a/packages/frontend/src/AdminRoutes.tsx 
b/packages/frontend/src/AdminRoutes.tsx
index b5631f5..b8da7cf 100644
--- a/packages/frontend/src/AdminRoutes.tsx
+++ b/packages/frontend/src/AdminRoutes.tsx
@@ -52,13 +52,10 @@ export enum AdminPaths {
 interface Props {
   pushNotification: (n: Notification) => void;
   instances: MerchantBackend.Instances.Instance[]
-  addTokenCleaner: any;
 }
-export function AdminRoutes({ instances, pushNotification, addTokenCleaner }: 
Props): VNode {
+export function AdminRoutes({ instances, pushNotification }: Props): VNode {
   const i18n = useMessageTemplate();
   
-  // const [token, updateToken] = useBackendInstanceToken(id);
-  // const { changeBackend } = useBackendContext();
   const updateLoginStatus = () => null;
 
   return <Router>
@@ -95,7 +92,6 @@ export function AdminRoutes({ instances, pushNotification, 
addTokenCleaner }: Pr
 
     <Route path={AdminPaths.instance_id_route} component={InstanceRoutes}
       pushNotification={pushNotification}
-      addTokenCleaner={addTokenCleaner}
       parent="/instance/:id"
     />
 
diff --git a/packages/frontend/src/ApplicationReadyRoutes.tsx 
b/packages/frontend/src/ApplicationReadyRoutes.tsx
index 64fbb32..6aff0fb 100644
--- a/packages/frontend/src/ApplicationReadyRoutes.tsx
+++ b/packages/frontend/src/ApplicationReadyRoutes.tsx
@@ -31,12 +31,10 @@ import { AdminRoutes } from './AdminRoutes';
 import { useMessageTemplate } from 'preact-messages';
 interface Props {
   pushNotification: (n: Notification) => void;
-  addTokenCleaner: any;
-  clearAllTokens: () => void;
 }
-export function ApplicationReadyRoutes({ pushNotification, addTokenCleaner, 
clearAllTokens }: Props): VNode {
+export function ApplicationReadyRoutes({ pushNotification }: Props): VNode {
   const i18n = useMessageTemplate();
-  const { url: currentBaseUrl, changeBackend, updateToken } = 
useBackendContext();
+  const { url: currentBaseUrl, changeBackend, updateToken, clearAllTokens } = 
useBackendContext();
 
   const updateLoginStatus = (url: string, token?: string) => {
     changeBackend(url);
@@ -58,22 +56,9 @@ export function ApplicationReadyRoutes({ pushNotification, 
addTokenCleaner, clea
       </Fragment>
     }
     if (list.notfound) {
-      try {
-        const path = new URL(currentBaseUrl).pathname
-        const match = INSTANCE_ID_LOOKUP.exec(path)
-        if (!match || !match[1]) throw new Error(i18n`Could not infer instance 
id from url ${currentBaseUrl}`)
-        return <Fragment>
-          <Menu instance={match[1]} onLogout={() => {
-            clearAllTokens();
-            route('/')
-          }} />
-          <InstanceRoutes
-            id={match[1]}
-            addTokenCleaner={addTokenCleaner}
-            pushNotification={pushNotification}
-          />
-        </Fragment>
-      } catch (e) {
+      const path = new URL(currentBaseUrl).pathname
+      const match = INSTANCE_ID_LOOKUP.exec(path)
+      if (!match || !match[1]) {
         // this should be rare becuase
         // query to /config is ok but the URL
         // doest not match with our pattern
@@ -83,34 +68,44 @@ export function ApplicationReadyRoutes({ pushNotification, 
addTokenCleaner, clea
             route('/')
           }} />
           <LoginPage
-            withMessage={{ message: i18n`Couldnt access the server`, 
description: e.message, type: 'ERROR', }}
+            withMessage={{ message: i18n`Couldnt access the server`, 
description: i18n`Could not infer instance id from url ${currentBaseUrl}`, 
type: 'ERROR', }}
             onConfirm={updateLoginStatus}
           />
         </Fragment>
       }
-    }
-    if (list.error) {
       return <Fragment>
-        <Menu title="Error" />
-        <LoginPage
-          withMessage={{ message: i18n`Couldnt access the server`, 
description: list.error.message, type: 'ERROR', }}
-          onConfirm={updateLoginStatus}
+        <Menu instance={match[1]} onLogout={() => {
+          clearAllTokens();
+          route('/')
+        }} />
+        <InstanceRoutes
+          id={match[1]}
+          pushNotification={pushNotification}
         />
       </Fragment>
-    }
-    
-    // is loading
-    return <Menu />
   }
+  
+  if (list.error) {
+    return <Fragment>
+      <Menu title="Error" />
+      <LoginPage
+        withMessage={{ message: i18n`Couldnt access the server`, description: 
list.error.message, type: 'ERROR', }}
+        onConfirm={updateLoginStatus}
+      />
+    </Fragment>
+  }
+
+  // is loading
+  return <Menu />
+}
 
-  return <Fragment>
-    <Menu onLogout={() => {
-      clearAllTokens();
-      route('/')
-    }} />
-    <AdminRoutes instances={list.data.instances}
-      addTokenCleaner={addTokenCleaner}
-      pushNotification={pushNotification}
-    />
-  </Fragment>
+return <Fragment>
+  <Menu onLogout={() => {
+    clearAllTokens();
+    route('/')
+  }} />
+  <AdminRoutes instances={list.data.instances}
+    pushNotification={pushNotification}
+  />
+</Fragment>
 }
diff --git a/packages/frontend/src/InstanceRoutes.tsx 
b/packages/frontend/src/InstanceRoutes.tsx
index a243fd6..a0db055 100644
--- a/packages/frontend/src/InstanceRoutes.tsx
+++ b/packages/frontend/src/InstanceRoutes.tsx
@@ -71,13 +71,12 @@ export enum InstancePaths {
 export interface Props {
   id: string;
   pushNotification: (n: Notification) => void;
-  addTokenCleaner: any;
   parent?: string;
 }
 
-export function InstanceRoutes({ id, pushNotification, addTokenCleaner, parent 
}: Props): VNode {
+export function InstanceRoutes({ id, pushNotification, parent }: Props): VNode 
{
   const [token, updateToken] = useBackendInstanceToken(id);
-  const { changeBackend } = useBackendContext();
+  const { changeBackend, addTokenCleaner } = useBackendContext();
   const cleaner = useCallback(() => { updateToken(undefined); }, [id]);
   const i18n = useMessageTemplate('');
 
diff --git a/packages/frontend/src/components/menu/index.tsx 
b/packages/frontend/src/components/menu/index.tsx
index f76b07e..b7d0874 100644
--- a/packages/frontend/src/components/menu/index.tsx
+++ b/packages/frontend/src/components/menu/index.tsx
@@ -29,7 +29,6 @@ interface Props {
 }
 
 function getInstanceTitle(path: string, id: string): string {
-  console.log('-->', path, id);
 
   switch (path) {
     case InstancePaths.details: return `${id}`
diff --git a/packages/frontend/src/context/backend.ts 
b/packages/frontend/src/context/backend.ts
index 55eba5d..39955ec 100644
--- a/packages/frontend/src/context/backend.ts
+++ b/packages/frontend/src/context/backend.ts
@@ -22,8 +22,8 @@ export interface BackendContextType {
   triedToLog: boolean;
   changeBackend: (url: string) => void;
   resetBackend: () => void;
-  // clearTokens: () => void;
-  // addTokenCleaner: (c: StateUpdater<string | undefined>) => void;
+  clearAllTokens: () => void;
+  addTokenCleaner: (c: () => void) => void;
   updateToken: (token?:string) => void;
   lang: string;
   setLang: (lang: string) => void;
@@ -47,8 +47,8 @@ const BackendContext = createContext<BackendContextType>({
   triedToLog: false,
   changeBackend: () => null,
   resetBackend: () => null,
-  // clearTokens: () => null,
-  // addTokenCleaner: () => null,
+  clearAllTokens: () => null,
+  addTokenCleaner: () => null,
   updateToken: () => null,
   setLang: () => null,
 })
diff --git a/packages/frontend/src/hooks/index.ts 
b/packages/frontend/src/hooks/index.ts
index 68a3c5b..cab1a05 100644
--- a/packages/frontend/src/hooks/index.ts
+++ b/packages/frontend/src/hooks/index.ts
@@ -19,7 +19,7 @@
 * @author Sebastian Javier Marchano (sebasjm)
 */
 
-import { StateUpdater, useEffect, useState } from "preact/hooks";
+import { StateUpdater, useCallback, useEffect, useState } from "preact/hooks";
 import { ValueOrFunction } from '../utils/types';
 
 
@@ -28,8 +28,16 @@ export function useBackendContextState() {
   const [url, triedToLog, changeBackend, resetBackend] = useBackendURL();
   const [token, updateToken] = useBackendDefaultToken();
 
+  const tokenCleaner = useCallback(() => { updateToken(undefined) }, [])
+  const [cleaners, setCleaners] = useState([tokenCleaner])
+  const addTokenCleaner = (c: () => void) => setCleaners(cs => [...cs, c])
+  const addTokenCleanerMemo = useCallback((c: () => void) => { 
addTokenCleaner(c) }, [tokenCleaner])
+  const clearAllTokens = () => {
+    cleaners.forEach(c => c())
+    resetBackend()
+  }
 
-  return { url, token, triedToLog, changeBackend, updateToken, lang, setLang, 
resetBackend }
+  return { url, token, triedToLog, changeBackend, updateToken, lang, setLang, 
resetBackend, clearAllTokens, addTokenCleaner: addTokenCleanerMemo }
 }
 
 export function useBackendURL(): [string, boolean, StateUpdater<string>, () => 
void] {
diff --git a/packages/frontend/src/index.tsx b/packages/frontend/src/index.tsx
index d7a984b..dd6916d 100644
--- a/packages/frontend/src/index.tsx
+++ b/packages/frontend/src/index.tsx
@@ -63,16 +63,6 @@ function ApplicationStatusRoutes(): VNode {
   const backendConfig = useBackendConfig();
   const i18n = useMessageTemplate()
 
-
-  const tokenCleaner = useCallback(() => { updateToken(undefined) }, [])
-  const [cleaners, setCleaners] = useState([tokenCleaner])
-  const addTokenCleaner = (c: () => void) => setCleaners(cs => [...cs, c])
-  const addTokenCleanerMemo = useCallback((c: () => void) => { 
addTokenCleaner(c) }, [tokenCleaner])
-  const clearAllTokens = () => {
-    cleaners.forEach(c => c())
-    resetBackend()
-  }
-
   const v = `${backendConfig.data?.currency} ${backendConfig.data?.version}`
   const ctx = useMemo(() => ({ currency: backendConfig.data?.currency || '', 
version: backendConfig.data?.version || '' }), [v])
 
@@ -126,7 +116,7 @@ function ApplicationStatusRoutes(): VNode {
   return <div id="app" class="has-navbar-fixed-top">
     <ConfigContextProvider value={ctx}>
       <Notifications notifications={notifications} 
removeNotification={removeNotification} />
-      <Route default component={ApplicationReadyRoutes} 
pushNotification={pushNotification} addTokenCleaner={addTokenCleanerMemo} 
clearAllTokens={clearAllTokens} />
+      <Route default component={ApplicationReadyRoutes} 
pushNotification={pushNotification} />
     </ConfigContextProvider>
   </div>
 }

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]