merchant-backoffice

ZZZ: Inactive/Deprecated
Log | Files | Refs | Submodules | README

commit 789de7f8522ca03f8460df0431b4e1a7e792e25f
parent 1ed3f01006aca39bf428aa831cc76d73043d2893
Author: Sebastian <sebasjm@gmail.com>
Date:   Fri, 12 Mar 2021 09:23:06 -0300

refactor navigation, part 1

Diffstat:
MCHANGELOG.md | 3+++
Mpackages/frontend/src/AdminRoutes.tsx | 6+-----
Mpackages/frontend/src/ApplicationReadyRoutes.tsx | 77++++++++++++++++++++++++++++++++++++-----------------------------------------
Mpackages/frontend/src/InstanceRoutes.tsx | 5++---
Mpackages/frontend/src/components/menu/index.tsx | 1-
Mpackages/frontend/src/context/backend.ts | 8++++----
Mpackages/frontend/src/hooks/index.ts | 12++++++++++--
Mpackages/frontend/src/index.tsx | 12+-----------
8 files changed, 57 insertions(+), 67 deletions(-)

diff --git 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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> }