simple view

This commit is contained in:
Florian Dold 2016-04-27 06:50:38 +02:00
parent d0a4f9f934
commit 2321614c48
2 changed files with 89 additions and 20 deletions

View File

@ -56,6 +56,11 @@
padding: 0.3em; padding: 0.3em;
} }
span.spacer {
padding-left: 0.5em;
padding-right: 0.5em;
}
</style> </style>
</head> </head>

View File

@ -76,31 +76,38 @@ class Controller {
callbackUrl: string; callbackUrl: string;
wtTypes: string[]; wtTypes: string[];
detailCollapsed = m.prop<boolean>(true); detailCollapsed = m.prop<boolean>(true);
suggestedExchangeUrl: string;
complexViewRequested = false;
urlOkay = false;
constructor(initialExchangeUrl: string, constructor(suggestedExchangeUrl: string,
amount: AmountJson, amount: AmountJson,
callbackUrl: string, callbackUrl: string,
wt_types: string[]) { wt_types: string[]) {
console.log("creating main controller"); console.log("creating main controller");
this.suggestedExchangeUrl = suggestedExchangeUrl;
this.amount = amount; this.amount = amount;
this.callbackUrl = callbackUrl; this.callbackUrl = callbackUrl;
this.wtTypes = wt_types; this.wtTypes = wt_types;
this.timer = new DelayTimer(800, () => this.update()); this.timer = new DelayTimer(800, () => this.update());
this.url(initialExchangeUrl); this.url(suggestedExchangeUrl);
this.update(); this.update();
} }
private update() { private update() {
this.timer.stop(); this.timer.stop();
const doUpdate = () => { const doUpdate = () => {
this.reserveCreationInfo = null;
if (!this.url()) { if (!this.url()) {
this.statusString = i18n`Please enter a URL`; this.statusString = i18n`Error: URL is empty`;
m.redraw(true);
return; return;
} }
this.statusString = null; this.statusString = null;
let parsedUrl = URI(this.url()); let parsedUrl = URI(this.url());
if (parsedUrl.is("relative")) { if (parsedUrl.is("relative")) {
this.statusString = i18n`The URL you've entered is not valid (must be absolute)`; this.statusString = i18n`Error: URL may not be relative`;
m.redraw(true);
return; return;
} }
@ -114,16 +121,15 @@ class Controller {
this.isValidExchange = true; this.isValidExchange = true;
this.reserveCreationInfo = r; this.reserveCreationInfo = r;
console.dir(r); console.dir(r);
this.statusString = "The exchange base URL is valid!";
m.endComputation(); m.endComputation();
}) })
.catch((e) => { .catch((e) => {
console.log("get exchange info rejected"); console.log("get exchange info rejected");
if (e.hasOwnProperty("httpStatus")) { if (e.hasOwnProperty("httpStatus")) {
this.statusString = `request failed with status ${this.request.status}`; this.statusString = `Error: request failed with status ${this.request.status}`;
} else if (e.hasOwnProperty("errorResponse")) { } else if (e.hasOwnProperty("errorResponse")) {
let resp = e.errorResponse; let resp = e.errorResponse;
this.statusString = `error: ${resp.error} (${resp.hint})`; this.statusString = `Error: ${resp.error} (${resp.hint})`;
} }
m.endComputation(); m.endComputation();
}); });
@ -131,7 +137,7 @@ class Controller {
doUpdate(); doUpdate();
console.log("got update"); console.log("got update", this.url());
} }
reset() { reset() {
@ -188,8 +194,7 @@ class Controller {
} }
} }
function view(ctrl: Controller): any {
function view(ctrl: Controller) {
let controls = []; let controls = [];
let mx = (x, ...args) => controls.push(m(x, ...args)); let mx = (x, ...args) => controls.push(m(x, ...args));
@ -197,6 +202,74 @@ function view(ctrl: Controller) {
i18n.parts`You are about to withdraw ${m("strong", amountToPretty( i18n.parts`You are about to withdraw ${m("strong", amountToPretty(
ctrl.amount))} from your bank account into your wallet.`); ctrl.amount))} from your bank account into your wallet.`);
if (ctrl.complexViewRequested || !ctrl.suggestedExchangeUrl) {
return controls.concat(viewComplex(ctrl));
}
return controls.concat(viewSimple(ctrl));
}
function viewSimple(ctrl: Controller) {
let controls = [];
let mx = (x, ...args) => controls.push(m(x, ...args));
if (ctrl.statusString) {
mx("p", "Error: ", ctrl.statusString);
mx("button.linky", {
onclick: () => {
ctrl.complexViewRequested = true;
}
}, "advanced options");
}
else if (ctrl.reserveCreationInfo) {
mx("button.accept", {
onclick: () => ctrl.confirmReserve(ctrl.reserveCreationInfo,
ctrl.url(),
ctrl.amount,
ctrl.callbackUrl),
disabled: !ctrl.isValidExchange
},
"Accept fees and withdraw");
mx("span.spacer");
mx("button.linky", {
onclick: () => {
ctrl.complexViewRequested = true;
}
}, "advanced options");
let totalCost = Amounts.add(ctrl.reserveCreationInfo.overhead,
ctrl.reserveCreationInfo.withdrawFee).amount;
mx("p", `Withdraw cost: ${amountToPretty(totalCost)}`);
} else {
mx("p", "Please wait ...");
}
return controls;
}
function viewComplex(ctrl: Controller) {
let controls = [];
let mx = (x, ...args) => controls.push(m(x, ...args));
mx("button.accept", {
onclick: () => ctrl.confirmReserve(ctrl.reserveCreationInfo,
ctrl.url(),
ctrl.amount,
ctrl.callbackUrl),
disabled: !ctrl.isValidExchange
},
"Accept fees and withdraw");
mx("span.spacer");
mx("button.linky", {
onclick: () => {
ctrl.complexViewRequested = false;
}
}, "back to simple view");
mx("br");
mx("input", mx("input",
{ {
className: "url", className: "url",
@ -208,18 +281,9 @@ function view(ctrl: Controller) {
mx("br"); mx("br");
mx("button.accept", {
onclick: () => ctrl.confirmReserve(ctrl.reserveCreationInfo,
ctrl.url(),
ctrl.amount,
ctrl.callbackUrl),
disabled: !ctrl.isValidExchange
},
"Accept fees and withdraw");
if (ctrl.statusString) { if (ctrl.statusString) {
mx("p", ctrl.statusString); mx("p", ctrl.statusString);
} else { } else if (!ctrl.reserveCreationInfo) {
mx("p", "Checking URL, please wait ..."); mx("p", "Checking URL, please wait ...");
} }