]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blame - cmd/web/js/poloniex.jsx
Various fixes.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / cmd / web / js / poloniex.jsx
CommitLineData
989fb5c7 1import Api from './api.js';
989fb5c7 2import React from 'react';
3
4class PoloniexController extends React.Component {
5 constructor(props) {
6 super(props);
78e3e81d 7 this.state = {'flag': 'loading', 'valueCurrency': null, 'balanceValue': null, 'balance': null};
989fb5c7 8 }
9
10 loadBalance = () => {
2f91f20a 11 Api.Call('MARKET_BALANCE', {'name': 'poloniex', 'currency': 'BTC'}, function(err, status, data) {
12 if (err) {
13 console.error(err, data);
14 if (err.code === 'invalid_market_credentials') {
78e3e81d 15 this.setState({'flag': 'invalidCredentials', 'valueCurrency': null, 'balanceValue': null, 'balance': null});
908ee2dd 16 } else if (err.code === 'ip_restricted_api_key') {
78e3e81d 17 this.setState({'flag': 'ipRestricted', 'valueCurrency': null, 'balanceValue': null, 'balance': null});
2f91f20a 18 }
19 return;
20 }
7a9e5112 21
78e3e81d 22 this.setState({'flag': 'ok', 'valueCurrency': data.valueCurrency, 'balanceValue': data.value, 'balance': data.balance});
7a9e5112 23 }.bind(this));
989fb5c7 24 }
25
26 componentDidMount = () => {
16e43cc7 27 this.loadBalance();
989fb5c7 28 }
29
30 render = () => {
2f91f20a 31 var displayText = null;
32 switch (this.state.flag) {
33 case 'loading':
34 displayText = 'Loading data from poloniex...';
35 break;
36 case 'invalidCredentials':
908ee2dd 37 case 'ipRestricted':
2f91f20a 38 case 'emptyCredentials':
16e43cc7 39 displayText = <div>Please provide poloniex credentials in <a href="/account">Account</a> page.</div>;
2f91f20a 40 break;
41 default:
42 displayText = null;
43 }
44 return (
45 <div>
46 <PoloniexBalance balanceCurrency={this.state.valueCurrency}
47 balanceValue={this.state.balanceValue}
48 balance={this.state.balance}
49 displayText={displayText}/>
2f91f20a 50 </div>
51 );
52 }
989fb5c7 53}
54
6b3f0ad0 55class CurrencyLogo extends React.Component {
56 render = () => {
57 return <img className="currency-logo"
58 src={'/public/icons/black/' + this.props.currency.toLowerCase() + '.svg' }
59 title={this.props.currency}
60 alt={this.props.currency} />;
61 }
62}
63
989fb5c7 64class PoloniexBalance extends React.Component {
65 constructor(props) {
66 super(props);
67 this.state = {'hideMsg': true, 'msg': '', 'msgOk': false};
68 }
2f91f20a 69
50c6eea6 70 computeCurrencyRatio = (currency) => {
71 return (parseFloat(this.props.balance[currency].btcValue) / parseFloat(this.props.balanceValue) * 100.0).toFixed(1);
72 }
73
989fb5c7 74 render = () => {
2f91f20a 75 var dashboard = null;
76
77 if (this.props.balanceValue !== null) {
78
79 var balance = Object.keys(this.props.balance).map(function(currency) {
6b3f0ad0 80 return <div key={currency}>
50c6eea6 81 <div>
82 <CurrencyLogo currency={currency} /> {this.props.balance[currency].amount} {currency} ({this.computeCurrencyRatio(currency)}%)
83 </div>
6b3f0ad0 84 </div>;
2f91f20a 85 }.bind(this));
86
989fb5c7 87 dashboard =
88 <div className="row">
50c6eea6 89 <div className="col-6 align-self-center h-100 balances">
2f91f20a 90 {balance}
2f91f20a 91 </div>
50c6eea6 92 <div className="offset-1 col-5 h-100 align-self-center">
989fb5c7 93 <div className="text-center">
6b3f0ad0 94 Balance ({this.props.balanceCurrency}): <span>{this.props.balanceValue}</span><CurrencyLogo currency={this.props.balanceCurrency} />
2f91f20a 95 </div>
96 </div>
989fb5c7 97 </div>;
16e43cc7 98 } else {
989fb5c7 99 dashboard =
100 <div className="row">
101 <div className="col-12 text-center">
2f91f20a 102 <span>{this.props.displayText}</span>
103 </div>
989fb5c7 104 </div>;
16e43cc7 105 }
2f91f20a 106
107 return (
989fb5c7 108 <div className="row">
6b3f0ad0 109 <div className="box offset-2 col-8 portfolio">
989fb5c7 110 <div className="row">
111 <div className="col-4">Portfolio</div>
2f91f20a 112 </div>
113 <hr/>
114 {dashboard}
115 </div>
116 </div>
117 );
118 }
989fb5c7 119}
2f91f20a 120
989fb5c7 121export default PoloniexController;