From 6d3db4ce6ce3119f299936d9f0e4052bf1eff6c7 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 15 Jul 2021 15:05:09 -0300 Subject: added remove button and more complex add-provider interaction --- .../src/popup/ProviderAddPage.tsx | 66 +++++++++++++++------- 1 file changed, 45 insertions(+), 21 deletions(-) (limited to 'packages/taler-wallet-webextension/src/popup/ProviderAddPage.tsx') diff --git a/packages/taler-wallet-webextension/src/popup/ProviderAddPage.tsx b/packages/taler-wallet-webextension/src/popup/ProviderAddPage.tsx index 92579e36a..2680345f7 100644 --- a/packages/taler-wallet-webextension/src/popup/ProviderAddPage.tsx +++ b/packages/taler-wallet-webextension/src/popup/ProviderAddPage.tsx @@ -1,6 +1,7 @@ -import { Amounts, BackupBackupProviderTerms, i18n } from "@gnu-taler/taler-util"; +import { Amounts, BackupBackupProviderTerms, canonicalizeBaseUrl, i18n } from "@gnu-taler/taler-util"; +import { verify } from "@gnu-taler/taler-wallet-core/src/crypto/primitives/nacl-fast"; import { VNode } from "preact"; -import { useState } from "preact/hooks"; +import { useEffect, useState } from "preact/hooks"; import { Checkbox } from "../components/Checkbox"; import { ErrorMessage } from "../components/ErrorMessage"; import { Button, ButtonPrimary, Input, LightText, PopupBox, SmallTextLight } from "../components/styled/index"; @@ -25,18 +26,24 @@ function getJsonIfOk(r: Response) { export function ProviderAddPage({ onBack }: Props): VNode { - const [verifying, setVerifying] = useState<{ url: string, provider: BackupBackupProviderTerms } | undefined>(undefined) + const [verifying, setVerifying] = useState<{ url: string, name: string, provider: BackupBackupProviderTerms } | undefined>(undefined) + + async function getProviderInfo(url: string): Promise { + return fetch(`${url}config`) + .catch(e => { throw new Error(`Network error`) }) + .then(getJsonIfOk) + } if (!verifying) { return { - return fetch(`${url}/config`) - .catch(e => { throw new Error(`Network error`) }) - .then(getJsonIfOk) - .then((provider) => { setVerifying({ url, provider }); return undefined }) - .catch((e) => e.message) - }} + onVerify={(url) => getProviderInfo(url)} + onConfirm={(url, name) => getProviderInfo(url) + .then((provider) => { + setVerifying({ url, name, provider }); + }) + .catch(e => e.message) + } /> } return { - wxApi.addBackupProvider(verifying.url).then(onBack) + wxApi.addBackupProvider(verifying.url, verifying.name).then(onBack) }} /> @@ -56,37 +63,54 @@ export function ProviderAddPage({ onBack }: Props): VNode { export interface SetUrlViewProps { initialValue?: string; onCancel: () => void; - onVerify: (s: string) => Promise; + onVerify: (s: string) => Promise; + onConfirm: (url: string, name: string) => Promise; withError?: string; } -export function SetUrlView({ initialValue, onCancel, onVerify, withError }: SetUrlViewProps) { +export function SetUrlView({ initialValue, onCancel, onVerify, onConfirm, withError }: SetUrlViewProps) { const [value, setValue] = useState(initialValue || "") + const [urlError, setUrlError] = useState(false) + const [name, setName] = useState(undefined) const [error, setError] = useState(withError) + useEffect(() => { + try { + const url = canonicalizeBaseUrl(value) + onVerify(url).then(r => { + setUrlError(false) + setName(new URL(url).hostname) + }).catch(() => { + setUrlError(true) + setName(undefined) + }) + } catch { + setUrlError(true) + setName(undefined) + } + }, [value]) return

Add backup provider

Backup providers may charge for their service

- + setValue(e.currentTarget.value)} /> - + setName(e.currentTarget.value)}/>

{ - let url = value.startsWith('http://') || value.startsWith('https://') ? value : `https://${value}` - url = url.endsWith('/') ? url.substring(0, url.length - 1) : url; - return onVerify(url).then(r => r ? setError(r) : undefined) + const url = canonicalizeBaseUrl(value) + return onConfirm(url, name!).then(r => r ? setError(r) : undefined) }}>Next
@@ -108,13 +132,13 @@ export function ConfirmProviderView({ url, provider, onCancel, onConfirm }: Conf Please review and accept this provider's terms of service

1. Pricing

- {Amounts.isZero(provider.annual_fee) ? 'free of charge' : `${provider.annual_fee} per year of service`} + {Amounts.isZero(provider.annual_fee) ? 'free of charge' : `${provider.annual_fee} per year of service`}

2. Storage

{provider.storage_limit_in_megabytes} megabytes of storage per year of service

- setAccepted(old => !old)} enabled={accepted}/> + setAccepted(old => !old)} enabled={accepted} />