1 import Api from './api.js';
2 import React from 'react';
4 class PoloniexController extends React.Component {
7 this.state = {'apiKey': '', 'apiSecret': '', 'apiRequested': false, 'flag': 'loading', 'valueCurrency': null, 'balanceValue': null, 'balance': null};
10 handleCredentialsChange = (key, secret) => {
11 this.setState({'apiKey': key, 'apiSecret': secret});
14 handleCredentialsSubmit = () => {
15 if (!this.state.apiKey || !this.state.apiSecret) {
18 Api.Call('UPDATE_MARKET', {'key': this.state.apiKey, 'secret': this.state.apiSecret, 'name': 'poloniex'}, function(err, status, data) {
20 console.error(err, data);
24 this.setState({'flag': 'loading', 'valueCurrency': null, 'balanceValue': null, 'balance': null});
30 Api.Call('MARKET_BALANCE', {'name': 'poloniex', 'currency': 'BTC'}, function(err, status, data) {
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});
41 this.setState({'flag': 'ok', 'valueCurrency': data.valueCurrency, 'balanceValue': data.value, 'balance': data.balance});
45 componentDidMount = () => {
46 Api.Call('MARKET', {'name': 'poloniex'}, function(err, status, data) {
47 this.setState({'apiRequested': true});
49 console.error(err, data);
53 var flag = this.state.flag;
54 if (!data.key || !data.secret) {
55 flag = 'emptyCredentials';
60 this.setState({'apiKey': data.key, 'apiSecret': data.secret, 'flag': flag});
65 var displayText = null;
66 switch (this.state.flag) {
68 displayText = 'Loading data from poloniex...';
70 case 'invalidCredentials':
71 displayText = 'Invalid poloniex credentials';
74 displayText = 'Your API key is IP restricted. Please whitelist us.';
76 case 'emptyCredentials':
77 displayText = 'Please provide poloniex credentials';
82 if (this.state.apiRequested === false) {
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}/>
101 class CurrencyLogo extends React.Component {
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} />;
110 class PoloniexBalance extends React.Component {
113 this.state = {'hideMsg': true, 'msg': '', 'msgOk': false};
117 var dashboard = null;
119 if (this.props.balanceValue !== null) {
121 var balance = Object.keys(this.props.balance).map(function(currency) {
122 return <div key={currency}>
123 <CurrencyLogo currency={currency} /> {this.props.balance[currency]}
128 <div className="row">
129 <div className="col-4 align-self-center h-100 balances">
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} />
140 <div className="row">
141 <div className="col-12 text-center">
142 <span>{this.props.displayText}</span>
148 <div className="row">
149 <div className="box offset-2 col-8 portfolio">
150 <div className="row">
151 <div className="col-4">Portfolio</div>
161 class PoloniexCredentialsForm extends React.Component {
164 this.state = {'hideMsg': true, 'msg': '', 'editMode': false, 'msgOk': false};
167 handleSubmit = (e) => {
168 this.props.onCredentialsSubmit();
169 this.setState({'editMode': false});
173 handleApiKeyChange = (event) => {
174 this.props.onCredentialsChange(event.target.value, this.props.apiSecret);
177 handleApiSecretChange = (event) => {
178 this.props.onCredentialsChange(this.props.apiKey, event.target.value);
181 onEditClick = () => {
182 this.setState({'editMode': true});
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';
192 <div className="row api-credentials-form">
193 <div className="offset-2 col-8 box">
194 <span className="text-center">Poloniex credentials</span>
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}/>
200 <label className="w-100">Secret:
201 <input className="form-control" type="text" placeholder="secret" value={secretDisplayed} onChange={this.handleApiSecretChange} disabled={!this.state.editMode}/>
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>
212 export default PoloniexController;