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