diff options
author | jloup <jeanloup.jamet@gmail.com> | 2018-05-09 19:44:17 +0200 |
---|---|---|
committer | jloup <jeanloup.jamet@gmail.com> | 2018-05-09 19:44:17 +0200 |
commit | 24e4797900b3d2edf642fdb547bc22357a5b39ad (patch) | |
tree | a2e62dd7382eb4c9ba817c25ba2fa15a659638d3 /cmd/web/js | |
parent | 78e3e81ddf01f41102f3f4e32c5a3955cf5fb04f (diff) | |
download | Front-24e4797900b3d2edf642fdb547bc22357a5b39ad.tar.gz Front-24e4797900b3d2edf642fdb547bc22357a5b39ad.tar.zst Front-24e4797900b3d2edf642fdb547bc22357a5b39ad.zip |
Refactor Portfolio balance.
Diffstat (limited to 'cmd/web/js')
-rw-r--r-- | cmd/web/js/account.jsx | 9 | ||||
-rw-r--r-- | cmd/web/js/api.js | 17 | ||||
-rw-r--r-- | cmd/web/js/balance.jsx | 99 | ||||
-rw-r--r-- | cmd/web/js/poloniex.jsx | 112 |
4 files changed, 174 insertions, 63 deletions
diff --git a/cmd/web/js/account.jsx b/cmd/web/js/account.jsx index 43e7083..d30abe7 100644 --- a/cmd/web/js/account.jsx +++ b/cmd/web/js/account.jsx | |||
@@ -8,7 +8,7 @@ class PoloniexConfiguration extends React.Component { | |||
8 | } | 8 | } |
9 | 9 | ||
10 | checkCredentials = () => { | 10 | checkCredentials = () => { |
11 | Api.Call('MARKET_BALANCE', {'name': 'poloniex', 'currency': 'BTC'}, function(err, status, data) { | 11 | Api.Call('MARKET_TEST_CREDENTIALS', {'name': 'poloniex'}, function(err, status, data) { |
12 | if (err) { | 12 | if (err) { |
13 | console.error(err, data); | 13 | console.error(err, data); |
14 | if (err.code === 'invalid_market_credentials') { | 14 | if (err.code === 'invalid_market_credentials') { |
@@ -28,13 +28,6 @@ class PoloniexConfiguration extends React.Component { | |||
28 | } | 28 | } |
29 | 29 | ||
30 | handleCredentialsSubmit = () => { | 30 | handleCredentialsSubmit = () => { |
31 | |||
32 | /* | ||
33 | *If (!this.state.apiKey || !this.state.apiSecret) { | ||
34 | * return; | ||
35 | *} | ||
36 | */ | ||
37 | |||
38 | Api.Call('UPDATE_MARKET', {'key': this.state.apiKey, 'secret': this.state.apiSecret, 'name': 'poloniex'}, function(err, status, data) { | 31 | Api.Call('UPDATE_MARKET', {'key': this.state.apiKey, 'secret': this.state.apiSecret, 'name': 'poloniex'}, function(err, status, data) { |
39 | if (err) { | 32 | if (err) { |
40 | console.error(err, data); | 33 | console.error(err, data); |
diff --git a/cmd/web/js/api.js b/cmd/web/js/api.js index c9b4ef5..f892a6b 100644 --- a/cmd/web/js/api.js +++ b/cmd/web/js/api.js | |||
@@ -74,15 +74,24 @@ var ApiEndpoints = { | |||
74 | return '/market/' + params.name; | 74 | return '/market/' + params.name; |
75 | } | 75 | } |
76 | }, | 76 | }, |
77 | 'MARKET_BALANCE': { | 77 | 'MARKET_TEST_CREDENTIALS': { |
78 | 'type': 'GET', | 78 | 'type': 'GET', |
79 | 'auth': true, | 79 | 'auth': true, |
80 | 'parameters': [ | 80 | 'parameters': [ |
81 | {'name': 'name', 'mandatory': true, 'inquery': false}, | 81 | {'name': 'name', 'mandatory': true, 'inquery': false}, |
82 | {'name': 'currency', 'mandatory': true, 'inquery': true}, | 82 | ], |
83 | 'buildUrl': function(params) { | ||
84 | return '/market/' + params.name + '/test-credentials'; | ||
85 | } | ||
86 | }, | ||
87 | 'MARKET_GET_PORTFOLIO': { | ||
88 | 'type': 'GET', | ||
89 | 'auth': true, | ||
90 | 'parameters': [ | ||
91 | {'name': 'name', 'mandatory': true, 'inquery': false}, | ||
83 | ], | 92 | ], |
84 | 'buildUrl': function(params) { | 93 | 'buildUrl': function(params) { |
85 | return '/market/' + params.name + '/balance'; | 94 | return '/market/' + params.name + '/portfolio'; |
86 | } | 95 | } |
87 | }, | 96 | }, |
88 | 'UPDATE_MARKET': { | 97 | 'UPDATE_MARKET': { |
diff --git a/cmd/web/js/balance.jsx b/cmd/web/js/balance.jsx new file mode 100644 index 0000000..d141aa8 --- /dev/null +++ b/cmd/web/js/balance.jsx | |||
@@ -0,0 +1,99 @@ | |||
1 | import React from 'react'; | ||
2 | import moment from 'moment'; | ||
3 | |||
4 | class CurrencyLogo extends React.Component { | ||
5 | render = () => { | ||
6 | return <div className="d-inline-block h-100"> | ||
7 | <img className="currency-logo align-top" | ||
8 | src={'/public/icons/black/' + this.props.currency.toLowerCase() + '.svg' } | ||
9 | title={this.props.currency} | ||
10 | alt={this.props.currency} /> | ||
11 | </div>; | ||
12 | } | ||
13 | } | ||
14 | |||
15 | var formatVariation = (variation) => { | ||
16 | var variationAbs = Math.abs(variation); | ||
17 | if (variation === 0.0) { | ||
18 | return <span>{variationAbs}%</span>; | ||
19 | } else if (variation > 0) { | ||
20 | return <span className="performance-up">+{variationAbs}%</span>; | ||
21 | } | ||
22 | return <span className="performance-down">-{variationAbs}%</span>; | ||
23 | }; | ||
24 | |||
25 | |||
26 | class CurrencyRateHeader extends React.Component { | ||
27 | render = () => { | ||
28 | return <React.Fragment> | ||
29 | <div className="row text-center"> | ||
30 | <div className="d-inline col-2">Asset</div> | ||
31 | <div className="d-inline col-2">Position</div> | ||
32 | <div className="d-inline col-2">Qty</div> | ||
33 | <div className="d-inline col-2">Value (BTC)</div> | ||
34 | <div className="d-inline col-2">Weight</div> | ||
35 | <div className="d-inline col-2">Perf %</div> | ||
36 | </div> | ||
37 | </React.Fragment>; | ||
38 | } | ||
39 | } | ||
40 | |||
41 | class CurrencyRate extends React.Component { | ||
42 | render = () => { | ||
43 | return <React.Fragment> | ||
44 | <div className="row text-center"> | ||
45 | <div className="d-inline col-2 text-left"><CurrencyLogo currency={this.props.currency} /><span>{this.props.currency}</span></div> | ||
46 | <div className="d-inline col-2">{this.props.positionType}</div> | ||
47 | <div className="d-inline col-2">{this.props.quantity}</div> | ||
48 | <div className="d-inline col-2">{this.props.BTCValue}</div> | ||
49 | <div className="d-inline col-2">{this.props.weight}%</div> | ||
50 | <div className="d-inline col-2">{formatVariation(this.props.positionPerformanceP)}</div> | ||
51 | </div> | ||
52 | </React.Fragment>; | ||
53 | } | ||
54 | } | ||
55 | |||
56 | class Assets extends React.Component { | ||
57 | render = () => { | ||
58 | var currencies = Object.keys(this.props.balances).map(function(currency) { | ||
59 | var balance = this.props.balances[currency]; | ||
60 | balance.currency = currency; | ||
61 | return <div className="row" key={currency}> | ||
62 | <div className="col-12"><CurrencyRate {...balance} /></div> | ||
63 | </div>; | ||
64 | }.bind(this)); | ||
65 | |||
66 | return <div className="assets"> | ||
67 | <CurrencyRateHeader /> | ||
68 | {currencies} | ||
69 | </div>; | ||
70 | } | ||
71 | } | ||
72 | |||
73 | class PFBalance extends React.Component { | ||
74 | render = () => { | ||
75 | var date = moment(this.props.periodStart).format('MMM Do, h:mma'); | ||
76 | return <React.Fragment> | ||
77 | <div className="h-100 align-self-center balance"> | ||
78 | <div className="row"> | ||
79 | <div className="col-8"> | ||
80 | Current balance | ||
81 | </div> | ||
82 | <div className="col-4 text-center h-100 btcValue"> | ||
83 | <CurrencyLogo currency="BTC" /> <span className="h-100 align-middle"><strong>{this.props.balance}</strong></span> | ||
84 | </div> | ||
85 | </div> | ||
86 | <div className="row"> | ||
87 | <div className="col-8"> | ||
88 | <em>since {date}</em> | ||
89 | </div> | ||
90 | <div className="col-4 variation text-center"> | ||
91 | <strong>{formatVariation(this.props.variationP)}</strong> | ||
92 | </div> | ||
93 | </div> | ||
94 | </div> | ||
95 | </React.Fragment>; | ||
96 | } | ||
97 | } | ||
98 | |||
99 | export {PFBalance, Assets}; | ||
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 | } |