diff options
Diffstat (limited to 'cmd/web/js/poloniex.jsx')
-rw-r--r-- | cmd/web/js/poloniex.jsx | 112 |
1 files changed, 61 insertions, 51 deletions
diff --git a/cmd/web/js/poloniex.jsx b/cmd/web/js/poloniex.jsx index edac368..6019ef8 100644 --- a/cmd/web/js/poloniex.jsx +++ b/cmd/web/js/poloniex.jsx | |||
@@ -1,30 +1,45 @@ | |||
1 | import Api from './api.js'; | 1 | import Api from './api.js'; |
2 | import React from 'react'; | 2 | import React from 'react'; |
3 | import {PFBalance, Assets} from './balance.js'; | ||
3 | 4 | ||
4 | class PoloniexController extends React.Component { | 5 | class PoloniexController extends React.Component { |
5 | constructor(props) { | 6 | constructor(props) { |
6 | super(props); | 7 | super(props); |
7 | this.state = {'flag': 'loading', 'valueCurrency': null, 'balanceValue': null, 'balance': null}; | 8 | this.state = {'flag': 'loading', 'periodStart': null, 'variationP': null, 'balance': null, 'balances': null}; |
8 | } | 9 | } |
9 | 10 | ||
10 | loadBalance = () => { | 11 | testCredentials = () => { |
11 | Api.Call('MARKET_BALANCE', {'name': 'poloniex', 'currency': 'BTC'}, function(err, status, data) { | 12 | Api.Call('MARKET_TEST_CREDENTIALS', {'name': 'poloniex'}, function(err, status, data) { |
12 | if (err) { | 13 | if (err) { |
13 | console.error(err, data); | 14 | console.error(err, data); |
14 | if (err.code === 'invalid_market_credentials') { | 15 | if (err.code === 'invalid_market_credentials') { |
15 | this.setState({'flag': 'invalidCredentials', 'valueCurrency': null, 'balanceValue': null, 'balance': null}); | 16 | this.setState({'flag': 'invalidCredentials', 'variationP': null, 'balance': null, 'balances': null, 'periodStart': null}); |
16 | } else if (err.code === 'ip_restricted_api_key') { | 17 | } else if (err.code === 'ip_restricted_api_key') { |
17 | this.setState({'flag': 'ipRestricted', 'valueCurrency': null, 'balanceValue': null, 'balance': null}); | 18 | this.setState({'flag': 'ipRestricted', 'variationP': null, 'balance': null, 'balances': null, 'periodStart': null}); |
18 | } | 19 | } |
19 | return; | 20 | return; |
20 | } | 21 | } |
21 | 22 | ||
22 | this.setState({'flag': 'ok', 'valueCurrency': data.valueCurrency, 'balanceValue': data.value, 'balance': data.balance}); | 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}); | ||
23 | }.bind(this)); | 38 | }.bind(this)); |
24 | } | 39 | } |
25 | 40 | ||
26 | componentDidMount = () => { | 41 | componentDidMount = () => { |
27 | this.loadBalance(); | 42 | this.testCredentials(); |
28 | } | 43 | } |
29 | 44 | ||
30 | render = () => { | 45 | render = () => { |
@@ -33,10 +48,13 @@ class PoloniexController extends React.Component { | |||
33 | case 'loading': | 48 | case 'loading': |
34 | displayText = 'Loading data from poloniex...'; | 49 | displayText = 'Loading data from poloniex...'; |
35 | break; | 50 | break; |
51 | case 'noReport': | ||
52 | displayText = 'Your account is setup ! Reporting will start next Monday !'; | ||
53 | break; | ||
36 | case 'invalidCredentials': | 54 | case 'invalidCredentials': |
37 | case 'ipRestricted': | 55 | case 'ipRestricted': |
38 | case 'emptyCredentials': | 56 | case 'emptyCredentials': |
39 | displayText = <div>Please provide poloniex credentials in <a href="/account">Account</a> page.</div>; | 57 | displayText = <div>Please provide poloniex credentials in <a href="/account"><u>Account</u></a> page.</div>; |
40 | break; | 58 | break; |
41 | default: | 59 | default: |
42 | displayText = null; | 60 | displayText = null; |
@@ -44,59 +62,55 @@ class PoloniexController extends React.Component { | |||
44 | return ( | 62 | return ( |
45 | <div> | 63 | <div> |
46 | <PoloniexBalance balanceCurrency={this.state.valueCurrency} | 64 | <PoloniexBalance balanceCurrency={this.state.valueCurrency} |
47 | balanceValue={this.state.balanceValue} | 65 | variationP={this.state.variationP} |
66 | periodStart={this.state.periodStart} | ||
48 | balance={this.state.balance} | 67 | balance={this.state.balance} |
68 | balances={this.state.balances} | ||
49 | displayText={displayText}/> | 69 | displayText={displayText}/> |
50 | </div> | 70 | </div> |
51 | ); | 71 | ); |
52 | } | 72 | } |
53 | } | 73 | } |
54 | 74 | ||
55 | class CurrencyLogo extends React.Component { | 75 | class Panel extends React.Component { |
56 | render = () => { | 76 | render = () => { |
57 | return <img className="currency-logo" | 77 | if (this.props.component === null) { |
58 | src={'/public/icons/black/' + this.props.currency.toLowerCase() + '.svg' } | 78 | return <div></div>; |
59 | title={this.props.currency} | 79 | } |
60 | alt={this.props.currency} />; | 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>); | ||
61 | } | 91 | } |
62 | } | 92 | } |
63 | 93 | ||
64 | class PoloniexBalance extends React.Component { | 94 | class PoloniexBalance extends React.Component { |
65 | constructor(props) { | ||
66 | super(props); | ||
67 | this.state = {'hideMsg': true, 'msg': '', 'msgOk': false}; | ||
68 | } | ||
69 | |||
70 | computeCurrencyRatio = (currency) => { | ||
71 | return (parseFloat(this.props.balance[currency].btcValue) / parseFloat(this.props.balanceValue) * 100.0).toFixed(1); | ||
72 | } | ||
73 | 95 | ||
74 | render = () => { | 96 | render = () => { |
75 | var dashboard = null; | 97 | var balancePanel = null; |
76 | 98 | var assetsPanel = null; | |
77 | if (this.props.balanceValue !== null) { | 99 | var messagePanel = null; |
78 | 100 | ||
79 | var balance = Object.keys(this.props.balance).map(function(currency) { | 101 | if (this.props.variationP !== null) { |
80 | return <div key={currency}> | 102 | balancePanel = |
81 | <div> | ||
82 | <CurrencyLogo currency={currency} /> {this.props.balance[currency].amount} {currency} ({this.computeCurrencyRatio(currency)}%) | ||
83 | </div> | ||
84 | </div>; | ||
85 | }.bind(this)); | ||
86 | |||
87 | dashboard = | ||
88 | <div className="row"> | 103 | <div className="row"> |
89 | <div className="col-6 align-self-center h-100 balances"> | 104 | <div className="col-12 offset-md-1 col-md-10 align-self-center h-100 balances"> |
90 | {balance} | 105 | <PFBalance variationP={this.props.variationP} balance={this.props.balance} periodStart={this.props.periodStart}/> |
91 | </div> | ||
92 | <div className="offset-1 col-5 h-100 align-self-center"> | ||
93 | <div className="text-center"> | ||
94 | Balance ({this.props.balanceCurrency}): <span>{this.props.balanceValue}</span><CurrencyLogo currency={this.props.balanceCurrency} /> | ||
95 | </div> | ||
96 | </div> | 106 | </div> |
97 | </div>; | 107 | </div>; |
108 | |||
109 | assetsPanel = | ||
110 | <Assets balances={this.props.balances} />; | ||
111 | |||
98 | } else { | 112 | } else { |
99 | dashboard = | 113 | messagePanel = |
100 | <div className="row"> | 114 | <div className="row"> |
101 | <div className="col-12 text-center"> | 115 | <div className="col-12 text-center"> |
102 | <span>{this.props.displayText}</span> | 116 | <span>{this.props.displayText}</span> |
@@ -105,15 +119,11 @@ class PoloniexBalance extends React.Component { | |||
105 | } | 119 | } |
106 | 120 | ||
107 | return ( | 121 | return ( |
108 | <div className="row"> | 122 | <React.Fragment> |
109 | <div className="box offset-2 col-8 portfolio"> | 123 | <Panel title="Balance" component={balancePanel}/> |
110 | <div className="row"> | 124 | <Panel title="Assets" component={assetsPanel}/> |
111 | <div className="col-4">Portfolio</div> | 125 | <Panel title="Balance" component={messagePanel}/> |
112 | </div> | 126 | </React.Fragment> |
113 | <hr/> | ||
114 | {dashboard} | ||
115 | </div> | ||
116 | </div> | ||
117 | ); | 127 | ); |
118 | } | 128 | } |
119 | } | 129 | } |