]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blame - cmd/web/js/poloniex.jsx
Fix message on empty credentials.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / cmd / web / js / poloniex.jsx
CommitLineData
989fb5c7 1import Api from './api.js';
989fb5c7 2import React from 'react';
24e47979 3import {PFBalance, Assets} from './balance.js';
989fb5c7 4
5class PoloniexController extends React.Component {
6 constructor(props) {
7 super(props);
24e47979 8 this.state = {'flag': 'loading', 'periodStart': null, 'variationP': null, 'balance': null, 'balances': null};
989fb5c7 9 }
10
24e47979 11 testCredentials = () => {
12 Api.Call('MARKET_TEST_CREDENTIALS', {'name': 'poloniex'}, function(err, status, data) {
2f91f20a 13 if (err) {
14 console.error(err, data);
15 if (err.code === 'invalid_market_credentials') {
24e47979 16 this.setState({'flag': 'invalidCredentials', 'variationP': null, 'balance': null, 'balances': null, 'periodStart': null});
908ee2dd 17 } else if (err.code === 'ip_restricted_api_key') {
24e47979 18 this.setState({'flag': 'ipRestricted', 'variationP': null, 'balance': null, 'balances': null, 'periodStart': null});
a3e231e4 19 } else if (err.code === 'market_credentials_not_configured') {
20 this.setState({'flag': 'emptyCredentials'});
2f91f20a 21 }
22 return;
23 }
7a9e5112 24
24e47979 25 this.loadPortfolio();
26 }.bind(this));
27 }
28
29 loadPortfolio = () => {
30 Api.Call('MARKET_GET_PORTFOLIO', {'name': 'poloniex'}, function(err, status, data) {
31 if (err) {
32 console.error(err, data);
33 if (err.code === 'not_found') {
34 this.setState({'flag': 'noReport', 'variationP': null, 'balance': null, 'balances': null, 'periodStart': null});
35 }
36 return;
37 }
38
39 this.setState({'flag': 'ok', 'variationP': data.performance.variationP, 'balance': data.value, 'balances': data.balances, 'periodStart': data.periodStart});
7a9e5112 40 }.bind(this));
989fb5c7 41 }
42
43 componentDidMount = () => {
24e47979 44 this.testCredentials();
989fb5c7 45 }
46
47 render = () => {
2f91f20a 48 var displayText = null;
49 switch (this.state.flag) {
50 case 'loading':
51 displayText = 'Loading data from poloniex...';
52 break;
24e47979 53 case 'noReport':
54 displayText = 'Your account is setup ! Reporting will start next Monday !';
55 break;
2f91f20a 56 case 'invalidCredentials':
908ee2dd 57 case 'ipRestricted':
2f91f20a 58 case 'emptyCredentials':
24e47979 59 displayText = <div>Please provide poloniex credentials in <a href="/account"><u>Account</u></a> page.</div>;
2f91f20a 60 break;
61 default:
62 displayText = null;
63 }
64 return (
65 <div>
66 <PoloniexBalance balanceCurrency={this.state.valueCurrency}
24e47979 67 variationP={this.state.variationP}
68 periodStart={this.state.periodStart}
2f91f20a 69 balance={this.state.balance}
24e47979 70 balances={this.state.balances}
2f91f20a 71 displayText={displayText}/>
2f91f20a 72 </div>
73 );
74 }
989fb5c7 75}
76
24e47979 77class Panel extends React.Component {
6b3f0ad0 78 render = () => {
24e47979 79 if (this.props.component === null) {
80 return <div></div>;
81 }
82
83 return (
84 <div className="row">
85 <div className="box col-12">
86 <div className="row">
87 <div className="col-4">{this.props.title}</div>
88 </div>
89 <hr/>
90 {this.props.component}
91 </div>
92 </div>);
6b3f0ad0 93 }
94}
95
989fb5c7 96class PoloniexBalance extends React.Component {
50c6eea6 97
989fb5c7 98 render = () => {
24e47979 99 var balancePanel = null;
100 var assetsPanel = null;
101 var messagePanel = null;
2f91f20a 102
24e47979 103 if (this.props.variationP !== null) {
104 balancePanel =
989fb5c7 105 <div className="row">
24e47979 106 <div className="col-12 offset-md-1 col-md-10 align-self-center h-100 balances">
107 <PFBalance variationP={this.props.variationP} balance={this.props.balance} periodStart={this.props.periodStart}/>
2f91f20a 108 </div>
989fb5c7 109 </div>;
24e47979 110
111 assetsPanel =
112 <Assets balances={this.props.balances} />;
113
16e43cc7 114 } else {
24e47979 115 messagePanel =
989fb5c7 116 <div className="row">
117 <div className="col-12 text-center">
2f91f20a 118 <span>{this.props.displayText}</span>
119 </div>
989fb5c7 120 </div>;
16e43cc7 121 }
2f91f20a 122
123 return (
24e47979 124 <React.Fragment>
125 <Panel title="Balance" component={balancePanel}/>
126 <Panel title="Assets" component={assetsPanel}/>
127 <Panel title="Balance" component={messagePanel}/>
128 </React.Fragment>
2f91f20a 129 );
130 }
989fb5c7 131}
2f91f20a 132
989fb5c7 133export default PoloniexController;