diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-02-15 11:29:58 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-02-15 11:30:06 +0100 |
commit | 526e88695f6c8a2b3dea4b9befeb2a85ba00b66b (patch) | |
tree | db2a0422d322ad45842cd12e1e107f1f08718f99 /extension/pages | |
parent | c34a6612ec079a78e67325dac8d9a18775abe022 (diff) |
suggest mint based on currency (stub)
Diffstat (limited to 'extension/pages')
-rw-r--r-- | extension/pages/confirm-create-reserve.html | 2 | ||||
-rw-r--r-- | extension/pages/confirm-create-reserve.js | 143 | ||||
-rw-r--r-- | extension/pages/confirm-create-reserve.tsx | 164 |
3 files changed, 195 insertions, 114 deletions
diff --git a/extension/pages/confirm-create-reserve.html b/extension/pages/confirm-create-reserve.html index 522efc872..7bf2b0498 100644 --- a/extension/pages/confirm-create-reserve.html +++ b/extension/pages/confirm-create-reserve.html @@ -24,7 +24,7 @@ <section id="main"> <article> - <div id="mint-selection"></div> + <div class="fade" id="mint-selection"></div> </article> </section> diff --git a/extension/pages/confirm-create-reserve.js b/extension/pages/confirm-create-reserve.js index 124abdf22..d49e709c2 100644 --- a/extension/pages/confirm-create-reserve.js +++ b/extension/pages/confirm-create-reserve.js @@ -13,46 +13,71 @@ You should have received a copy of the GNU General Public License along with TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> */ -System.register(["../lib/wallet/helpers", "../lib/wallet/types"], function(exports_1, context_1) { +System.register(["../lib/wallet/helpers", "../lib/wallet/types", "mithril"], function(exports_1, context_1) { "use strict"; var __moduleName = context_1 && context_1.id; - var helpers_1, types_1; + var helpers_1, types_1, mithril_1; var DelayTimer, Controller; + function view(ctrl) { + var controls = []; + var mx = function (x) { + var args = []; + for (var _i = 1; _i < arguments.length; _i++) { + args[_i - 1] = arguments[_i]; + } + return controls.push(mithril_1.default.apply(void 0, [x].concat(args))); + }; + mx("p", (_a = ["The bank wants to create a reserve over ", "."], _a.raw = ["The bank wants to create a reserve over ", "."], i18n(_a, helpers_1.amountToPretty(ctrl.amount)))); + mx("input", { + className: "url", + type: "text", + spellcheck: false, + value: ctrl.url(), + oninput: mithril_1.default.withAttr("value", ctrl.onUrlChanged.bind(ctrl)), + }); + mx("button", { + onclick: function () { return ctrl.confirmReserve(ctrl.url(), ctrl.amount, ctrl.callbackUrl); }, + disabled: !ctrl.isValidMint + }, "Confirm mint selection"); + if (ctrl.statusString) { + mx("p", ctrl.statusString); + } + else { + mx("p", "Checking URL, please wait ..."); + } + return mithril_1.default("div", controls); + var _a; + } + function getSuggestedMint(currency) { + // TODO: make this request go to the wallet backend + // Right now, this is a stub. + var defaultMint = { + "KUDOS": "http://mint.test.taler.net" + }; + var mint = defaultMint[currency]; + if (!mint) { + mint = ""; + } + return Promise.resolve(mint); + } function main() { var url = URI(document.location.href); var query = URI.parseQuery(url.query()); var amount = types_1.AmountJson.checked(JSON.parse(query.amount)); var callback_url = query.callback_url; - var MintSelection = { - controller: function () { return new Controller(); }, - view: function (ctrl) { - var controls = []; - var mx = function () { - var args = []; - for (var _i = 0; _i < arguments.length; _i++) { - args[_i - 0] = arguments[_i]; - } - return controls.push(m.apply(void 0, args)); - }; - mx("p", (_a = ["The bank wants to create a reserve over ", "."], _a.raw = ["The bank wants to create a reserve over ", "."], i18n(_a, helpers_1.amountToPretty(amount)))); - mx("input.url", { - type: "text", - spellcheck: false, - oninput: m.withAttr("value", ctrl.onUrlChanged.bind(ctrl)), - }); - if (ctrl.isValidMint) { - mx("button", { - onclick: function () { return ctrl.confirmReserve(ctrl.url, amount, callback_url); } - }, "Confirm mint selection"); - } - if (ctrl.errorString) { - mx("p", ctrl.errorString); - } - return m("div", controls); - var _a; - } - }; - m.mount(document.getElementById("mint-selection"), MintSelection); + var bank_url = query.bank_url; + getSuggestedMint(amount.currency) + .then(function (suggestedMintUrl) { + var controller = function () { return new Controller(suggestedMintUrl, amount, callback_url); }; + var MintSelection = { controller: controller, view: view }; + mithril_1.default.mount(document.getElementById("mint-selection"), MintSelection); + }) + .catch(function (e) { + // TODO: provide more context information, maybe factor it out into a + // TODO:generic error reporting function or component. + document.body.innerText = "Fatal error: \"" + e.message + "\"."; + console.error("got backend error \"" + e.message + "\""); + }); } exports_1("main", main); return { @@ -62,6 +87,9 @@ System.register(["../lib/wallet/helpers", "../lib/wallet/types"], function(expor }, function (types_1_1) { types_1 = types_1_1; + }, + function (mithril_1_1) { + mithril_1 = mithril_1_1; }], execute: function() { "use strict"; @@ -77,39 +105,48 @@ System.register(["../lib/wallet/helpers", "../lib/wallet/types"], function(expor } DelayTimer.prototype.bump = function () { var _this = this; - if (this.timerId !== null) { - window.clearTimeout(this.timerId); - } + this.stop(); var handler = function () { _this.f(); }; this.timerId = window.setTimeout(handler, this.ms); }; + DelayTimer.prototype.stop = function () { + if (this.timerId !== null) { + window.clearTimeout(this.timerId); + } + }; return DelayTimer; }()); Controller = (function () { - function Controller() { + function Controller(initialMintUrl, amount, callbackUrl) { var _this = this; - this.url = null; - this.errorString = null; + this.url = mithril_1.default.prop(); + this.statusString = null; this.isValidMint = false; - this.update(); + this.amount = amount; + this.callbackUrl = callbackUrl; this.timer = new DelayTimer(800, function () { return _this.update(); }); + this.url(initialMintUrl); + this.update(); } Controller.prototype.update = function () { var _this = this; + this.timer.stop(); var doUpdate = function () { - if (!_this.url) { - _this.errorString = (_a = ["Please enter a URL"], _a.raw = ["Please enter a URL"], i18n(_a)); + if (!_this.url()) { + _this.statusString = (_a = ["Please enter a URL"], _a.raw = ["Please enter a URL"], i18n(_a)); + mithril_1.default.endComputation(); return; } - _this.errorString = null; - var parsedUrl = URI(_this.url); + _this.statusString = null; + var parsedUrl = URI(_this.url()); if (parsedUrl.is("relative")) { - _this.errorString = (_b = ["The URL you've entered is not valid (must be absolute)"], _b.raw = ["The URL you've entered is not valid (must be absolute)"], i18n(_b)); + _this.statusString = (_b = ["The URL you've entered is not valid (must be absolute)"], _b.raw = ["The URL you've entered is not valid (must be absolute)"], i18n(_b)); + mithril_1.default.endComputation(); return; } - var keysUrl = URI("/keys").absoluteTo(helpers_1.canonicalizeBaseUrl(_this.url)); + var keysUrl = URI("/keys").absoluteTo(helpers_1.canonicalizeBaseUrl(_this.url())); console.log("requesting keys from '" + keysUrl + "'"); _this.request = new XMLHttpRequest(); _this.request.onreadystatechange = function () { @@ -117,33 +154,33 @@ System.register(["../lib/wallet/helpers", "../lib/wallet/types"], function(expor switch (_this.request.status) { case 200: _this.isValidMint = true; + _this.statusString = "The mint base URL is valid!"; break; case 0: - _this.errorString = "unknown request error"; + _this.statusString = "unknown request error"; break; default: - _this.errorString = "request failed with status " + _this.request.status; + _this.statusString = "request failed with status " + _this.request.status; break; } - m.redraw(); } + mithril_1.default.endComputation(); }; _this.request.open("get", keysUrl.href()); _this.request.send(); var _a, _b; }; + mithril_1.default.startComputation(); doUpdate(); - m.redraw(); console.log("got update"); }; Controller.prototype.reset = function () { this.isValidMint = false; - this.errorString = null; + this.statusString = null; if (this.request) { this.request.abort(); this.request = null; } - m.redraw(); }; Controller.prototype.confirmReserve = function (mint, amount, callback_url) { var _this = this; @@ -170,7 +207,7 @@ System.register(["../lib/wallet/helpers", "../lib/wallet/types"], function(expor } else { _this.reset(); - _this.errorString = ("Oops, something went wrong." + + _this.statusString = ("Oops, something went wrong." + ("The wallet responded with error status (" + rawResp.error + ").")); } }; @@ -178,7 +215,7 @@ System.register(["../lib/wallet/helpers", "../lib/wallet/types"], function(expor }; Controller.prototype.onUrlChanged = function (url) { this.reset(); - this.url = url; + this.url(url); this.timer.bump(); }; return Controller; diff --git a/extension/pages/confirm-create-reserve.tsx b/extension/pages/confirm-create-reserve.tsx index 8cb46559b..0effc752c 100644 --- a/extension/pages/confirm-create-reserve.tsx +++ b/extension/pages/confirm-create-reserve.tsx @@ -14,14 +14,14 @@ TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> */ +/// <reference path="../lib/decl/mithril.d.ts" /> + import {amountToPretty, canonicalizeBaseUrl} from "../lib/wallet/helpers"; import {AmountJson, CreateReserveResponse} from "../lib/wallet/types"; +import m from "mithril"; "use strict"; -declare var m: any; - - /** * Execute something after a delay, with the possibility * to reset the delay. @@ -37,43 +37,55 @@ class DelayTimer { } bump() { - if (this.timerId !== null) { - window.clearTimeout(this.timerId); - } + this.stop(); const handler = () => { this.f(); }; this.timerId = window.setTimeout(handler, this.ms); } + + stop() { + if (this.timerId !== null) { + window.clearTimeout(this.timerId); + } + } } class Controller { - url = null; - errorString = null; + url = m.prop<string>(); + statusString = null; isValidMint = false; private timer: DelayTimer; private request: XMLHttpRequest; + amount: AmountJson; + callbackUrl: string; - constructor() { - this.update(); + constructor(initialMintUrl: string, amount: AmountJson, callbackUrl: string) { + this.amount = amount; + this.callbackUrl = callbackUrl; this.timer = new DelayTimer(800, () => this.update()); + this.url(initialMintUrl); + this.update(); } - update() { + private update() { + this.timer.stop(); const doUpdate = () => { - if (!this.url) { - this.errorString = i18n`Please enter a URL`; + if (!this.url()) { + this.statusString = i18n`Please enter a URL`; + m.endComputation(); return; } - this.errorString = null; - let parsedUrl = URI(this.url); + this.statusString = null; + let parsedUrl = URI(this.url()); if (parsedUrl.is("relative")) { - this.errorString = i18n`The URL you've entered is not valid (must be absolute)`; + this.statusString = i18n`The URL you've entered is not valid (must be absolute)`; + m.endComputation(); return; } - const keysUrl = URI("/keys").absoluteTo(canonicalizeBaseUrl(this.url)); + const keysUrl = URI("/keys").absoluteTo(canonicalizeBaseUrl(this.url())); console.log(`requesting keys from '${keysUrl}'`); @@ -83,34 +95,36 @@ class Controller { switch (this.request.status) { case 200: this.isValidMint = true; + this.statusString = "The mint base URL is valid!"; break; case 0: - this.errorString = `unknown request error`; + this.statusString = `unknown request error`; break; default: - this.errorString = `request failed with status ${this.request.status}`; + this.statusString = `request failed with status ${this.request.status}`; break; } - m.redraw(); } + m.endComputation(); }; this.request.open("get", keysUrl.href()); this.request.send(); }; + m.startComputation(); doUpdate(); - m.redraw(); + + console.log("got update"); } reset() { this.isValidMint = false; - this.errorString = null; + this.statusString = null; if (this.request) { this.request.abort(); this.request = null; } - m.redraw(); } confirmReserve(mint: string, amount: AmountJson, callback_url: string) { @@ -136,7 +150,7 @@ class Controller { document.location.href = url.href(); } else { this.reset(); - this.errorString = ( + this.statusString = ( `Oops, something went wrong.` + `The wallet responded with error status (${rawResp.error}).`); } @@ -146,50 +160,80 @@ class Controller { onUrlChanged(url: string) { this.reset(); - this.url = url; + this.url(url); this.timer.bump(); } } -export function main() { - const url = URI(document.location.href); - const query: any = URI.parseQuery(url.query()); - const amount = AmountJson.checked(JSON.parse(query.amount)); - const callback_url = query.callback_url; +function view(ctrl: Controller) { + let controls = []; + let mx = (x: string, ...args) => controls.push(m(x, ...args)); + + mx("p", + i18n`The bank wants to create a reserve over ${amountToPretty( + ctrl.amount)}.`); + mx("input", + { + className: "url", + type: "text", + spellcheck: false, + value: ctrl.url(), + oninput: m.withAttr("value", ctrl.onUrlChanged.bind(ctrl)), + }); + + mx("button", { + onclick: () => ctrl.confirmReserve(ctrl.url(), + ctrl.amount, + ctrl.callbackUrl), + disabled: !ctrl.isValidMint + }, + "Confirm mint selection"); + + if (ctrl.statusString) { + mx("p", ctrl.statusString); + } else { + mx("p", "Checking URL, please wait ..."); + } - var MintSelection = { - controller: () => new Controller(), - view(ctrl: Controller) { - let controls = []; - let mx = (...args) => controls.push(m(...args)); - - mx("p", - i18n`The bank wants to create a reserve over ${amountToPretty( - amount)}.`); - mx("input.url", - { - type: "text", - spellcheck: false, - oninput: m.withAttr("value", ctrl.onUrlChanged.bind(ctrl)), - }); - - if (ctrl.isValidMint) { - mx("button", { - onclick: () => ctrl.confirmReserve(ctrl.url, - amount, - callback_url) - }, - "Confirm mint selection"); - } + return m("div", controls); +} - if (ctrl.errorString) { - mx("p", ctrl.errorString); - } - return m("div", controls); - } +function getSuggestedMint(currency: string): Promise<string> { + // TODO: make this request go to the wallet backend + // Right now, this is a stub. + const defaultMint = { + "KUDOS": "http://mint.test.taler.net" }; - m.mount(document.getElementById("mint-selection"), MintSelection); + let mint = defaultMint[currency]; + + if (!mint) { + mint = "" + } + + return Promise.resolve(mint); +} + + +export function main() { + const url = URI(document.location.href); + const query: any = URI.parseQuery(url.query()); + const amount = AmountJson.checked(JSON.parse(query.amount)); + const callback_url = query.callback_url; + const bank_url = query.bank_url; + + getSuggestedMint(amount.currency) + .then((suggestedMintUrl) => { + const controller = () => new Controller(suggestedMintUrl, amount, callback_url); + var MintSelection = {controller, view}; + m.mount(document.getElementById("mint-selection"), MintSelection); + }) + .catch((e) => { + // TODO: provide more context information, maybe factor it out into a + // TODO:generic error reporting function or component. + document.body.innerText = `Fatal error: "${e.message}".`; + console.error(`got backend error "${e.message}"`); + }); }
\ No newline at end of file |