]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blame - cmd/web/js/poloniex.jsx
Account page.
[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);
16e43cc7 7 this.state = {'apiRequested': false, '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') {
16e43cc7 15 this.setState({'flag': 'invalidCredentials', 'apiRequested': true, 'valueCurrency': null, 'balanceValue': null, 'balance': null});
908ee2dd 16 } else if (err.code === 'ip_restricted_api_key') {
16e43cc7 17 this.setState({'flag': 'ipRestricted', 'apiRequested': true, 'valueCurrency': null, 'balanceValue': null, 'balance': null});
2f91f20a 18 }
19 return;
20 }
7a9e5112 21
16e43cc7 22 this.setState({'flag': 'ok', 'apiRequested': true, '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 }
adf936f6 44 if (this.state.apiRequested === false) {
45 return <div></div>;
46 }
2f91f20a 47 return (
48 <div>
49 <PoloniexBalance balanceCurrency={this.state.valueCurrency}
50 balanceValue={this.state.balanceValue}
51 balance={this.state.balance}
52 displayText={displayText}/>
2f91f20a 53 </div>
54 );
55 }
989fb5c7 56}
57
6b3f0ad0 58class CurrencyLogo extends React.Component {
59 render = () => {
60 return <img className="currency-logo"
61 src={'/public/icons/black/' + this.props.currency.toLowerCase() + '.svg' }
62 title={this.props.currency}
63 alt={this.props.currency} />;
64 }
65}
66
989fb5c7 67class PoloniexBalance extends React.Component {
68 constructor(props) {
69 super(props);
70 this.state = {'hideMsg': true, 'msg': '', 'msgOk': false};
71 }
2f91f20a 72
50c6eea6 73 computeCurrencyRatio = (currency) => {
74 return (parseFloat(this.props.balance[currency].btcValue) / parseFloat(this.props.balanceValue) * 100.0).toFixed(1);
75 }
76
989fb5c7 77 render = () => {
2f91f20a 78 var dashboard = null;
79
80 if (this.props.balanceValue !== null) {
81
82 var balance = Object.keys(this.props.balance).map(function(currency) {
6b3f0ad0 83 return <div key={currency}>
50c6eea6 84 <div>
85 <CurrencyLogo currency={currency} /> {this.props.balance[currency].amount} {currency} ({this.computeCurrencyRatio(currency)}%)
86 </div>
6b3f0ad0 87 </div>;
2f91f20a 88 }.bind(this));
89
989fb5c7 90 dashboard =
91 <div className="row">
50c6eea6 92 <div className="col-6 align-self-center h-100 balances">
2f91f20a 93 {balance}
2f91f20a 94 </div>
50c6eea6 95 <div className="offset-1 col-5 h-100 align-self-center">
989fb5c7 96 <div className="text-center">
6b3f0ad0 97 Balance ({this.props.balanceCurrency}): <span>{this.props.balanceValue}</span><CurrencyLogo currency={this.props.balanceCurrency} />
2f91f20a 98 </div>
99 </div>
989fb5c7 100 </div>;
16e43cc7 101 } else {
989fb5c7 102 dashboard =
103 <div className="row">
104 <div className="col-12 text-center">
2f91f20a 105 <span>{this.props.displayText}</span>
106 </div>
989fb5c7 107 </div>;
16e43cc7 108 }
2f91f20a 109
110 return (
989fb5c7 111 <div className="row">
6b3f0ad0 112 <div className="box offset-2 col-8 portfolio">
989fb5c7 113 <div className="row">
114 <div className="col-4">Portfolio</div>
2f91f20a 115 </div>
116 <hr/>
117 {dashboard}
118 </div>
119 </div>
120 );
121 }
989fb5c7 122}
2f91f20a 123
989fb5c7 124export default PoloniexController;