/* 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 */ /** * Page shown to the user to accept or ignore a tip from a merchant. * * @author sebasjm */ import { amountFractionalBase, AmountJson, Amounts, PrepareTipResult, } from "@gnu-taler/taler-util"; import { h, VNode } from "preact"; import { useEffect, useState } from "preact/hooks"; import { Loading } from "../components/Loading"; import { useTranslationContext } from "../context/translation"; import * as wxApi from "../wxApi"; interface Props { talerTipUri?: string; } export interface ViewProps { prepareTipResult: PrepareTipResult; onAccept: () => void; onIgnore: () => void; } export function View({ prepareTipResult, onAccept, onIgnore, }: ViewProps): VNode { const { i18n } = useTranslationContext(); return (

GNU Taler Wallet

{prepareTipResult.accepted ? ( Tip from {prepareTipResult.merchantBaseUrl} accepted. Check your transactions list for more details. ) : (

The merchant {prepareTipResult.merchantBaseUrl} is offering you a tip of{" "} {" "} via the exchange {prepareTipResult.exchangeBaseUrl}

)}
); } export function TipPage({ talerTipUri }: Props): VNode { const { i18n } = useTranslationContext(); const [updateCounter, setUpdateCounter] = useState(0); const [prepareTipResult, setPrepareTipResult] = useState< PrepareTipResult | undefined >(undefined); const [tipIgnored, setTipIgnored] = useState(false); useEffect(() => { if (!talerTipUri) return; const doFetch = async (): Promise => { 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); }; if (!talerTipUri) { return ( missing tip uri ); } if (tipIgnored) { return ( You've ignored the tip. ); } if (!prepareTipResult) { return ; } return ( ); } function renderAmount(amount: AmountJson | string): VNode { let a; if (typeof amount === "string") { a = Amounts.parse(amount); } else { a = amount; } if (!a) { return (invalid amount); } const x = a.value + a.fraction / amountFractionalBase; return ( {x} {a.currency} ); } const AmountView = ({ amount }: { amount: AmountJson | string }): VNode => renderAmount(amount);