wallet-core/pages/confirm-contract.tsx

185 lines
4.7 KiB
TypeScript
Raw Normal View History

2015-12-25 22:42:14 +01:00
/*
This file is part of TALER
(C) 2015 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
2016-07-07 17:59:29 +02:00
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
2015-12-25 22:42:14 +01:00
*/
2016-03-01 19:46:20 +01:00
/**
* Page shown to the user to confirm entering
* a contract.
*
* @author Florian Dold
*/
2015-12-16 10:45:16 +01:00
2016-10-05 17:04:57 +02:00
/// <reference path="../lib/decl/preact.d.ts" />
import {substituteFulfillmentUrl} from "../lib/wallet/helpers";
2016-09-12 20:25:56 +02:00
import {Contract, AmountJson} from "../lib/wallet/types";
2016-09-29 16:31:55 +02:00
import {renderContract, prettyAmount} from "../lib/wallet/renderHtml";
2016-03-02 02:16:18 +01:00
"use strict";
2015-12-16 10:45:16 +01:00
2016-10-05 17:04:57 +02:00
interface DetailState {
collapsed: boolean;
}
interface DetailProps {
contract: Contract;
}
class Details extends preact.Component<DetailProps, DetailState> {
constructor() {
super();
this.state = {
collapsed: true
};
}
render(props: DetailProps, state: DetailState) {
if (state.collapsed) {
2016-10-05 17:38:02 +02:00
return (
<div>
<button className="linky"
onClick={() => { this.setState({collapsed: false})}}>
show more details
</button>
</div>
);
2016-03-02 02:16:18 +01:00
} else {
2016-10-07 17:10:22 +02:00
return (
<div>
<button className="linky"
onClick={() => this.setState({collapsed: true})}>
show less details
</button>
<div>
Accepted exchanges:
<ul>
{props.contract.exchanges.map(
e => <li>{`${e.url}: ${e.master_pub}`}</li>)}
</ul>
</div>
</div>);
2016-03-02 02:16:18 +01:00
}
}
2016-10-05 17:04:57 +02:00
}
2016-03-02 02:16:18 +01:00
2016-10-05 17:04:57 +02:00
interface ContractPromptProps {
offer: any;
}
interface ContractPromptState {
error: string|null;
payDisabled: boolean;
}
class ContractPrompt extends preact.Component<ContractPromptProps, ContractPromptState> {
constructor() {
super();
this.state = {
error: null,
payDisabled: true,
2016-01-26 17:21:17 +01:00
}
2016-10-05 17:04:57 +02:00
}
2016-01-26 17:21:17 +01:00
2016-10-05 17:04:57 +02:00
componentWillMount() {
this.checkPayment();
}
componentWillUnmount() {
// FIXME: abort running ops
}
2016-01-26 17:21:17 +01:00
2016-10-05 17:04:57 +02:00
checkPayment() {
let msg = {
type: 'check-pay',
detail: {
offer: this.props.offer
}
};
chrome.runtime.sendMessage(msg, (resp) => {
2016-04-27 06:03:04 +02:00
if (resp.error) {
console.log("check-pay error", JSON.stringify(resp));
switch (resp.error) {
case "coins-insufficient":
2016-10-05 17:04:57 +02:00
this.state.error = i18n`You have insufficient funds of the requested currency in your wallet.`;
2016-04-27 06:03:04 +02:00
break;
default:
2016-10-05 17:04:57 +02:00
this.state.error = `Error: ${resp.error}`;
2016-04-27 06:03:04 +02:00
break;
}
2016-10-05 17:04:57 +02:00
this.state.payDisabled = true;
2016-04-27 06:03:04 +02:00
} else {
2016-10-05 17:04:57 +02:00
this.state.payDisabled = false;
this.state.error = null;
2016-04-27 06:03:04 +02:00
}
2016-10-05 17:04:57 +02:00
this.forceUpdate();
window.setTimeout(() => this.checkPayment(), 300);
2016-04-27 06:03:04 +02:00
});
}
2016-10-05 17:04:57 +02:00
doPayment() {
let d = {offer: this.props.offer};
2016-01-26 17:21:17 +01:00
chrome.runtime.sendMessage({type: 'confirm-pay', detail: d}, (resp) => {
2016-02-15 15:53:59 +01:00
if (resp.error) {
2016-01-26 17:21:17 +01:00
console.log("confirm-pay error", JSON.stringify(resp));
2016-02-15 15:53:59 +01:00
switch (resp.error) {
case "coins-insufficient":
2016-10-05 17:04:57 +02:00
this.state.error = "You do not have enough coins of the" +
" requested currency.";
2016-02-15 15:53:59 +01:00
break;
default:
2016-10-05 17:04:57 +02:00
this.state.error = `Error: ${resp.error}`;
2016-02-15 15:53:59 +01:00
break;
}
2016-10-05 17:04:57 +02:00
preact.rerender();
2015-12-17 22:56:24 +01:00
return;
2015-12-16 10:45:16 +01:00
}
2016-01-26 17:21:17 +01:00
let c = d.offer.contract;
console.log("contract", c);
2016-02-15 15:53:59 +01:00
document.location.href = substituteFulfillmentUrl(c.fulfillment_url,
2016-10-05 17:04:57 +02:00
this.props.offer);
2015-12-16 10:45:16 +01:00
});
2016-01-26 17:21:17 +01:00
}
2016-10-05 17:04:57 +02:00
render(props: ContractPromptProps, state: ContractPromptState) {
let c = props.offer.contract;
2016-10-07 17:10:22 +02:00
return (
<div>
{renderContract(c)}
<button onClick={() => this.doPayment()}
disabled={state.payDisabled}
className="accept">
Confirm payment
</button>
(state.error ? <p className="errorbox">{state.error}</p> : <p />)
<Details contract={c} />
</div>
2016-10-05 17:04:57 +02:00
);
}
}
export function main() {
let url = URI(document.location.href);
let query: any = URI.parseQuery(url.query());
let offer = JSON.parse(query.offer);
console.dir(offer);
let contract = offer.contract;
2016-10-07 17:10:22 +02:00
preact.render(<ContractPrompt offer={offer}/>, document.getElementById(
"contract")!);
2016-03-01 19:46:20 +01:00
}