]>
Commit | Line | Data |
---|---|---|
989fb5c7 | 1 | import Api from './api.js'; |
989fb5c7 | 2 | import React from 'react'; |
24e47979 | 3 | import {PFBalance, Assets} from './balance.js'; |
d5b8f0ff | 4 | import Panel from './panel.js'; |
989fb5c7 | 5 | |
6 | class PoloniexController extends React.Component { | |
7 | constructor(props) { | |
8 | super(props); | |
24e47979 | 9 | this.state = {'flag': 'loading', 'periodStart': null, 'variationP': null, 'balance': null, 'balances': null}; |
989fb5c7 | 10 | } |
11 | ||
24e47979 | 12 | testCredentials = () => { |
13 | Api.Call('MARKET_TEST_CREDENTIALS', {'name': 'poloniex'}, function(err, status, data) { | |
2f91f20a | 14 | if (err) { |
15 | console.error(err, data); | |
16 | if (err.code === 'invalid_market_credentials') { | |
24e47979 | 17 | this.setState({'flag': 'invalidCredentials', 'variationP': null, 'balance': null, 'balances': null, 'periodStart': null}); |
908ee2dd | 18 | } else if (err.code === 'ip_restricted_api_key') { |
24e47979 | 19 | this.setState({'flag': 'ipRestricted', 'variationP': null, 'balance': null, 'balances': null, 'periodStart': null}); |
a3e231e4 | 20 | } else if (err.code === 'market_credentials_not_configured') { |
21 | this.setState({'flag': 'emptyCredentials'}); | |
2f91f20a | 22 | } |
23 | return; | |
24 | } | |
7a9e5112 | 25 | |
24e47979 | 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}); | |
7a9e5112 | 41 | }.bind(this)); |
989fb5c7 | 42 | } |
43 | ||
44 | componentDidMount = () => { | |
24e47979 | 45 | this.testCredentials(); |
989fb5c7 | 46 | } |
47 | ||
48 | render = () => { | |
2f91f20a | 49 | var displayText = null; |
50 | switch (this.state.flag) { | |
51 | case 'loading': | |
52 | displayText = 'Loading data from poloniex...'; | |
53 | break; | |
24e47979 | 54 | case 'noReport': |
55 | displayText = 'Your account is setup ! Reporting will start next Monday !'; | |
56 | break; | |
2f91f20a | 57 | case 'invalidCredentials': |
908ee2dd | 58 | case 'ipRestricted': |
2f91f20a | 59 | case 'emptyCredentials': |
24e47979 | 60 | displayText = <div>Please provide poloniex credentials in <a href="/account"><u>Account</u></a> page.</div>; |
2f91f20a | 61 | break; |
62 | default: | |
63 | displayText = null; | |
64 | } | |
65 | return ( | |
66 | <div> | |
67 | <PoloniexBalance balanceCurrency={this.state.valueCurrency} | |
24e47979 | 68 | variationP={this.state.variationP} |
69 | periodStart={this.state.periodStart} | |
2f91f20a | 70 | balance={this.state.balance} |
24e47979 | 71 | balances={this.state.balances} |
2f91f20a | 72 | displayText={displayText}/> |
2f91f20a | 73 | </div> |
74 | ); | |
75 | } | |
989fb5c7 | 76 | } |
77 | ||
78 | class PoloniexBalance extends React.Component { | |
50c6eea6 | 79 | |
989fb5c7 | 80 | render = () => { |
24e47979 | 81 | var balancePanel = null; |
82 | var assetsPanel = null; | |
83 | var messagePanel = null; | |
2f91f20a | 84 | |
24e47979 | 85 | if (this.props.variationP !== null) { |
86 | balancePanel = | |
989fb5c7 | 87 | <div className="row"> |
24e47979 | 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}/> | |
2f91f20a | 90 | </div> |
989fb5c7 | 91 | </div>; |
24e47979 | 92 | |
93 | assetsPanel = | |
94 | <Assets balances={this.props.balances} />; | |
95 | ||
16e43cc7 | 96 | } else { |
24e47979 | 97 | messagePanel = |
989fb5c7 | 98 | <div className="row"> |
99 | <div className="col-12 text-center"> | |
2f91f20a | 100 | <span>{this.props.displayText}</span> |
101 | </div> | |
989fb5c7 | 102 | </div>; |
16e43cc7 | 103 | } |
2f91f20a | 104 | |
105 | return ( | |
24e47979 | 106 | <React.Fragment> |
107 | <Panel title="Balance" component={balancePanel}/> | |
108 | <Panel title="Assets" component={assetsPanel}/> | |
109 | <Panel title="Balance" component={messagePanel}/> | |
110 | </React.Fragment> | |
2f91f20a | 111 | ); |
112 | } | |
989fb5c7 | 113 | } |
2f91f20a | 114 | |
989fb5c7 | 115 | export default PoloniexController; |