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