wallet-core/packages/taler-wallet-webextension/src/cta/Tip.tsx

139 lines
3.8 KiB
TypeScript
Raw Normal View History

2017-11-30 04:07:36 +01:00
/*
This file is part of TALER
(C) 2017 GNUnet e.V.
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.
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
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
* Page shown to the user to accept or ignore a tip from a merchant.
2017-11-30 04:07:36 +01:00
*
2022-02-23 19:18:37 +01:00
* @author sebasjm <dold@taler.net>
2017-11-30 04:07:36 +01:00
*/
import { PrepareTipResult } from "@gnu-taler/taler-util";
2021-11-16 17:59:53 +01:00
import { h, VNode } from "preact";
import { useEffect, useState } from "preact/hooks";
2022-01-25 14:29:29 +01:00
import { Loading } from "../components/Loading";
import { useTranslationContext } from "../context/translation";
import { AmountView } from "../renderHtml";
import * as wxApi from "../wxApi";
2021-08-13 23:04:05 +02:00
interface Props {
2021-11-15 15:18:58 +01:00
talerTipUri?: string;
2021-08-13 23:04:05 +02:00
}
export interface ViewProps {
prepareTipResult: PrepareTipResult;
onAccept: () => void;
onIgnore: () => void;
}
2021-11-16 17:59:53 +01:00
export function View({
prepareTipResult,
onAccept,
onIgnore,
}: ViewProps): VNode {
const { i18n } = useTranslationContext();
2021-11-15 15:18:58 +01:00
return (
<section class="main">
<h1>GNU Taler Wallet</h1>
<article class="fade">
{prepareTipResult.accepted ? (
<span>
<i18n.Translate>
2022-02-23 19:18:37 +01:00
Tip from <code>{prepareTipResult.merchantBaseUrl}</code> accepted.
Check your transactions list for more details.
</i18n.Translate>
2021-11-15 15:18:58 +01:00
</span>
) : (
2021-08-13 23:04:05 +02:00
<div>
<p>
<i18n.Translate>
2022-02-23 19:18:37 +01:00
The merchant <code>{prepareTipResult.merchantBaseUrl}</code> is
offering you a tip of{" "}
<strong>
<AmountView amount={prepareTipResult.tipAmountEffective} />
</strong>{" "}
via the exchange <code>{prepareTipResult.exchangeBaseUrl}</code>
</i18n.Translate>
2021-08-13 23:04:05 +02:00
</p>
2022-02-23 19:18:37 +01:00
<button onClick={onAccept}>
<i18n.Translate>Accept tip</i18n.Translate>
2022-02-23 19:18:37 +01:00
</button>
<button onClick={onIgnore}>
<i18n.Translate>Ignore</i18n.Translate>
2022-02-23 19:18:37 +01:00
</button>
2021-08-13 23:04:05 +02:00
</div>
)}
2021-11-15 15:18:58 +01:00
</article>
</section>
);
2021-05-07 23:10:27 +02:00
}
2021-11-16 17:59:53 +01:00
export function TipPage({ talerTipUri }: Props): VNode {
const { i18n } = useTranslationContext();
const [updateCounter, setUpdateCounter] = useState<number>(0);
const [prepareTipResult, setPrepareTipResult] = useState<
PrepareTipResult | undefined
2021-05-07 15:38:28 +02:00
>(undefined);
const [tipIgnored, setTipIgnored] = useState(false);
useEffect(() => {
2021-05-07 23:10:27 +02:00
if (!talerTipUri) return;
const doFetch = async (): Promise<void> => {
const p = await wxApi.prepareTip({ talerTipUri });
setPrepareTipResult(p);
};
doFetch();
}, [talerTipUri, updateCounter]);
const doAccept = async () => {
if (!prepareTipResult) {
return;
}
await wxApi.acceptTip({ walletTipId: prepareTipResult?.walletTipId });
setUpdateCounter(updateCounter + 1);
};
const doIgnore = () => {
setTipIgnored(true);
};
2021-05-07 23:10:27 +02:00
if (!talerTipUri) {
2022-02-23 19:18:37 +01:00
return (
<span>
<i18n.Translate>missing tip uri</i18n.Translate>
2022-02-23 19:18:37 +01:00
</span>
);
2021-05-07 23:10:27 +02:00
}
if (tipIgnored) {
2022-02-23 19:18:37 +01:00
return (
<span>
<i18n.Translate>You've ignored the tip.</i18n.Translate>
2022-02-23 19:18:37 +01:00
</span>
);
}
if (!prepareTipResult) {
2022-01-25 14:29:29 +01:00
return <Loading />;
}
2021-11-15 15:18:58 +01:00
return (
<View
prepareTipResult={prepareTipResult}
onAccept={doAccept}
onIgnore={doIgnore}
/>
);
}