diff options
Diffstat (limited to 'packages/merchant-backoffice-ui/src/context')
5 files changed, 104 insertions, 70 deletions
diff --git a/packages/merchant-backoffice-ui/src/context/backend.ts b/packages/merchant-backoffice-ui/src/context/backend.ts index 1d11a3aad..f8d1bc397 100644 --- a/packages/merchant-backoffice-ui/src/context/backend.ts +++ b/packages/merchant-backoffice-ui/src/context/backend.ts @@ -15,12 +15,12 @@ */ /** -* -* @author Sebastian Javier Marchano (sebasjm) -*/ + * + * @author Sebastian Javier Marchano (sebasjm) + */ -import { createContext, h, VNode } from 'preact' -import { useCallback, useContext, useState } from 'preact/hooks' +import { createContext, h, VNode } from "preact"; +import { useCallback, useContext, useState } from "preact/hooks"; import { useBackendDefaultToken, useBackendURL } from "../hooks/index.js"; interface BackendContextType { @@ -34,49 +34,76 @@ interface BackendContextType { } const BackendContext = createContext<BackendContextType>({ - url: '', + url: "", token: undefined, triedToLog: false, resetBackend: () => null, clearAllTokens: () => null, addTokenCleaner: () => null, updateLoginStatus: () => null, -}) - -function useBackendContextState(defaultUrl?: string, initialToken?: string): BackendContextType { - const [url, triedToLog, changeBackend, resetBackend] = useBackendURL(defaultUrl); +}); + +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) - } + _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 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()) + 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) + const k = localStorage.key(i); + if (k && /^backend-token/.test(k)) localStorage.removeItem(k); } - resetBackend() - } + resetBackend(); + }; const updateLoginStatus = (url: string, token?: string) => { changeBackend(url); if (token) updateToken(token); }; - - return { url, token, triedToLog, updateLoginStatus, resetBackend, clearAllTokens, addTokenCleaner: addTokenCleanerMemo } + return { + url, + token, + triedToLog, + updateLoginStatus, + resetBackend, + clearAllTokens, + addTokenCleaner: addTokenCleanerMemo, + }; } -export const BackendContextProvider = ({ children, defaultUrl, initialToken }: { children: any, defaultUrl?: string, initialToken?: string }): VNode => { - const value = useBackendContextState(defaultUrl, initialToken) +export const BackendContextProvider = ({ + children, + defaultUrl, + initialToken, +}: { + children: any; + defaultUrl?: string; + initialToken?: string; +}): VNode => { + const value = useBackendContextState(defaultUrl, initialToken); return h(BackendContext.Provider, { value, children }); -} +}; -export const useBackendContext = (): BackendContextType => useContext(BackendContext); +export const useBackendContext = (): BackendContextType => + useContext(BackendContext); diff --git a/packages/merchant-backoffice-ui/src/context/config.ts b/packages/merchant-backoffice-ui/src/context/config.ts index a0d530830..040bd0341 100644 --- a/packages/merchant-backoffice-ui/src/context/config.ts +++ b/packages/merchant-backoffice-ui/src/context/config.ts @@ -15,18 +15,18 @@ */ /** -* -* @author Sebastian Javier Marchano (sebasjm) -*/ + * + * @author Sebastian Javier Marchano (sebasjm) + */ -import { createContext } from 'preact' -import { useContext } from 'preact/hooks' +import { createContext } from "preact"; +import { useContext } from "preact/hooks"; interface Type { currency: string; version: string; } -const Context = createContext<Type>(null!) +const Context = createContext<Type>(null!); -export const ConfigContextProvider = Context.Provider +export const ConfigContextProvider = Context.Provider; export const useConfigContext = (): Type => useContext(Context); diff --git a/packages/merchant-backoffice-ui/src/context/instance.ts b/packages/merchant-backoffice-ui/src/context/instance.ts index a45e8283c..9a25fe80c 100644 --- a/packages/merchant-backoffice-ui/src/context/instance.ts +++ b/packages/merchant-backoffice-ui/src/context/instance.ts @@ -15,21 +15,21 @@ */ /** -* -* @author Sebastian Javier Marchano (sebasjm) -*/ + * + * @author Sebastian Javier Marchano (sebasjm) + */ -import { createContext } from 'preact' -import { useContext } from 'preact/hooks' +import { createContext } from "preact"; +import { useContext } from "preact/hooks"; interface Type { id: string; token?: string; admin?: boolean; - changeToken: (t?:string) => void; + changeToken: (t?: string) => void; } -const Context = createContext<Type>({} as any) +const Context = createContext<Type>({} as any); -export const InstanceContextProvider = Context.Provider +export const InstanceContextProvider = Context.Provider; export const useInstanceContext = (): Type => useContext(Context); diff --git a/packages/merchant-backoffice-ui/src/context/listener.ts b/packages/merchant-backoffice-ui/src/context/listener.ts index 097810273..cf51bb718 100644 --- a/packages/merchant-backoffice-ui/src/context/listener.ts +++ b/packages/merchant-backoffice-ui/src/context/listener.ts @@ -15,21 +15,21 @@ */ /** -* -* @author Sebastian Javier Marchano (sebasjm) -*/ + * + * @author Sebastian Javier Marchano (sebasjm) + */ -import { createContext } from 'preact' -import { useContext } from 'preact/hooks' +import { createContext } from "preact"; +import { useContext } from "preact/hooks"; interface Type { id: string; token?: string; admin?: boolean; - changeToken: (t?:string) => void; + changeToken: (t?: string) => void; } -const Context = createContext<Type>({} as any) +const Context = createContext<Type>({} as any); -export const ListenerContextProvider = Context.Provider +export const ListenerContextProvider = Context.Provider; export const useListenerContext = (): Type => useContext(Context); diff --git a/packages/merchant-backoffice-ui/src/context/translation.ts b/packages/merchant-backoffice-ui/src/context/translation.ts index 88359a149..027eac14c 100644 --- a/packages/merchant-backoffice-ui/src/context/translation.ts +++ b/packages/merchant-backoffice-ui/src/context/translation.ts @@ -15,13 +15,13 @@ */ /** -* -* @author Sebastian Javier Marchano (sebasjm) -*/ + * + * @author Sebastian Javier Marchano (sebasjm) + */ -import { createContext, h, VNode } from 'preact' -import { useContext, useEffect } from 'preact/hooks' -import { useLang } from '../hooks' +import { createContext, h, VNode } from "preact"; +import { useContext, useEffect } from "preact/hooks"; +import { useLang } from "../hooks"; import * as jedLib from "jed"; import { strings } from "../i18n/strings"; @@ -31,29 +31,36 @@ interface Type { changeLanguage: (l: string) => void; } const initial = { - lang: 'en', + lang: "en", handler: null, changeLanguage: () => { // do not change anything - } -} -const Context = createContext<Type>(initial) + }, +}; +const Context = createContext<Type>(initial); interface Props { - initial?: string, - children: any, - forceLang?: string + initial?: string; + children: any; + forceLang?: string; } -export const TranslationProvider = ({ initial, children, forceLang }: Props): VNode => { - const [lang, changeLanguage] = useLang(initial) +export const TranslationProvider = ({ + initial, + children, + forceLang, +}: Props): VNode => { + const [lang, changeLanguage] = useLang(initial); useEffect(() => { if (forceLang) { - changeLanguage(forceLang) + changeLanguage(forceLang); } - }) + }); const handler = new jedLib.Jed(strings[lang]); - return h(Context.Provider, { value: { lang, handler, changeLanguage }, children }); -} + return h(Context.Provider, { + value: { lang, handler, changeLanguage }, + children, + }); +}; -export const useTranslationContext = (): Type => useContext(Context);
\ No newline at end of file +export const useTranslationContext = (): Type => useContext(Context); |