X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=cmd%2Fweb%2Fjs%2Fpoloniex.jsx;h=b28bc26a7b39f4dd78c252aa804573b5c89766fb;hb=refs%2Ftags%2Fv0.0.12;hp=8b577b4a256e2e4c365fe1ebfcebc8c423dcdbcb;hpb=2f91f20a8645339385ada602684f4957f20f4da4;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FFront.git diff --git a/cmd/web/js/poloniex.jsx b/cmd/web/js/poloniex.jsx index 8b577b4..b28bc26 100644 --- a/cmd/web/js/poloniex.jsx +++ b/cmd/web/js/poloniex.jsx @@ -1,68 +1,62 @@ -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; - } +import Api from './api.js'; +import React from 'react'; +import {PFBalance, Assets} from './balance.js'; - 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) { +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); if (err.code === 'invalid_market_credentials') { - this.setState({'flag': 'invalidCredentials', 'valueCurrency': null, 'balanceValue': null, 'balance': null}); + 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.setState({'flag': 'ok', 'valueCurrency': data.valueCurrency, 'balanceValue': data.value, 'balance': data.balance}); + this.loadPortfolio(); }.bind(this)); - }, - componentDidMount: function() { - Api.Call('MARKET', {'name': 'poloniex'}, function(err, status, data) { + } + + 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; } - 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}); + this.setState({'flag': 'ok', 'variationP': data.performance.variationP, 'balance': data.value, 'balances': data.balances, 'periodStart': data.periodStart}); }.bind(this)); - }, - render: function() { + } + + componentDidMount = () => { + this.testCredentials(); + } + + render = () => { var displayText = null; switch (this.state.flag) { case 'loading': displayText = 'Loading data from poloniex...'; break; - case 'invalidCredentials': - displayText = 'Invalid poloniex credentials'; + case 'noReport': + displayText = 'Your account is setup ! Reporting will start next Monday !'; break; + case 'invalidCredentials': + case 'ipRestricted': case 'emptyCredentials': - displayText = 'Please provide poloniex credentials'; + displayText =