1 import Api from './api.js';
2 import classNames from 'classnames';
3 import React from 'react';
5 class PoloniexController extends React.Component {
8 this.state = {'apiKey': '', 'apiSecret': '', 'apiRequested': false, 'flag': 'loading', 'valueCurrency': null, 'balanceValue': null, 'balance': null};
11 handleCredentialsChange = (key, secret) => {
12 this.setState({'apiKey': key, 'apiSecret': secret});
15 handleCredentialsSubmit = () => {
16 if (!this.state.apiKey || !this.state.apiSecret) {
19 Api.Call('UPDATE_MARKET', {'key': this.state.apiKey, 'secret': this.state.apiSecret, 'name': 'poloniex'}, function(err, status, data) {
21 console.error(err, data);
25 this.setState({'flag': 'loading', 'valueCurrency': null, 'balanceValue': null, 'balance': null});
31 Api.Call('MARKET_BALANCE', {'name': 'poloniex', 'currency': 'BTC'}, function(err, status, data) {
33 console.error(err, data);
34 if (err.code === 'invalid_market_credentials') {
35 this.setState({'flag': 'invalidCredentials', 'valueCurrency': null, 'balanceValue': null, 'balance': null});
40 this.setState({'flag': 'ok', 'valueCurrency': data.valueCurrency, 'balanceValue': data.value, 'balance': data.balance});
44 componentDidMount = () => {
45 Api.Call('MARKET', {'name': 'poloniex'}, function(err, status, data) {
46 this.setState({'apiRequested': true});
48 console.error(err, data);
52 var flag = this.state.flag;
53 if (!data.key || !data.secret) {
54 flag = 'emptyCredentials';
59 this.setState({'apiKey': data.key, 'apiSecret': data.secret, 'flag': flag});
64 var displayText = null;
65 switch (this.state.flag) {
67 displayText = 'Loading data from poloniex...';
69 case 'invalidCredentials':
70 displayText = 'Invalid poloniex credentials';
72 case 'emptyCredentials':
73 displayText = 'Please provide poloniex credentials';
78 if (this.state.apiRequested === false) {
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}/>
97 class PoloniexBalance extends React.Component {
100 this.state = {'hideMsg': true, 'msg': '', 'msgOk': false};
104 var dashboard = null;
106 if (this.props.balanceValue !== null) {
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>;
113 <div className="row">
114 <div className="col-4 align-self-center h-100">
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>
127 <div className="row">
128 <div className="col-12 text-center">
129 <span>{this.props.displayText}</span>
135 <div className="row">
136 <div className="box offset-2 col-8">
137 <div className="row">
138 <div className="col-4">Portfolio</div>
148 class PoloniexCredentialsForm extends React.Component {
151 this.state = {'hideMsg': true, 'msg': '', 'editMode': false, 'msgOk': false};
154 handleSubmit = (e) => {
155 this.props.onCredentialsSubmit();
156 this.setState({'editMode': false});
160 handleApiKeyChange = (event) => {
161 this.props.onCredentialsChange(event.target.value, this.props.apiSecret);
164 handleApiSecretChange = (event) => {
165 this.props.onCredentialsChange(this.props.apiKey, event.target.value);
168 onEditClick = () => {
169 this.setState({'editMode': true});
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';
179 <div className="row api-credentials-form">
180 <div className="offset-3 col-6 box">
181 <span className="text-center">Poloniex credentials</span>
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}/>
187 <label className="w-100">Secret:
188 <input className="form-control" type="text" placeholder="secret" value={secretDisplayed} onChange={this.handleApiSecretChange} disabled={!this.state.editMode}/>
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>
199 export default PoloniexController;