From 7f6101a24df0db86f33c3217e52838f09a25286d Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 9 Nov 2021 00:19:50 -0300 Subject: add provider/ remove provider --- .../src/pages/home/AddingProviderScreen.tsx | 99 +++++++++++++--------- 1 file changed, 57 insertions(+), 42 deletions(-) (limited to 'packages/anastasis-webui/src/pages/home/AddingProviderScreen.tsx') diff --git a/packages/anastasis-webui/src/pages/home/AddingProviderScreen.tsx b/packages/anastasis-webui/src/pages/home/AddingProviderScreen.tsx index 5cf6fbb09..7504f4d2b 100644 --- a/packages/anastasis-webui/src/pages/home/AddingProviderScreen.tsx +++ b/packages/anastasis-webui/src/pages/home/AddingProviderScreen.tsx @@ -1,6 +1,6 @@ import { AuthenticationProviderStatusOk } from "anastasis-core"; import { h, VNode } from "preact"; -import { useEffect, useLayoutEffect, useRef, useState } from "preact/hooks"; +import { useEffect, useRef, useState } from "preact/hooks"; import { TextInput } from "../../components/fields/TextInput"; import { useAnastasisContext } from "../../context/anastasis"; import { authMethods, KnownAuthMethods } from "./authMethod"; @@ -8,13 +8,13 @@ import { AnastasisClientFrame } from "./index"; interface Props { providerType?: KnownAuthMethods; - cancel: () => void; + onCancel: () => void; } async function testProvider(url: string, expectedMethodType?: string): Promise { try { - const response = await fetch(`${url}/config`) + const response = await fetch(new URL("config", url).href) const json = await (response.json().catch(d => ({}))) if (!("methods" in json) || !Array.isArray(json.methods)) { throw Error("This provider doesn't have authentication method. Check the provider URL") @@ -41,7 +41,7 @@ async function testProvider(url: string, expectedMethodType?: string): Promise { if (timeout) window.clearTimeout(timeout.current) timeout.current = window.setTimeout(async () => { - const url = providerURL.endsWith('/') ? providerURL.substring(0, providerURL.length - 1) : providerURL - if (!url) return; + const url = providerURL.endsWith('/') ? providerURL : (providerURL + '/') + if (!providerURL || authProviders.includes(url)) return; try { setTesting(true) await testProvider(url, providerType) @@ -67,40 +67,50 @@ export function AddingProviderScreen({ providerType, cancel }: Props): VNode { if (e instanceof Error) setError(e.message) } setTesting(false) - }, 1000); - }, [providerURL]) + }, 200); + }, [providerURL, reducer]) if (!reducer) { return
no reducer in context
; } - function addProvider(): void { - // addAuthMethod({ - // authentication_method: { - // type: "sms", - // instructions: `SMS to ${providerURL}`, - // challenge: encodeCrock(stringToBytes(providerURL)), - // }, - // }); + if (!reducer.currentReducerState || !("authentication_providers" in reducer.currentReducerState)) { + return
invalid state
+ } + + async function addProvider(provider_url: string): Promise { + await reducer?.transition("add_provider", { provider_url }) + onCancel() + } + function deleteProvider(provider_url: string): void { + reducer?.transition("delete_provider", { provider_url }) } + const allAuthProviders = reducer.currentReducerState.authentication_providers || {} + const authProviders = Object.keys(allAuthProviders).filter(provUrl => { + const p = allAuthProviders[provUrl]; + if (!providerLabel) { + return p && ("currency" in p) + } else { + return p && ("currency" in p) && p.methods.findIndex(m => m.type === providerType) !== -1 + } + }) + let errors = !providerURL ? 'Add provider URL' : undefined + let url: string | undefined; try { - new URL(providerURL) + url = new URL("",providerURL).href } catch { errors = 'Check the URL' } if (!!error && !errors) { errors = error } - - if (!reducer.currentReducerState || !("authentication_providers" in reducer.currentReducerState)) { - return
invalid state
+ if (!errors && authProviders.includes(url!)) { + errors = 'That provider is already known' } - const authProviders = reducer.currentReducerState.authentication_providers || {} - return (

Example: https://kudos.demo.anastasis.lu

- - {testing &&

Testing

} - {!!error &&

{error}

} - {error === "" &&

This provider worked!

} - + {testing &&

Testing

} +
- + - +
-

- Current providers -

- {/* */} - {Object.keys(authProviders).map(k => { - const p = authProviders[k] - if (("currency" in p)) { - return - } - } + {authProviders.length > 0 ? ( + !providerLabel ? +

+ Current providers +

:

+ Current providers for {providerLabel} service +

+ ) : ( + !providerLabel ?

+ No known providers, add one. +

:

+ No known providers for {providerLabel} service +

)} - {/*
*/} + + {authProviders.map(k => { + const p = allAuthProviders[k] as AuthenticationProviderStatusOk + return + })}
); } -function TableRow({ url, info }: { url: string, info: AuthenticationProviderStatusOk }) { +function TableRow({ url, info, onDelete }: { onDelete: (s: string) => void, url: string, info: AuthenticationProviderStatusOk }) { const [status, setStatus] = useState("checking") useEffect(function () { testProvider(url.endsWith('/') ? url.substring(0, url.length - 1) : url) @@ -174,7 +189,7 @@ function TableRow({ url, info }: { url: string, info: AuthenticationProviderStat
- +
} \ No newline at end of file -- cgit v1.2.3