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