diff options
Diffstat (limited to 'packages/merchant-backoffice-ui/src/context/backend.ts')
-rw-r--r-- | packages/merchant-backoffice-ui/src/context/backend.ts | 66 |
1 files changed, 11 insertions, 55 deletions
diff --git a/packages/merchant-backoffice-ui/src/context/backend.ts b/packages/merchant-backoffice-ui/src/context/backend.ts index 43e9e4d27..056f9a192 100644 --- a/packages/merchant-backoffice-ui/src/context/backend.ts +++ b/packages/merchant-backoffice-ui/src/context/backend.ts @@ -20,90 +20,46 @@ */ import { createContext, h, VNode } from "preact"; -import { useCallback, useContext, useState } from "preact/hooks"; +import { useContext } from "preact/hooks"; +import { LoginToken } from "../declaration.js"; import { useBackendDefaultToken, useBackendURL } from "../hooks/index.js"; interface BackendContextType { - url: string; - token?: string; - triedToLog: boolean; - resetBackend: () => void; - // clearAllTokens: () => void; - // addTokenCleaner: (c: () => void) => void; - updateLoginStatus: (url: string, token?: string) => void; - updateToken: (token?: string) => void; + url: string, + token?: LoginToken; + updateToken: (token: LoginToken | undefined) => void; + changeBackend: (url: string) => void; } const BackendContext = createContext<BackendContextType>({ url: "", token: undefined, - triedToLog: false, - resetBackend: () => null, - // clearAllTokens: () => null, - // addTokenCleaner: () => null, - updateLoginStatus: () => null, updateToken: () => null, + changeBackend: () => null, }); function useBackendContextState( defaultUrl?: string, - initialToken?: string, ): BackendContextType { - const [url, triedToLog, changeBackend, resetBackend] = - useBackendURL(defaultUrl); - const [token, _updateToken] = useBackendDefaultToken(initialToken); - const updateToken = (t?: string) => { - _updateToken(t); - }; - - // 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()); - // for (let i = 0; i < localStorage.length; i++) { - // const k = localStorage.key(i); - // if (k && /^backend-token/.test(k)) localStorage.removeItem(k); - // } - // resetBackend(); - // }; - - const updateLoginStatus = (url: string, token?: string) => { - changeBackend(url); - updateToken(token); - }; + const [url, changeBackend] = useBackendURL(defaultUrl); + const [token, updateToken] = useBackendDefaultToken(); return { url, token, - triedToLog, - updateLoginStatus, - resetBackend, - // clearAllTokens, updateToken, - // addTokenCleaner: addTokenCleanerMemo, + changeBackend }; } export const BackendContextProvider = ({ children, defaultUrl, - initialToken, }: { children: any; defaultUrl?: string; - initialToken?: string; }): VNode => { - const value = useBackendContextState(defaultUrl, initialToken); + const value = useBackendContextState(defaultUrl); return h(BackendContext.Provider, { value, children }); }; |