diff options
author | Sebastian <sebasjm@gmail.com> | 2022-06-11 19:10:26 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-06-11 19:10:26 -0300 |
commit | 6d06b52605005f4d25381fc73383c3c9e48f20f8 (patch) | |
tree | d1e01d71c538602a92848595f92d24bf214c264f /packages/anastasis-webui/src/pages/home/AddingProviderScreen/state.ts | |
parent | 716da3246b7d544fc81265d1942ae64067ecd8b7 (diff) | |
download | wallet-core-6d06b52605005f4d25381fc73383c3c9e48f20f8.tar.gz wallet-core-6d06b52605005f4d25381fc73383c3c9e48f20f8.tar.bz2 wallet-core-6d06b52605005f4d25381fc73383c3c9e48f20f8.zip |
add testing to web components
Diffstat (limited to 'packages/anastasis-webui/src/pages/home/AddingProviderScreen/state.ts')
-rw-r--r-- | packages/anastasis-webui/src/pages/home/AddingProviderScreen/state.ts | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/packages/anastasis-webui/src/pages/home/AddingProviderScreen/state.ts b/packages/anastasis-webui/src/pages/home/AddingProviderScreen/state.ts new file mode 100644 index 000000000..a04c7957b --- /dev/null +++ b/packages/anastasis-webui/src/pages/home/AddingProviderScreen/state.ts @@ -0,0 +1,147 @@ +/* + This file is part of GNU Anastasis + (C) 2021-2022 Anastasis SARL + + GNU Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + GNU Anastasis; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ +import { useEffect, useRef, useState } from "preact/hooks"; +import { useAnastasisContext } from "../../../context/anastasis.js"; +import { authMethods, KnownAuthMethods } from "../authMethod/index.jsx"; +import { AuthProvByStatusMap, State, testProvider } from "./index.js"; + +interface Props { + providerType?: KnownAuthMethods; + onCancel: () => Promise<void>; +} + +export default function useComponentState({ providerType, onCancel }: Props): State { + const reducer = useAnastasisContext(); + + const [providerURL, setProviderURL] = useState(""); + + const [error, setError] = useState<string | undefined>(); + const [testing, setTesting] = useState(false); + + const providerLabel = providerType + ? authMethods[providerType].label + : undefined; + + const allAuthProviders = + !reducer || + !reducer.currentReducerState || + reducer.currentReducerState.reducer_type === "error" || + !reducer.currentReducerState.authentication_providers + ? {} + : reducer.currentReducerState.authentication_providers; + + const authProvidersByStatus = Object.keys(allAuthProviders).reduce( + (prev, url) => { + const p = allAuthProviders[url]; + if ( + providerLabel && + p.status === "ok" && + p.methods.findIndex((m) => m.type === providerType) !== -1 + ) { + return prev; + } + prev[p.status].push({ ...p, url }); + return prev; + }, + { "not-contacted": [], disabled: [], error: [], ok: [] } as AuthProvByStatusMap, + ); + const authProviders = authProvidersByStatus["ok"].map((p) => p.url); + + //FIXME: move this timeout logic into a hook + const timeout = useRef<ReturnType<typeof setTimeout> | undefined>(undefined); + useEffect(() => { + if (timeout.current) clearTimeout(timeout.current); + timeout.current = setTimeout(async () => { + const url = providerURL.endsWith("/") ? providerURL : providerURL + "/"; + if (!providerURL || authProviders.includes(url)) return; + try { + setTesting(true); + await testProvider(url, providerType); + setError(""); + } catch (e) { + if (e instanceof Error) setError(e.message); + } + setTesting(false); + }, 200); + }, [providerURL, reducer]); + + if (!reducer) { + return { + status: "no-reducer", + }; + } + + if ( + !reducer.currentReducerState || + !("authentication_providers" in reducer.currentReducerState) + ) { + return { + status: "invalid-state", + }; + } + + const addProvider = async (provider_url: string): Promise<void> => { + await reducer.transition("add_provider", { provider_url }); + onCancel(); + } + const deleteProvider = async (provider_url: string): Promise<void> => { + reducer.transition("delete_provider", { provider_url }); + } + + let errors = !providerURL ? "Add provider URL" : undefined; + let url: string | undefined; + try { + url = new URL("", providerURL).href; + } catch { + errors = "Check the URL"; + } + const _url = url + + if (!!error && !errors) { + errors = error; + } + if (!errors && authProviders.includes(url!)) { + errors = "That provider is already known"; + } + + const commonState = { + addProvider: !_url ? undefined : async () => addProvider(_url), + deleteProvider: async (url: string) => deleteProvider(url), + allAuthProviders, + authProvidersByStatus, + onCancel, + providerURL, + testing, + setProviderURL: async (s: string) => setProviderURL(s), + errors, + error, + } + + if (!providerLabel) { + return { + status: "without-type", + ...commonState + } + } else { + return { + status: "with-type", + providerLabel, + ...commonState + } + } + +} + |