var Api = require('./api.js').Api; var classNames = require('classnames'); module.exports.PoloniexController = React.createClass({ getInitialState: function() { return {'apiKey': '', 'apiSecret': '', 'flag': 'loading', 'valueCurrency': null, 'balanceValue': null, 'balance': null}; }, handleCredentialsChange: function(key, secret) { this.setState({'apiKey': key, 'apiSecret': secret}); }, handleCredentialsSubmit: function() { if (!this.state.apiKey || !this.state.apiSecret) { return; } Api.Call('UPDATE_MARKET', {'key': this.state.apiKey, 'secret': this.state.apiSecret, 'name': 'poloniex'}, function(err, status, data) { if (err) { console.error(err, data); return; } this.setState({'flag': 'loading', 'valueCurrency': null, 'balanceValue': null, 'balance': null}); this.loadBalance(); }.bind(this)); }, loadBalance: function() { Api.Call('MARKET_BALANCE', {'name': 'poloniex', 'currency': 'BTC'}, function(err, status, data) { if (err) { console.error(err, data); if (err.code === 'invalid_market_credentials') { this.setState({'flag': 'invalidCredentials', 'valueCurrency': null, 'balanceValue': null, 'balance': null}); } return; } this.setState({'flag': 'ok', 'valueCurrency': data.valueCurrency, 'balanceValue': data.value, 'balance': data.balance}); }.bind(this)); }, componentDidMount: function() { Api.Call('MARKET', {'name': 'poloniex'}, function(err, status, data) { if (err) { console.error(err, data); return; } var flag = this.state.flag; if (!data.key || !data.secret) { flag = 'emptyCredentials'; } else { this.loadBalance(); } this.setState({'apiKey': data.key, 'apiSecret': data.secret, 'flag': flag}); }.bind(this)); }, render: function() { var displayText = null; switch (this.state.flag) { case 'loading': displayText = 'Loading data from poloniex...'; break; case 'invalidCredentials': displayText = 'Invalid poloniex credentials'; break; case 'emptyCredentials': displayText = 'Please provide poloniex credentials'; break; default: displayText = null; } return (
); } }); var PoloniexBalance = React.createClass({ getInitialState: function() { return {'hideMsg': true, 'msg': '', 'msgOk': false}; }, render: function() { var dashboard = null; if (this.props.balanceValue !== null) { var balance = Object.keys(this.props.balance).map(function(currency) { return
{this.props.balance[currency]}
; }.bind(this)); dashboard = (
{balance}
Balance ({this.props.balanceCurrency}): {this.props.balanceValue}
); } else { dashboard = (
{this.props.displayText}
); } return (
Portfolio

{dashboard}
); } }); module.exports.PoloniexBalance = PoloniexBalance; var PoloniexCredentialsForm = React.createClass({ getInitialState: function() { return {'hideMsg': true, 'msg': '', 'editMode': false, 'msgOk': false}; }, handleSubmit: function(e) { this.props.onCredentialsSubmit(); this.setState({'editMode': false}); e.preventDefault(); }, handleApiKeyChange: function(event) { this.props.onCredentialsChange(event.target.value, this.props.apiSecret); }, handleApiSecretChange: function(event) { this.props.onCredentialsChange(this.props.apiKey, event.target.value); }, onEditClick: function() { this.setState({'editMode': true}); }, render: function() { var submitType = (this.state.editMode === true) ? 'submit' : 'hidden'; var buttonDisplay = (this.state.editMode === true) ? 'none' : 'inline'; var secretDisplayed = (this.state.editMode === true) ? this.props.apiSecret : 'XXXXXXX'; var keyDisplayed = (this.state.editMode === true) ? this.props.apiKey : 'XXXXXXX'; return (
Poloniex credentials
); } }); module.exports.PoloniexCredentialsForm = PoloniexCredentialsForm;