diff options
Diffstat (limited to 'packages/merchant-backoffice-ui/src/context')
3 files changed, 17 insertions, 60 deletions
diff --git a/packages/merchant-backoffice-ui/src/context/backend.test.ts b/packages/merchant-backoffice-ui/src/context/backend.test.ts index cb0010c4b..b042d5a25 100644 --- a/packages/merchant-backoffice-ui/src/context/backend.test.ts +++ b/packages/merchant-backoffice-ui/src/context/backend.test.ts @@ -21,7 +21,7 @@ import * as tests from "@gnu-taler/web-util/testing"; import { ComponentChildren, h, VNode } from "preact"; -import { MerchantBackend } from "../declaration.js"; +import { AccessToken, MerchantBackend } from "../declaration.js"; import { useAdminAPI, useInstanceAPI, @@ -64,7 +64,7 @@ describe("backend context api ", () => { } as MerchantBackend.Instances.QueryInstancesResponse, }); - management.setNewToken("another_token"); + management.setNewToken("another_token" as AccessToken); }, ({ instance, management, admin }) => { expect(env.assertJustExpectedRequestWereMade()).deep.eq({ @@ -113,7 +113,7 @@ describe("backend context api ", () => { name: "instance_name", } as MerchantBackend.Instances.QueryInstancesResponse, }); - instance.setNewToken("another_token"); + instance.setNewToken("another_token" as AccessToken); }, ({ instance, management, admin }) => { expect(env.assertJustExpectedRequestWereMade()).deep.eq({ 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 }); }; diff --git a/packages/merchant-backoffice-ui/src/context/instance.ts b/packages/merchant-backoffice-ui/src/context/instance.ts index 9a25fe80c..3c6cc2b63 100644 --- a/packages/merchant-backoffice-ui/src/context/instance.ts +++ b/packages/merchant-backoffice-ui/src/context/instance.ts @@ -21,12 +21,13 @@ import { createContext } from "preact"; import { useContext } from "preact/hooks"; +import { LoginToken } from "../declaration.js"; interface Type { id: string; - token?: string; + token?: LoginToken; admin?: boolean; - changeToken: (t?: string) => void; + changeToken: (t?: LoginToken) => void; } const Context = createContext<Type>({} as any); |