aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/js/account.jsx
blob: 3dc8afd084b48551ca7e2a673fed7781db2178d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import Api from './api.js';
import React from 'react';
import classnames from 'classnames';

class Panel extends React.Component {
  render = () => {
    if (this.props.component === null) {
      return <div></div>;
    }

    var className = classnames('row', this.props.topClassName);

    return (
      <div className={className}>
        <div className="box col-12">
          <div className="row">
            <div className="col-4">{this.props.title}</div>
          </div>
          <hr/>
          {this.props.component}
        </div>
      </div>);
  }
}

class AccountInformation extends React.Component {
  constructor(props) {
    super(props);
    this.state = {'email': null};
  }

  loadAccount = () => {
    Api.Call('USER_ACCOUNT', {}, function(err, status, data) {
      if (err) {
        console.error(err, data);
        return;
      }

      this.setState({'email': data.email});
    }.bind(this));
  }

  componentDidMount = () => {
    this.loadAccount();
  }

  render = () => {
    var component = <p>Loading...</p>;
    if (this.state.email !== null) {
      component = <p>Email: {this.state.email}</p>;
    }

    return component;
  }

}

class PoloniexConfiguration extends React.Component {
  constructor(props) {
    super(props);
    this.state = {'apiKey': '', 'apiSecret': '', 'status': 'loading', 'editMode': false};
  }

  checkCredentials = () => {
    Api.Call('MARKET_TEST_CREDENTIALS', {'name': 'poloniex'}, function(err, status, data) {
      if (err) {
        console.error(err, data);
        if (err.code === 'invalid_market_credentials') {
          this.setState({'status': 'invalidCredentials'});
        } else if (err.code === 'ip_restricted_api_key') {
          this.setState({'status': 'ipRestricted'});
        } else if (err.code === 'market_credentials_not_configured') {
          this.setState({'status': 'emptyCredentials'});
        }
        return;
      }

      this.setState({'status': 'ok'});
    }.bind(this));
  }

  handleCredentialsChange = (key, secret) => {
    this.setState({'apiKey': key, 'apiSecret': secret});
  }

  handleCredentialsSubmit = () => {
    this.setState({'status': 'loading', 'editMode': false});
    Api.Call('UPDATE_MARKET', {'key': this.state.apiKey, 'secret': this.state.apiSecret, 'name': 'poloniex'}, function(err, status, data) {
      if (err) {
        console.error(err, data);
        if (err.code === 'invalid_market_credentials') {
          this.setState({'status': 'invalidCredentials'});
        } else if (err.code === 'ip_restricted_api_key') {
          this.setState({'status': 'ipRestricted'});
        }
        return;
      }

      this.checkCredentials();
    }.bind(this));
  }

  componentDidMount = () => {
    this.checkCredentials();
  }

  onEditClick = () => {
   Api.Call('MARKET', {'name': 'poloniex'}, function(err, status, data) {
      this.setState({'editMode': true});
      if (err) {
        console.error(err, data);
        return;
      }

      var newStatus = this.state.status;
      if (!data.key || !data.secret) {
        newStatus = 'emptyCredentials';
      }

      this.setState({'apiKey': data.key, 'apiSecret': data.secret, 'status': newStatus});
    }.bind(this));
  }

  render = () => {
    var displayText = null;
    switch (this.state.status) {
      case 'loading':
        displayText = 'Checking Poloniex credentials...';
        break;
      case 'invalidCredentials':
        displayText = 'Invalid poloniex credentials';
        break;
      case 'ipRestricted':
        displayText = 'Your API key is IP restricted.';
        break;
      case 'emptyCredentials':
        displayText = 'Please provide poloniex credentials';
        break;
      case 'ok':
        displayText = 'You are all set !';
        break;
      default:
        console.error('unknown status', this.state.status);
        displayText = null;
    }

    return (
        <PoloniexCredentialsForm onLoadCredentials={this.onLoadCredentials}
                                 onCredentialsSubmit={this.handleCredentialsSubmit}
                                 onCredentialsChange={this.handleCredentialsChange}
                                 apiSecret={this.state.apiSecret}
                                 apiKey={this.state.apiKey}
                                 status={this.state.status}
                                 statusMessage={displayText}
                                 editMode={this.state.editMode}
                                 onEditClick={this.onEditClick}/>
    );
  }
}

class PoloniexCredentialsForm extends React.Component {
  handleSubmit = (e) => {
    this.props.onCredentialsSubmit();
    e.preventDefault();
  }

  handleApiKeyChange = (event) => {
    this.props.onCredentialsChange(event.target.value, this.props.apiSecret);
  }

  handleApiSecretChange = (event) => {
    this.props.onCredentialsChange(this.props.apiKey, event.target.value);
  }

  render = () => {
    var submitType      = this.props.editMode === true ? 'submit' : 'hidden';
    var buttonDisplay   = this.props.editMode === true ? 'none' : 'inline';
    var secretDisplayed = this.props.editMode === true ? this.props.apiSecret : 'XXXXXXX';
    var keyDisplayed    = this.props.editMode === true ? this.props.apiKey : 'XXXXXXX';

    var iconName = 'icon-cancel-circled';
    switch (this.props.status) {
      case 'loading':
        iconName = 'icon-hourglass-2';
        break;
      case 'ok':
        iconName = 'icon-ok-circled';
        break;
    }

    return (
        <React.Fragment>
        <div className="row config-status">
          <div className="col-12">
            <span><i className={iconName}></i>{this.props.statusMessage}</span>
          </div>
        </div>
        <div className="row">
          <div className="col-12">
            <form role="form" onSubmit={this.handleSubmit}>
              <label className="w-100">Key:
                <input className="form-control" type="text" placeholder="key" value={keyDisplayed} onChange={this.handleApiKeyChange} disabled={!this.props.editMode}/>
              </label>
              <label className="w-100">Secret:
                <input className="form-control" type="text" placeholder="secret" value={secretDisplayed} onChange={this.handleApiSecretChange} disabled={!this.props.editMode}/>
              </label>
              <input className="form-control submit" type={submitType} value="Save" />
              <button className="form-control submit" style={{display: buttonDisplay}} onSubmit={null} onClick={this.props.onEditClick} type="button">Show/Edit</button>
            </form>
          </div>
        </div>
        </React.Fragment>
       );
  }
}

class UserAccount extends React.Component {
  render = () => {
    return (
      <React.Fragment>
        <Panel component={<AccountInformation/>} title="Account" />
        <Panel component={<PoloniexConfiguration/>} title="Poloniex credentials" topClassName="api-credentials-form" />
      </React.Fragment>
    );
  }
}

export default UserAccount;