]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blob - cmd/web/js/poloniex.jsx
Handle poloniex ip restriction.
[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 render = () => {
117 var dashboard = null;
118
119 if (this.props.balanceValue !== null) {
120
121 var balance = Object.keys(this.props.balance).map(function(currency) {
122 return <div key={currency}>
123 <CurrencyLogo currency={currency} /> {this.props.balance[currency]}
124 </div>;
125 }.bind(this));
126
127 dashboard =
128 <div className="row">
129 <div className="col-4 align-self-center h-100 balances">
130 {balance}
131 </div>
132 <div className="offset-1 col-7 h-100 align-self-center">
133 <div className="text-center">
134 Balance ({this.props.balanceCurrency}): <span>{this.props.balanceValue}</span><CurrencyLogo currency={this.props.balanceCurrency} />
135 </div>
136 </div>
137 </div>;
138 } else {
139 dashboard =
140 <div className="row">
141 <div className="col-12 text-center">
142 <span>{this.props.displayText}</span>
143 </div>
144 </div>;
145 }
146
147 return (
148 <div className="row">
149 <div className="box offset-2 col-8 portfolio">
150 <div className="row">
151 <div className="col-4">Portfolio</div>
152 </div>
153 <hr/>
154 {dashboard}
155 </div>
156 </div>
157 );
158 }
159 }
160
161 class PoloniexCredentialsForm extends React.Component {
162 constructor(props) {
163 super(props);
164 this.state = {'hideMsg': true, 'msg': '', 'editMode': false, 'msgOk': false};
165 }
166
167 handleSubmit = (e) => {
168 this.props.onCredentialsSubmit();
169 this.setState({'editMode': false});
170 e.preventDefault();
171 }
172
173 handleApiKeyChange = (event) => {
174 this.props.onCredentialsChange(event.target.value, this.props.apiSecret);
175 }
176
177 handleApiSecretChange = (event) => {
178 this.props.onCredentialsChange(this.props.apiKey, event.target.value);
179 }
180
181 onEditClick = () => {
182 this.setState({'editMode': true});
183 }
184
185 render = () => {
186 var submitType = this.state.editMode === true ? 'submit' : 'hidden';
187 var buttonDisplay = this.state.editMode === true ? 'none' : 'inline';
188 var secretDisplayed = this.state.editMode === true ? this.props.apiSecret : 'XXXXXXX';
189 var keyDisplayed = this.state.editMode === true ? this.props.apiKey : 'XXXXXXX';
190
191 return (
192 <div className="row api-credentials-form">
193 <div className="offset-2 col-8 box">
194 <span className="text-center">Poloniex credentials</span>
195 <hr/>
196 <form role="form" onSubmit={this.handleSubmit}>
197 <label className="w-100">Key:
198 <input className="form-control" type="text" placeholder="key" value={keyDisplayed} onChange={this.handleApiKeyChange} disabled={!this.state.editMode}/>
199 </label>
200 <label className="w-100">Secret:
201 <input className="form-control" type="text" placeholder="secret" value={secretDisplayed} onChange={this.handleApiSecretChange} disabled={!this.state.editMode}/>
202 </label>
203 <input className="form-control submit" type={submitType} value="Save" />
204 <button className="form-control submit" style={{display: buttonDisplay}} onSubmit={null} onClick={this.onEditClick} type="button">Show/Edit</button>
205 </form>
206 </div>
207 </div>
208 );
209 }
210 }
211
212 export default PoloniexController;