X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=cmd%2Fweb%2Fjs%2Fpoloniex.jsx;h=b28bc26a7b39f4dd78c252aa804573b5c89766fb;hb=a3e231e4c3ac1a41bae49a2cea2874d9e8f1b9ea;hp=877198d81fed44dfa5312032ab128010daabd801;hpb=7a9e5112eaaea58d55f181d3e5296e4ff839921c;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FFront.git diff --git a/cmd/web/js/poloniex.jsx b/cmd/web/js/poloniex.jsx index 877198d..b28bc26 100644 --- a/cmd/web/js/poloniex.jsx +++ b/cmd/web/js/poloniex.jsx @@ -1,49 +1,133 @@ -var Api = require('./api.js').Api; -var App = require('./app.js'); -var classNames = require('classnames'); - -module.exports.PoloniexForm = React.createClass({ - getInitialState: function() { - return {'hideMsg': true, 'msg': '', 'msgOk': false, 'apiSecret': this.props.apiSecret, 'apiKey': this.props.apiKey}; - }, - handleSubmit: function(e) { - Api.Call('UPDATE_MARKET', {'key': this.state.apiKey, 'secret': this.state.apiSecret, 'name': 'poloniex'}, function(err, status, data) { +import Api from './api.js'; +import React from 'react'; +import {PFBalance, Assets} from './balance.js'; + +class PoloniexController extends React.Component { + constructor(props) { + super(props); + this.state = {'flag': 'loading', 'periodStart': null, 'variationP': null, 'balance': null, 'balances': null}; + } + + testCredentials = () => { + Api.Call('MARKET_TEST_CREDENTIALS', {'name': 'poloniex'}, function(err, status, data) { if (err) { console.error(err, data); - this.displayMessage(App.errorCodeToMessage(err.code), false); + if (err.code === 'invalid_market_credentials') { + this.setState({'flag': 'invalidCredentials', 'variationP': null, 'balance': null, 'balances': null, 'periodStart': null}); + } else if (err.code === 'ip_restricted_api_key') { + this.setState({'flag': 'ipRestricted', 'variationP': null, 'balance': null, 'balances': null, 'periodStart': null}); + } else if (err.code === 'market_credentials_not_configured') { + this.setState({'flag': 'emptyCredentials'}); + } return; } - this.displayMessage('OK', true); + this.loadPortfolio(); + }.bind(this)); + } + loadPortfolio = () => { + Api.Call('MARKET_GET_PORTFOLIO', {'name': 'poloniex'}, function(err, status, data) { + if (err) { + console.error(err, data); + if (err.code === 'not_found') { + this.setState({'flag': 'noReport', 'variationP': null, 'balance': null, 'balances': null, 'periodStart': null}); + } + return; + } + + this.setState({'flag': 'ok', 'variationP': data.performance.variationP, 'balance': data.value, 'balances': data.balances, 'periodStart': data.periodStart}); }.bind(this)); - e.preventDefault(); - }, - handleApiKeyChange: function(event) { - this.setState({'apiKey': event.target.value}); - }, - handleApiSecretChange: function(event) { - this.setState({'apiSecret': event.target.value}); - }, - hideMessage: function() { - this.setState({'hideMsg': true}); - }, - displayMessage: function(msg, ok) { - this.setState({'msg': msg, 'msgOk': ok, 'hideMsg': false}); - }, - render: function() { - var cName = classNames('form-message', {'hidden': this.state.hideMsg, 'message-ok': this.state.msgOk}); + } + + componentDidMount = () => { + this.testCredentials(); + } + + render = () => { + var displayText = null; + switch (this.state.flag) { + case 'loading': + displayText = 'Loading data from poloniex...'; + break; + case 'noReport': + displayText = 'Your account is setup ! Reporting will start next Monday !'; + break; + case 'invalidCredentials': + case 'ipRestricted': + case 'emptyCredentials': + displayText =
Please provide poloniex credentials in Account page.
; + break; + default: + displayText = null; + } return ( -
-
-
- - - -
{this.state.msg}
-
+
+ +
+ ); + } +} + +class Panel extends React.Component { + render = () => { + if (this.props.component === null) { + return
; + } + + return ( +
+
+
+
{this.props.title}
+
+ {this.props.component}
- ); +
); + } +} + +class PoloniexBalance extends React.Component { + + render = () => { + var balancePanel = null; + var assetsPanel = null; + var messagePanel = null; + + if (this.props.variationP !== null) { + balancePanel = +
+
+ +
+
; + + assetsPanel = + ; + + } else { + messagePanel = +
+
+ {this.props.displayText} +
+
; + } + + return ( + + + + + + ); } -}); +} + +export default PoloniexController;