]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blobdiff - cmd/web/js/poloniex.jsx
Refactor Portfolio balance.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / cmd / web / js / poloniex.jsx
index edac368e84d07f02d2060d838481d7853cab04a3..6019ef8cb59402ac143e1fdc4cf9d1668360b7fa 100644 (file)
@@ -1,30 +1,45 @@
 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', 'valueCurrency': null, 'balanceValue': null, 'balance': null};
+    this.state = {'flag': 'loading', 'periodStart': null, 'variationP': null, 'balance': null, 'balances': null};
   }
 
-  loadBalance = () => {
-    Api.Call('MARKET_BALANCE', {'name': 'poloniex', 'currency': 'BTC'}, function(err, status, data) {
+  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', 'valueCurrency': null, 'balanceValue': null, 'balance': null});
+          this.setState({'flag': 'ipRestricted', 'variationP': null, 'balance': null, 'balances': null, 'periodStart': null});
         }
         return;
       }
 
-      this.setState({'flag': 'ok', 'valueCurrency': data.valueCurrency, 'balanceValue': data.value, 'balance': data.balance});
+      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));
   }
 
   componentDidMount = () => {
-    this.loadBalance();
+    this.testCredentials();
   }
 
   render = () => {
@@ -33,10 +48,13 @@ class PoloniexController extends React.Component {
       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 = <div>Please provide poloniex credentials in <a href="/account">Account</a> page.</div>;
+        displayText = <div>Please provide poloniex credentials in <a href="/account"><u>Account</u></a> page.</div>;
         break;
       default:
         displayText = null;
@@ -44,59 +62,55 @@ class PoloniexController extends React.Component {
     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}/>
       </div>
     );
   }
 }
 
-class CurrencyLogo extends React.Component {
+class Panel extends React.Component {
   render = () => {
-    return <img className="currency-logo"
-                src={'/public/icons/black/' + this.props.currency.toLowerCase() + '.svg' }
-                title={this.props.currency}
-                alt={this.props.currency} />;
+    if (this.props.component === null) {
+      return <div></div>;
+    }
+
+    return (
+      <div className="row">
+        <div className="box col-12">
+          <div className="row">
+            <div className="col-4">{this.props.title}</div>
+          </div>
+          <hr/>
+          {this.props.component}
+        </div>
+      </div>);
   }
 }
 
 class PoloniexBalance extends React.Component {
-  constructor(props) {
-    super(props);
-    this.state = {'hideMsg': true, 'msg': '', 'msgOk': false};
-  }
-
-  computeCurrencyRatio = (currency) => {
-    return (parseFloat(this.props.balance[currency].btcValue) / parseFloat(this.props.balanceValue) * 100.0).toFixed(1);
-  }
 
   render = () => {
-    var dashboard = null;
-
-    if (this.props.balanceValue !== null) {
+    var balancePanel = null;
+    var assetsPanel  = null;
+    var messagePanel = null;
 
-      var balance = Object.keys(this.props.balance).map(function(currency) {
-        return <div key={currency}>
-                 <div>
-                 <CurrencyLogo currency={currency} /> {this.props.balance[currency].amount} {currency} ({this.computeCurrencyRatio(currency)}%)
-                 </div>
-               </div>;
-      }.bind(this));
-
-      dashboard =
+    if (this.props.variationP !== null) {
+      balancePanel =
         <div className="row">
-          <div className="col-6 align-self-center h-100 balances">
-              {balance}
-          </div>
-          <div className="offset-1 col-5 h-100 align-self-center">
-            <div className="text-center">
-              Balance ({this.props.balanceCurrency}): <span>{this.props.balanceValue}</span><CurrencyLogo currency={this.props.balanceCurrency} />
-            </div>
+          <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>;
+
+      assetsPanel =
+        <Assets balances={this.props.balances} />;
+
     } else {
-      dashboard =
+      messagePanel =
         <div className="row">
           <div className="col-12 text-center">
            <span>{this.props.displayText}</span>
@@ -105,15 +119,11 @@ class PoloniexBalance extends React.Component {
     }
 
     return (
-      <div className="row">
-        <div className="box offset-2 col-8 portfolio">
-          <div className="row">
-            <div className="col-4">Portfolio</div>
-          </div>
-          <hr/>
-          {dashboard}
-        </div>
-      </div>
+      <React.Fragment>
+        <Panel title="Balance" component={balancePanel}/>
+        <Panel title="Assets"  component={assetsPanel}/>
+        <Panel title="Balance" component={messagePanel}/>
+      </React.Fragment>
     );
   }
 }