2021-11-22 21:34:27 +01:00
|
|
|
import {
|
|
|
|
canonicalizeBaseUrl,
|
|
|
|
TalerConfigResponse,
|
|
|
|
} from "@gnu-taler/taler-util";
|
|
|
|
import { Fragment, h } from "preact";
|
|
|
|
import { useEffect, useState } from "preact/hooks";
|
|
|
|
import { ErrorMessage } from "../components/ErrorMessage";
|
2022-02-18 20:54:15 +01:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
ButtonPrimary,
|
|
|
|
Input,
|
|
|
|
LightText,
|
2022-03-28 19:03:59 +02:00
|
|
|
SubTitle,
|
|
|
|
Title,
|
2022-02-18 20:54:15 +01:00
|
|
|
WarningBox,
|
|
|
|
} from "../components/styled";
|
2022-03-14 19:20:32 +01:00
|
|
|
import { useTranslationContext } from "../context/translation";
|
2021-11-22 21:34:27 +01:00
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
initialValue?: string;
|
|
|
|
expectedCurrency?: string;
|
|
|
|
onCancel: () => void;
|
|
|
|
onVerify: (s: string) => Promise<TalerConfigResponse | undefined>;
|
|
|
|
onConfirm: (url: string) => Promise<string | undefined>;
|
|
|
|
withError?: string;
|
|
|
|
}
|
|
|
|
|
2021-12-06 19:27:20 +01:00
|
|
|
function useEndpointStatus<T>(
|
|
|
|
endpoint: string,
|
|
|
|
onVerify: (e: string) => Promise<T>,
|
|
|
|
): {
|
|
|
|
loading: boolean;
|
|
|
|
error?: string;
|
|
|
|
endpoint: string;
|
|
|
|
result: T | undefined;
|
|
|
|
updateEndpoint: (s: string) => void;
|
|
|
|
} {
|
|
|
|
const [value, setValue] = useState<string>(endpoint);
|
|
|
|
const [dirty, setDirty] = useState(false);
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [result, setResult] = useState<T | undefined>(undefined);
|
|
|
|
const [error, setError] = useState<string | undefined>(undefined);
|
|
|
|
|
|
|
|
const [handler, setHandler] = useState<number | undefined>(undefined);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!value) return;
|
|
|
|
window.clearTimeout(handler);
|
|
|
|
const h = window.setTimeout(async () => {
|
|
|
|
setDirty(true);
|
|
|
|
setLoading(true);
|
|
|
|
try {
|
|
|
|
const url = canonicalizeBaseUrl(value);
|
|
|
|
const result = await onVerify(url);
|
|
|
|
setResult(result);
|
|
|
|
setError(undefined);
|
|
|
|
setLoading(false);
|
|
|
|
} catch (e) {
|
|
|
|
const errorMessage =
|
|
|
|
e instanceof Error ? e.message : `unknown error: ${e}`;
|
|
|
|
setError(errorMessage);
|
|
|
|
setLoading(false);
|
|
|
|
setResult(undefined);
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
setHandler(h);
|
|
|
|
}, [value]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
error: dirty ? error : undefined,
|
|
|
|
loading: loading,
|
|
|
|
result: result,
|
|
|
|
endpoint: value,
|
|
|
|
updateEndpoint: setValue,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-22 21:34:27 +01:00
|
|
|
export function ExchangeSetUrlPage({
|
|
|
|
initialValue,
|
|
|
|
expectedCurrency,
|
|
|
|
onCancel,
|
|
|
|
onVerify,
|
|
|
|
onConfirm,
|
|
|
|
}: Props) {
|
2022-03-14 19:20:32 +01:00
|
|
|
const { i18n } = useTranslationContext();
|
2021-12-06 19:27:20 +01:00
|
|
|
const { loading, result, endpoint, updateEndpoint, error } =
|
|
|
|
useEndpointStatus(initialValue ?? "", onVerify);
|
2021-11-22 21:34:27 +01:00
|
|
|
|
2021-12-06 19:27:20 +01:00
|
|
|
const [confirmationError, setConfirmationError] = useState<
|
|
|
|
string | undefined
|
|
|
|
>(undefined);
|
2021-11-22 21:34:27 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<section>
|
|
|
|
{!expectedCurrency ? (
|
2022-03-28 19:03:59 +02:00
|
|
|
<Title>
|
2022-02-23 19:44:14 +01:00
|
|
|
<i18n.Translate>Add new exchange</i18n.Translate>
|
2022-03-28 19:03:59 +02:00
|
|
|
</Title>
|
2021-11-22 21:34:27 +01:00
|
|
|
) : (
|
2022-03-28 19:03:59 +02:00
|
|
|
<SubTitle>
|
2022-02-23 19:44:14 +01:00
|
|
|
<i18n.Translate>Add exchange for {expectedCurrency}</i18n.Translate>
|
2022-03-28 19:03:59 +02:00
|
|
|
</SubTitle>
|
2021-11-22 21:34:27 +01:00
|
|
|
)}
|
2022-02-18 20:54:15 +01:00
|
|
|
{!result && (
|
2022-02-23 19:18:37 +01:00
|
|
|
<LightText>
|
2022-02-23 19:44:14 +01:00
|
|
|
<i18n.Translate>
|
|
|
|
Enter the URL of an exchange you trust.
|
|
|
|
</i18n.Translate>
|
2022-02-23 19:18:37 +01:00
|
|
|
</LightText>
|
2022-02-18 20:54:15 +01:00
|
|
|
)}
|
|
|
|
{result && (
|
|
|
|
<LightText>
|
2022-02-23 19:44:14 +01:00
|
|
|
<i18n.Translate>
|
2022-02-23 19:18:37 +01:00
|
|
|
An exchange has been found! Review the information and click next
|
2022-02-23 19:44:14 +01:00
|
|
|
</i18n.Translate>
|
2022-02-18 20:54:15 +01:00
|
|
|
</LightText>
|
|
|
|
)}
|
2021-12-06 19:27:20 +01:00
|
|
|
{result && expectedCurrency && expectedCurrency !== result.currency && (
|
|
|
|
<WarningBox>
|
2022-02-23 19:44:14 +01:00
|
|
|
<i18n.Translate>
|
2022-02-23 19:18:37 +01:00
|
|
|
This exchange doesn't match the expected currency
|
|
|
|
<b>{expectedCurrency}</b>
|
2022-02-23 19:44:14 +01:00
|
|
|
</i18n.Translate>
|
2021-12-06 19:27:20 +01:00
|
|
|
</WarningBox>
|
|
|
|
)}
|
2022-02-23 19:18:37 +01:00
|
|
|
{error && (
|
|
|
|
<ErrorMessage
|
2022-02-23 19:44:14 +01:00
|
|
|
title={
|
|
|
|
<i18n.Translate>Unable to verify this exchange</i18n.Translate>
|
|
|
|
}
|
2022-02-23 19:18:37 +01:00
|
|
|
description={error}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{confirmationError && (
|
|
|
|
<ErrorMessage
|
2022-02-23 19:44:14 +01:00
|
|
|
title={<i18n.Translate>Unable to add this exchange</i18n.Translate>}
|
2022-02-23 19:18:37 +01:00
|
|
|
description={confirmationError}
|
|
|
|
/>
|
|
|
|
)}
|
2021-11-22 21:34:27 +01:00
|
|
|
<p>
|
2021-12-06 19:27:20 +01:00
|
|
|
<Input invalid={!!error}>
|
2021-11-22 21:34:27 +01:00
|
|
|
<label>URL</label>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
placeholder="https://"
|
2021-12-06 19:27:20 +01:00
|
|
|
value={endpoint}
|
|
|
|
onInput={(e) => updateEndpoint(e.currentTarget.value)}
|
2021-11-22 21:34:27 +01:00
|
|
|
/>
|
|
|
|
</Input>
|
2022-02-23 19:18:37 +01:00
|
|
|
{loading && (
|
|
|
|
<div>
|
2022-02-23 19:44:14 +01:00
|
|
|
<i18n.Translate>loading</i18n.Translate>...
|
2022-02-23 19:18:37 +01:00
|
|
|
</div>
|
|
|
|
)}
|
2021-12-06 19:27:20 +01:00
|
|
|
{result && !loading && (
|
2021-11-22 21:34:27 +01:00
|
|
|
<Fragment>
|
|
|
|
<Input>
|
2022-02-23 19:18:37 +01:00
|
|
|
<label>
|
2022-02-23 19:44:14 +01:00
|
|
|
<i18n.Translate>Version</i18n.Translate>
|
2022-02-23 19:18:37 +01:00
|
|
|
</label>
|
2021-11-22 21:34:27 +01:00
|
|
|
<input type="text" disabled value={result.version} />
|
|
|
|
</Input>
|
|
|
|
<Input>
|
2022-02-23 19:18:37 +01:00
|
|
|
<label>
|
2022-02-23 19:44:14 +01:00
|
|
|
<i18n.Translate>Currency</i18n.Translate>
|
2022-02-23 19:18:37 +01:00
|
|
|
</label>
|
2021-11-22 21:34:27 +01:00
|
|
|
<input type="text" disabled value={result.currency} />
|
|
|
|
</Input>
|
|
|
|
</Fragment>
|
|
|
|
)}
|
|
|
|
</p>
|
|
|
|
</section>
|
|
|
|
<footer>
|
|
|
|
<Button onClick={onCancel}>
|
2022-02-23 19:44:14 +01:00
|
|
|
<i18n.Translate>Cancel</i18n.Translate>
|
2021-11-22 21:34:27 +01:00
|
|
|
</Button>
|
|
|
|
<ButtonPrimary
|
|
|
|
disabled={
|
|
|
|
!result ||
|
|
|
|
!!error ||
|
2022-03-21 16:49:41 +01:00
|
|
|
(!!expectedCurrency && expectedCurrency !== result.currency)
|
2021-11-22 21:34:27 +01:00
|
|
|
}
|
|
|
|
onClick={() => {
|
2021-12-06 19:27:20 +01:00
|
|
|
const url = canonicalizeBaseUrl(endpoint);
|
|
|
|
return onConfirm(url).then((r) =>
|
|
|
|
r ? setConfirmationError(r) : undefined,
|
|
|
|
);
|
2021-11-22 21:34:27 +01:00
|
|
|
}}
|
|
|
|
>
|
2022-02-23 19:44:14 +01:00
|
|
|
<i18n.Translate>Next</i18n.Translate>
|
2021-11-22 21:34:27 +01:00
|
|
|
</ButtonPrimary>
|
|
|
|
</footer>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|