wallet-core/pages/confirm-contract.tsx

209 lines
5.4 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
import {substituteFulfillmentUrl} from "../lib/wallet/helpers";
2016-10-19 20:16:01 +02:00
import {Contract, AmountJson, IExchangeInfo} 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";
2016-10-19 20:16:01 +02:00
import {getExchanges} from "../lib/wallet/wxApi";
2015-12-16 10:45:16 +01:00
2016-10-05 17:04:57 +02:00
interface DetailState {
collapsed: boolean;
2016-10-19 20:16:01 +02:00
exchanges: null|IExchangeInfo[];
2016-10-05 17:04:57 +02:00
}
interface DetailProps {
2016-10-19 20:16:01 +02:00
contract: Contract
collapsed: boolean
2016-10-05 17:04:57 +02:00
}
class Details extends React.Component<DetailProps, DetailState> {
2016-10-19 20:16:01 +02:00
constructor(props: DetailProps) {
super(props);
2016-10-19 23:27:46 +02:00
this.setState({
2016-10-19 20:16:01 +02:00
collapsed: props.collapsed,
exchanges: null
2016-10-19 23:27:46 +02:00
});
console.log("initial state:", this.state);
2016-10-19 20:16:01 +02:00
this.update();
}
2016-10-19 23:27:46 +02:00
componentWillReceiveProps(props: DetailProps) {
this.setState({collapsed: props.collapsed} as any);
}
2016-10-19 20:16:01 +02:00
async update() {
let exchanges = await getExchanges();
this.setState({exchanges} as any);
2016-10-05 17:04:57 +02:00
}
render() {
console.log("details collapsed (state)", this.state.collapsed);
console.log("details collapsed (prop)", this.props.collapsed);
if (this.state.collapsed) {
2016-10-05 17:38:02 +02:00
return (
<div>
<button className="linky"
2016-10-19 20:16:01 +02:00
onClick={() => { this.setState({collapsed: false} as any)}}>
2016-10-05 17:38:02 +02:00
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"
2016-10-19 20:16:01 +02:00
onClick={() => this.setState({collapsed: true} as any)}>
2016-10-07 17:10:22 +02:00
show less details
</button>
<div>
Accepted exchanges:
<ul>
{this.props.contract.exchanges.map(
2016-10-07 17:10:22 +02:00
e => <li>{`${e.url}: ${e.master_pub}`}</li>)}
</ul>
2016-10-19 20:16:01 +02:00
Exchanges in the wallet:
<ul>
{(this.state.exchanges || []).map(
2016-10-19 20:16:01 +02:00
(e: IExchangeInfo) =>
<li>{`${e.baseUrl}: ${e.masterPublicKey}`}</li>)}
</ul>
2016-10-07 17:10:22 +02:00
</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 React.Component<ContractPromptProps, ContractPromptState> {
2016-10-05 17:04:57 +02:00
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-19 23:27:46 +02:00
this.setState({} as any);
2016-10-05 17:04:57 +02:00
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-19 20:16:01 +02:00
this.setState({} as any);
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() {
let c = this.props.offer.contract;
2016-10-07 17:10:22 +02:00
return (
<div>
{renderContract(c)}
<button onClick={() => this.doPayment()}
disabled={this.state.payDisabled}
2016-10-07 17:10:22 +02:00
className="accept">
Confirm payment
</button>
{(this.state.error ? <p className="errorbox">{this.state.error}</p> : <p />)}
<Details contract={c} collapsed={!this.state.error}/>
2016-10-07 17:10:22 +02:00
</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;
ReactDOM.render(<ContractPrompt offer={offer}/>, document.getElementById(
2016-10-07 17:10:22 +02:00
"contract")!);
2016-03-01 19:46:20 +01:00
}