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