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': '', '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) {
47 console.error(err, data);
51 var flag = this.state.flag;
52 if (!data.key || !data.secret) {
53 flag = 'emptyCredentials';
58 this.setState({'apiKey': data.key, 'apiSecret': data.secret, 'flag': flag});
63 var displayText = null;
64 switch (this.state.flag) {
66 displayText = 'Loading data from poloniex...';
68 case 'invalidCredentials':
69 displayText = 'Invalid poloniex credentials';
71 case 'emptyCredentials':
72 displayText = 'Please provide poloniex credentials';
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}/>
93 class PoloniexBalance extends React.Component {
96 this.state = {'hideMsg': true, 'msg': '', 'msgOk': false};
100 var dashboard = null;
102 if (this.props.balanceValue !== null) {
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>;
109 <div className="row">
110 <div className="col-4 align-self-center h-100">
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>
123 <div className="row">
124 <div className="col-12 text-center">
125 <span>{this.props.displayText}</span>
131 <div className="row">
132 <div className="box offset-2 col-8">
133 <div className="row">
134 <div className="col-4">Portfolio</div>
144 class PoloniexCredentialsForm extends React.Component {
147 this.state = {'hideMsg': true, 'msg': '', 'editMode': false, 'msgOk': false};
150 handleSubmit = (e) => {
151 this.props.onCredentialsSubmit();
152 this.setState({'editMode': false});
156 handleApiKeyChange = (event) => {
157 this.props.onCredentialsChange(event.target.value, this.props.apiSecret);
160 handleApiSecretChange = (event) => {
161 this.props.onCredentialsChange(this.props.apiKey, event.target.value);
164 onEditClick = () => {
165 this.setState({'editMode': true});
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';
175 <div className="row api-credentials-form">
176 <div className="offset-3 col-6 box">
177 <span className="text-center">Poloniex credentials</span>
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}/>
183 <label className="w-100">Secret:
184 <input className="form-control" type="text" placeholder="secret" value={secretDisplayed} onChange={this.handleApiSecretChange} disabled={!this.state.editMode}/>
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>
195 export default PoloniexController;