/* This file is part of GNU Taler (C) 2021 Taler Systems S.A. GNU Taler is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. GNU Taler 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 General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Taler; see the file COPYING. If not, see */ import { Amounts, BackupBackupProviderTerms, canonicalizeBaseUrl, } from "@gnu-taler/taler-util"; import { Fragment, h, VNode } from "preact"; import { useEffect, useState } from "preact/hooks"; import { Checkbox } from "../components/Checkbox.js"; import { ErrorMessage } from "../components/ErrorMessage.js"; import { Button, ButtonPrimary, Input, LightText, SmallLightText, SubTitle, Title, } from "../components/styled/index.js"; import { useTranslationContext } from "../context/translation.js"; import { queryToSlashConfig } from "../utils/index.js"; import * as wxApi from "../wxApi.js"; interface Props { currency: string; onBack: () => void; } export function ProviderAddPage({ onBack }: Props): VNode { const [verifying, setVerifying] = useState< | { url: string; name: string; provider: BackupBackupProviderTerms } | undefined >(undefined); if (!verifying) { return ( queryToSlashConfig(url)} onConfirm={(url, name) => queryToSlashConfig(url) .then((provider) => { setVerifying({ url, name, provider }); }) .catch((e) => e.message) } /> ); } return ( { setVerifying(undefined); }} onConfirm={() => { wxApi.addBackupProvider(verifying.url, verifying.name).then(onBack); }} /> ); } export interface SetUrlViewProps { initialValue?: string; onCancel: () => void; onVerify: (s: string) => Promise; onConfirm: (url: string, name: string) => Promise; withError?: string; } export function SetUrlView({ initialValue, onCancel, onVerify, onConfirm, withError, }: SetUrlViewProps): VNode { const { i18n } = useTranslationContext(); 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 (
<i18n.Translate>Add backup provider</i18n.Translate> {error && ( Could not get provider information } description={error} /> )} Backup providers may charge for their service

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

{ const url = canonicalizeBaseUrl(value); return onConfirm(url, name!).then((r) => r ? setError(r) : undefined, ); }} > Next
); } export interface ConfirmProviderViewProps { provider: BackupBackupProviderTerms; url: string; onCancel: () => void; onConfirm: () => void; } export function ConfirmProviderView({ url, provider, onCancel, onConfirm, }: ConfirmProviderViewProps): VNode { const [accepted, setAccepted] = useState(false); const { i18n } = useTranslationContext(); return (
<i18n.Translate>Review terms of service</i18n.Translate>
Provider URL:{" "} {url}
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 )}

2. Storage

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

Accept terms of service} name="terms" onToggle={() => setAccepted((old) => !old)} enabled={accepted} />
); }