import { canonicalizeBaseUrl, ExchangeListItem, i18n, TalerConfigResponse, } from "@gnu-taler/taler-util"; import { Fragment, h } from "preact"; import { useEffect, useState } from "preact/hooks"; import { ErrorMessage } from "../components/ErrorMessage"; import { Button, ButtonPrimary, Input, WarningBox, } from "../components/styled/index"; export interface Props { initialValue?: string; expectedCurrency?: string; knownExchanges: ExchangeListItem[]; onCancel: () => void; onVerify: (s: string) => Promise; onConfirm: (url: string) => Promise; withError?: string; } export function ExchangeSetUrlPage({ initialValue, knownExchanges, expectedCurrency, onCancel, onVerify, onConfirm, withError, }: Props) { const [value, setValue] = useState(initialValue || ""); const [dirty, setDirty] = useState(false); const [result, setResult] = useState( undefined, ); const [error, setError] = useState(withError); useEffect(() => { try { const url = canonicalizeBaseUrl(value); const found = knownExchanges.findIndex((e) => e.exchangeBaseUrl === url) !== -1; if (found) { setError("This exchange is already known"); return; } onVerify(url) .then((r) => { setResult(r); }) .catch(() => { setResult(undefined); }); setDirty(true); } catch { setResult(undefined); } }, [value]); return (
{!expectedCurrency ? (

Add new exchange

) : (

Add exchange for {expectedCurrency}

)}

setValue(e.currentTarget.value)} /> {result && ( )}

{result && expectedCurrency && expectedCurrency !== result.currency && ( This exchange doesn't match the expected currency{" "} {expectedCurrency} )}
); }