From 972674ce2e02d3fff9dfeeadce9c5dae8143e53a Mon Sep 17 00:00:00 2001 From: Alex Liebowitz Date: Mon, 21 Mar 2016 02:31:25 -0400 Subject: [PATCH] Implement lbry.call() and get balance display working --- js/gui.js | 15 +++++++++++++-- js/lbry.js | 25 +++++++++++++++++++------ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/js/gui.js b/js/gui.js index 943a9855f..39e2ecab1 100644 --- a/js/gui.js +++ b/js/gui.js @@ -101,12 +101,23 @@ var logoStyle = { }; var Header = React.createClass({ + getInitialState: function() { + return { + balance: 0 + }; + }, + componentDidMount: function() { + lbry.getBalance(function(balance) { + this.setState({ + balance: balance + }); + }.bind(this)); + }, render: function() { - var balance = lbry.getBalance(); return (
- +
diff --git a/js/lbry.js b/js/lbry.js index 96d161ad9..69c461cb6 100644 --- a/js/lbry.js +++ b/js/lbry.js @@ -6,13 +6,26 @@ var lbry = { } }; -lbry.call = function(method, params, callback) +lbry.call = function (method, params, callback) { - /* - * XHR magic - */ - //when XHR returns and is successful - // callback(JSON.parse(xhr.responseText)); + var xhr = new XMLHttpRequest; + xhr.addEventListener('load', function() { + // The response from the HTTP endpoint has a "result" key containing a JSON string of the output of the JSON-RPC method itself + var method_output = JSON.parse(JSON.parse(xhr.responseText).result); + + if (method_output.code !== 200) { + throw new Error('Call to method ' + method + ' failed with message: ' + method_output.message); + } + + callback(method_output.result); + }); + xhr.open('POST', 'http://localhost:5279/lbryapi', true); + xhr.send(JSON.stringify({ + 'jsonrpc': '2.0', + 'method': method, + 'params': params, + 'id': 0 + })); } //core