]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blobdiff - cmd/web/js/poloniex.jsx
Fix message on empty credentials.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / cmd / web / js / poloniex.jsx
index 8b577b4a256e2e4c365fe1ebfcebc8c423dcdbcb..b28bc26a7b39f4dd78c252aa804573b5c89766fb 100644 (file)
@@ -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 = <div>Please provide poloniex credentials in <a href="/account"><u>Account</u></a> page.</div>;
         break;
       default:
         displayText = null;
@@ -70,116 +64,70 @@ module.exports.PoloniexController = React.createClass({
     return (
       <div>
         <PoloniexBalance  balanceCurrency={this.state.valueCurrency}
-                          balanceValue={this.state.balanceValue}
+                          variationP={this.state.variationP}
+                          periodStart={this.state.periodStart}
                           balance={this.state.balance}
+                          balances={this.state.balances}
                           displayText={displayText}/>
-        <PoloniexCredentialsForm onLoadCredentials={this.onLoadCredentials}
-                                 onCredentialsSubmit={this.handleCredentialsSubmit}
-                                 onCredentialsChange={this.handleCredentialsChange}
-                                 apiSecret={this.state.apiSecret}
-                                 apiKey={this.state.apiKey}/>
       </div>
     );
   }
-});
-
-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 <div key={currency}><i className={classNames('cc', currency)}></i> {this.props.balance[currency]}</div>;
-      }.bind(this));
-
-      dashboard = (
-        <div className='row'>
-          <div className='col-4 align-self-center h-100'>
-            <div>
-              {balance}
-            </div>
-          </div>
-          <div className='offset-1 col-7 h-100 align-self-center'>
-            <div className='text-center'>
-              Balance ({this.props.balanceCurrency}): <span>{this.props.balanceValue} <i className={classNames('cc', this.props.balanceCurrency)}></i></span>
-            </div>
-          </div>
-        </div>
-      );
-    } else {
-      dashboard = (
-        <div className='row'>
-          <div className='col-12 text-center'>
-           <span>{this.props.displayText}</span>
-          </div>
-        </div>
+}
 
-      );
+class Panel extends React.Component {
+  render = () => {
+    if (this.props.component === null) {
+      return <div></div>;
     }
 
     return (
-      <div className='row'>
-        <div className='box offset-2 col-8'>
-          <div className='row'>
-            <div className='col-4'>Portfolio</div>
+      <div className="row">
+        <div className="box col-12">
+          <div className="row">
+            <div className="col-4">{this.props.title}</div>
           </div>
           <hr/>
-          {dashboard}
+          {this.props.component}
         </div>
-      </div>
-    );
+      </div>);
   }
-});
-
-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 (
-        <div className='row api-credentials-form'>
-          <div className='offset-3 col-6 box'>
-            <span className='text-center'>Poloniex credentials</span>
-            <hr/>
-            <form role='form' onSubmit={this.handleSubmit}>
-              <label className='w-100'>Key:
-                <input className='form-control' type='text' placeholder='key' value={keyDisplayed} onChange={this.handleApiKeyChange} disabled={!this.state.editMode}/>
-              </label>
-              <label className='w-100'>Secret:
-                <input className='form-control' type='text' placeholder='secret' value={secretDisplayed} onChange={this.handleApiSecretChange} disabled={!this.state.editMode}/>
-              </label>
-              <input className='form-control submit' type={submitType} value='Save' />
-              <button className='form-control submit' style={{display: buttonDisplay}} onSubmit={null} onClick={this.onEditClick} type='button'>Show/Edit</button>
-            </form>
+class PoloniexBalance extends React.Component {
+
+  render = () => {
+    var balancePanel = null;
+    var assetsPanel  = null;
+    var messagePanel = null;
+
+    if (this.props.variationP !== null) {
+      balancePanel =
+        <div className="row">
+          <div className="col-12 offset-md-1 col-md-10 align-self-center h-100 balances">
+            <PFBalance variationP={this.props.variationP} balance={this.props.balance} periodStart={this.props.periodStart}/>
           </div>
-        </div>
-       );
+        </div>;
+
+      assetsPanel =
+        <Assets balances={this.props.balances} />;
+
+    } else {
+      messagePanel =
+        <div className="row">
+          <div className="col-12 text-center">
+           <span>{this.props.displayText}</span>
+          </div>
+        </div>;
+    }
+
+    return (
+      <React.Fragment>
+        <Panel title="Balance" component={balancePanel}/>
+        <Panel title="Assets"  component={assetsPanel}/>
+        <Panel title="Balance" component={messagePanel}/>
+      </React.Fragment>
+    );
   }
-});
+}
 
-module.exports.PoloniexCredentialsForm = PoloniexCredentialsForm;
+export default PoloniexController;