]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blob - cmd/web/js/poloniex.jsx
db6b1c43da71f123ec830dc8d6a8f89bffbe8146
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / cmd / web / js / poloniex.jsx
1 import Api from './api.js';
2 import React from 'react';
3
4 class PoloniexController extends React.Component {
5 constructor(props) {
6 super(props);
7 this.state = {'apiKey': '', 'apiSecret': '', 'apiRequested': false, 'flag': 'loading', 'valueCurrency': null, 'balanceValue': null, 'balance': null};
8 }
9
10 handleCredentialsChange = (key, secret) => {
11 this.setState({'apiKey': key, 'apiSecret': secret});
12 }
13
14 handleCredentialsSubmit = () => {
15 if (!this.state.apiKey || !this.state.apiSecret) {
16 return;
17 }
18 Api.Call('UPDATE_MARKET', {'key': this.state.apiKey, 'secret': this.state.apiSecret, 'name': 'poloniex'}, function(err, status, data) {
19 if (err) {
20 console.error(err, data);
21 return;
22 }
23
24 this.setState({'flag': 'loading', 'valueCurrency': null, 'balanceValue': null, 'balance': null});
25 this.loadBalance();
26 }.bind(this));
27 }
28
29 loadBalance = () => {
30 Api.Call('MARKET_BALANCE', {'name': 'poloniex', 'currency': 'BTC'}, function(err, status, data) {
31 if (err) {
32 console.error(err, data);
33 if (err.code === 'invalid_market_credentials') {
34 this.setState({'flag': 'invalidCredentials', 'valueCurrency': null, 'balanceValue': null, 'balance': null});
35 } else if (err.code === 'ip_restricted_api_key') {
36 this.setState({'flag': 'ipRestricted', 'valueCurrency': null, 'balanceValue': null, 'balance': null});
37 }
38 return;
39 }
40
41 this.setState({'flag': 'ok', 'valueCurrency': data.valueCurrency, 'balanceValue': data.value, 'balance': data.balance});
42 }.bind(this));
43 }
44
45 componentDidMount = () => {
46 Api.Call('MARKET', {'name': 'poloniex'}, function(err, status, data) {
47 this.setState({'apiRequested': true});
48 if (err) {
49 console.error(err, data);
50 return;
51 }
52
53 var flag = this.state.flag;
54 if (!data.key || !data.secret) {
55 flag = 'emptyCredentials';
56 } else {
57 this.loadBalance();
58 }
59
60 this.setState({'apiKey': data.key, 'apiSecret': data.secret, 'flag': flag});
61 }.bind(this));
62 }
63
64 render = () => {
65 var displayText = null;
66 switch (this.state.flag) {
67 case 'loading':
68 displayText = 'Loading data from poloniex...';
69 break;
70 case 'invalidCredentials':
71 displayText = 'Invalid poloniex credentials';
72 break;
73 case 'ipRestricted':
74 displayText = 'Your API key is IP restricted. Please whitelist us.';
75 break;
76 case 'emptyCredentials':
77 displayText = 'Please provide poloniex credentials';
78 break;
79 default:
80 displayText = null;
81 }
82 if (this.state.apiRequested === false) {
83 return <div></div>;
84 }
85 return (
86 <div>
87 <PoloniexBalance balanceCurrency={this.state.valueCurrency}
88 balanceValue={this.state.balanceValue}
89 balance={this.state.balance}
90 displayText={displayText}/>
91 <PoloniexCredentialsForm onLoadCredentials={this.onLoadCredentials}
92 onCredentialsSubmit={this.handleCredentialsSubmit}
93 onCredentialsChange={this.handleCredentialsChange}
94 apiSecret={this.state.apiSecret}
95 apiKey={this.state.apiKey}/>
96 </div>
97 );
98 }
99 }
100
101 class CurrencyLogo extends React.Component {
102 render = () => {
103 return <img className="currency-logo"
104 src={'/public/icons/black/' + this.props.currency.toLowerCase() + '.svg' }
105 title={this.props.currency}
106 alt={this.props.currency} />;
107 }
108 }
109
110 class PoloniexBalance extends React.Component {
111 constructor(props) {
112 super(props);
113 this.state = {'hideMsg': true, 'msg': '', 'msgOk': false};
114 }
115
116 computeCurrencyRatio = (currency) => {
117 return (parseFloat(this.props.balance[currency].btcValue) / parseFloat(this.props.balanceValue) * 100.0).toFixed(1);
118 }
119
120 render = () => {
121 var dashboard = null;
122
123 if (this.props.balanceValue !== null) {
124
125 var balance = Object.keys(this.props.balance).map(function(currency) {
126 return <div key={currency}>
127 <div>
128 <CurrencyLogo currency={currency} /> {this.props.balance[currency].amount} {currency} ({this.computeCurrencyRatio(currency)}%)
129 </div>
130 </div>;
131 }.bind(this));
132
133 dashboard =
134 <div className="row">
135 <div className="col-6 align-self-center h-100 balances">
136 {balance}
137 </div>
138 <div className="offset-1 col-5 h-100 align-self-center">
139 <div className="text-center">
140 Balance ({this.props.balanceCurrency}): <span>{this.props.balanceValue}</span><CurrencyLogo currency={this.props.balanceCurrency} />
141 </div>
142 </div>
143 </div>;
144 } else {
145 dashboard =
146 <div className="row">
147 <div className="col-12 text-center">
148 <span>{this.props.displayText}</span>
149 </div>
150 </div>;
151 }
152
153 return (
154 <div className="row">
155 <div className="box offset-2 col-8 portfolio">
156 <div className="row">
157 <div className="col-4">Portfolio</div>
158 </div>
159 <hr/>
160 {dashboard}
161 </div>
162 </div>
163 );
164 }
165 }
166
167 class PoloniexCredentialsForm extends React.Component {
168 constructor(props) {
169 super(props);
170 this.state = {'hideMsg': true, 'msg': '', 'editMode': false, 'msgOk': false};
171 }
172
173 handleSubmit = (e) => {
174 this.props.onCredentialsSubmit();
175 this.setState({'editMode': false});
176 e.preventDefault();
177 }
178
179 handleApiKeyChange = (event) => {
180 this.props.onCredentialsChange(event.target.value, this.props.apiSecret);
181 }
182
183 handleApiSecretChange = (event) => {
184 this.props.onCredentialsChange(this.props.apiKey, event.target.value);
185 }
186
187 onEditClick = () => {
188 this.setState({'editMode': true});
189 }
190
191 render = () => {
192 var submitType = this.state.editMode === true ? 'submit' : 'hidden';
193 var buttonDisplay = this.state.editMode === true ? 'none' : 'inline';
194 var secretDisplayed = this.state.editMode === true ? this.props.apiSecret : 'XXXXXXX';
195 var keyDisplayed = this.state.editMode === true ? this.props.apiKey : 'XXXXXXX';
196
197 return (
198 <div className="row api-credentials-form">
199 <div className="offset-2 col-8 box">
200 <span className="text-center">Poloniex credentials</span>
201 <hr/>
202 <form role="form" onSubmit={this.handleSubmit}>
203 <label className="w-100">Key:
204 <input className="form-control" type="text" placeholder="key" value={keyDisplayed} onChange={this.handleApiKeyChange} disabled={!this.state.editMode}/>
205 </label>
206 <label className="w-100">Secret:
207 <input className="form-control" type="text" placeholder="secret" value={secretDisplayed} onChange={this.handleApiSecretChange} disabled={!this.state.editMode}/>
208 </label>
209 <input className="form-control submit" type={submitType} value="Save" />
210 <button className="form-control submit" style={{display: buttonDisplay}} onSubmit={null} onClick={this.onEditClick} type="button">Show/Edit</button>
211 </form>
212 </div>
213 </div>
214 );
215 }
216 }
217
218 export default PoloniexController;