diff options
Diffstat (limited to 'packages/demobank-ui/src/context/config.ts')
-rw-r--r-- | packages/demobank-ui/src/context/config.ts | 65 |
1 files changed, 50 insertions, 15 deletions
diff --git a/packages/demobank-ui/src/context/config.ts b/packages/demobank-ui/src/context/config.ts index a2cde18eb..013d8922e 100644 --- a/packages/demobank-ui/src/context/config.ts +++ b/packages/demobank-ui/src/context/config.ts @@ -14,36 +14,71 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ +import { TalerCorebankApi, TalerCoreBankHttpClient, TalerError } from "@gnu-taler/taler-util"; +import { BrowserHttpLib, useTranslationContext } from "@gnu-taler/web-util/browser"; import { ComponentChildren, createContext, h, VNode } from "preact"; -import { useContext } from "preact/hooks"; +import { useContext, useEffect, useState } from "preact/hooks"; +import { ErrorLoading } from "../components/ErrorLoading.js"; /** * * @author Sebastian Javier Marchano (sebasjm) */ -export type Type = Required<SandboxBackend.Config>; - -const initial: Type = { - name: "", - version: "0:0:0", - currency_fraction_digits: 2, - currency_fraction_limit: 2, - fiat_currency: "", - have_cashout: false, +export type Type = { + url: URL, + config: TalerCorebankApi.Config, + api: TalerCoreBankHttpClient, }; -const Context = createContext<Type>(initial); -export const useConfigContext = (): Type => useContext(Context); +const Context = createContext<Type>(undefined as any); + +export const useBankCoreApiContext = (): Type => useContext(Context); + +export type ConfigResult = undefined + | { type: "ok", config: TalerCorebankApi.Config } + | { type: "incompatible", result: TalerCorebankApi.Config, supported: string } + | { type: "error", error: TalerError } -export const ConfigStateProvider = ({ - value, +export const BankCoreApiProvider = ({ + baseUrl, children, }: { - value: Type, + baseUrl: string, children: ComponentChildren; }): VNode => { + const [checked, setChecked] = useState<ConfigResult>() + const { i18n } = useTranslationContext(); + const url = new URL(baseUrl) + const api = new TalerCoreBankHttpClient(url.href, new BrowserHttpLib()) + useEffect(() => { + api.getConfig() + .then((resp) => { + if (api.isCompatible(resp.body.version)) { + setChecked({ type: "ok", config: resp.body }); + } else { + setChecked({ type: "incompatible", result: resp.body, supported: api.PROTOCOL_VERSION }) + } + }) + .catch((error: unknown) => { + if (error instanceof TalerError) { + setChecked({ type: "error", error }); + } + }); + }, []); + if (checked === undefined) { + return h("div", {}, "loading...") + } + if (checked.type === "error") { + return h(ErrorLoading, { error: checked.error, showDetail: true }) + } + if (checked.type === "incompatible") { + return h("div", {}, i18n.str`the bank backend is not supported. supported version "${checked.supported}", server version "${checked.result.version}"`) + } + const value: Type = { + url, config: checked.config, api + } return h(Context.Provider, { value, children, |