import Api from './api.js'; import classNames from 'classnames'; import React from 'react'; class PoloniexController extends React.Component { constructor(props) { super(props); this.state = {'apiKey': '', 'apiSecret': '', 'apiRequested': false, 'flag': 'loading', 'valueCurrency': null, 'balanceValue': null, 'balance': null}; } handleCredentialsChange = (key, secret) => { this.setState({'apiKey': key, 'apiSecret': secret}); } handleCredentialsSubmit = () => { 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 = () => { 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 = () => { Api.Call('MARKET', {'name': 'poloniex'}, function(err, status, data) { this.setState({'apiRequested': true}); 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 = () => { 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; } if (this.state.apiRequested === false) { return
; } return (