wallet-core/packages/taler-wallet-webextension/src/wallet/ExchangeAddConfirm.tsx

142 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-02-23 19:18:37 +01:00
import { i18n, Translate } from "@gnu-taler/taler-util";
2021-11-22 21:34:27 +01:00
import { Fragment, h, VNode } from "preact";
import { useState } from "preact/hooks";
2022-01-04 21:06:17 +01:00
import { Button, ButtonSuccess, ButtonWarning } from "../components/styled";
2021-11-22 21:34:27 +01:00
import { TermsOfServiceSection } from "../cta/TermsOfServiceSection";
import { useAsyncAsHook } from "../hooks/useAsyncAsHook";
2022-01-04 21:06:17 +01:00
import { buildTermsOfServiceState, TermsState } from "../utils/index";
2021-11-22 21:34:27 +01:00
import * as wxApi from "../wxApi";
export interface Props {
url: string;
onCancel: () => void;
onConfirm: () => void;
}
export function ExchangeAddConfirmPage({
url,
onCancel,
onConfirm,
}: Props): VNode {
const detailsHook = useAsyncAsHook(async () => {
const tos = await wxApi.getExchangeTos(url, ["text/xml"]);
const tosState = buildTermsOfServiceState(tos);
return { tos: tosState };
});
const termsNotFound: TermsState = {
status: "notfound",
version: "",
content: undefined,
};
const terms = !detailsHook
? undefined
: detailsHook.hasError
? termsNotFound
: detailsHook.response.tos;
// const [errorAccepting, setErrorAccepting] = useState<string | undefined>(
// undefined,
// );
const onAccept = async (): Promise<void> => {
if (!terms) return;
try {
await wxApi.setExchangeTosAccepted(url, terms.version);
} catch (e) {
if (e instanceof Error) {
// setErrorAccepting(e.message);
}
}
};
return (
<View
url={url}
onAccept={onAccept}
onCancel={onCancel}
onConfirm={onConfirm}
terms={terms}
/>
);
}
export interface ViewProps {
url: string;
terms: TermsState | undefined;
onAccept: (b: boolean) => Promise<void>;
onCancel: () => void;
onConfirm: () => void;
}
export function View({
url,
terms,
onAccept: doAccept,
onConfirm,
onCancel,
}: ViewProps): VNode {
const needsReview =
!terms || terms.status === "changed" || terms.status === "new";
const [reviewed, setReviewed] = useState<boolean>(false);
return (
<Fragment>
<section>
2022-02-23 19:18:37 +01:00
<h1>
<Translate>Review terms of service</Translate>
</h1>
2021-11-22 21:34:27 +01:00
<div>
2022-02-23 19:18:37 +01:00
<Translate>Exchange URL</Translate>:
2021-11-22 21:34:27 +01:00
<a href={url} target="_blank" rel="noreferrer">
{url}
</a>
</div>
</section>
{terms && (
<TermsOfServiceSection
reviewed={reviewed}
reviewing={true}
terms={terms}
onAccept={(value) =>
doAccept(value).then(() => {
setReviewed(value);
})
}
/>
)}
<footer>
<Button onClick={onCancel}>
2022-02-23 19:18:37 +01:00
<Translate>Cancel</Translate>
2021-11-22 21:34:27 +01:00
</Button>
{!terms && (
<Button disabled>
2022-02-23 19:18:37 +01:00
<Translate>Loading terms..</Translate>
2021-11-22 21:34:27 +01:00
</Button>
)}
{terms && (
<Fragment>
{needsReview && !reviewed && (
<ButtonSuccess disabled upperCased onClick={onConfirm}>
2022-02-23 19:18:37 +01:00
<Translate>Add exchange</Translate>
2021-11-22 21:34:27 +01:00
</ButtonSuccess>
)}
{(terms.status === "accepted" || (needsReview && reviewed)) && (
<ButtonSuccess upperCased onClick={onConfirm}>
2022-02-23 19:18:37 +01:00
<Translate>Add exchange</Translate>
2021-11-22 21:34:27 +01:00
</ButtonSuccess>
)}
{terms.status === "notfound" && (
<ButtonWarning upperCased onClick={onConfirm}>
2022-02-23 19:18:37 +01:00
<Translate>Add exchange anyway</Translate>
2021-11-22 21:34:27 +01:00
</ButtonWarning>
)}
</Fragment>
)}
</footer>
</Fragment>
);
}