wallet-core/pages/confirm-contract.tsx

188 lines
4.9 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-03-02 02:16:18 +01:00
import m from "mithril";
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;
}
let h = preact.h;
class Details extends preact.Component<DetailProps, DetailState> {
constructor() {
super();
this.state = {
collapsed: true
};
}
render(props: DetailProps, state: DetailState) {
if (state.collapsed) {
return h("div", {},
h("button", {
className: "linky",
onClick: () => {
this.setState({collapsed: false});
}
}, "show more details"));
2016-03-02 02:16:18 +01:00
} else {
2016-10-05 17:04:57 +02:00
return h("div", {},
h("button", {
className: "linky",
onClick: () => {
this.setState({collapsed: true});
}
}, "show less details"),
h("div", {},
"Accepted exchanges:",
h("ul", {},
...props.contract.exchanges.map(
e => h("li", {}, `${e.url}: ${e.master_pub}`)))));
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;
return h("div", {},
renderContract(c),
h("button",
{
onClick: () => this.doPayment(),
disabled: state.payDisabled,
"className": "accept"
},
i18n`Confirm Payment`),
(state.error ? h("p",
{className: "errorbox"},
state.error) : h("p", "")),
h(Details, {contract: c})
);
}
}
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;
let prompt = h(ContractPrompt, {offer});
preact.render(prompt, document.getElementById("contract")!);
2016-03-01 19:46:20 +01:00
}