]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/commitdiff
Load credentials only when user requests it.
authorjloup <jloup@jloup.work>
Sun, 13 May 2018 13:10:21 +0000 (14:10 +0100)
committerjloup <jloup@jloup.work>
Sun, 13 May 2018 13:10:21 +0000 (14:10 +0100)
api/const.go
api/const_string.go
api/market_config.go
cmd/web/js/account.jsx

index 1f15c6ec35176fdd71e361879500b7bcc1fec9ec..2ad4d6135af2920bf9b899970910e38fe17e6f33 100644 (file)
@@ -19,6 +19,7 @@ const (
        InvalidCredentials
        InvalidEmail
        InvalidMarketCredentials
+       MarketCredentialsNotConfigured
        IPRestrictedApiKey
        InvalidOtp
        InvalidPassword
@@ -36,7 +37,7 @@ func StatusToHttpCode(status Status, code ErrorCode) int {
        }
 
        switch code {
-       case BadRequest, InvalidPassword, InvalidEmail, InvalidMarketCredentials, IPRestrictedApiKey:
+       case BadRequest, InvalidPassword, InvalidEmail, InvalidMarketCredentials, IPRestrictedApiKey, MarketCredentialsNotConfigured:
                return http.StatusBadRequest
 
        case InvalidCredentials, InvalidOtp:
index 58ed230aeb47795c1896c199fac582eeb922fe1f..5af7574c5bae8968d07d2c2810d994413d54b9ec 100644 (file)
@@ -1,8 +1,8 @@
-// Code generated by "stringer -type=Status,ErrorCode -output const_string.go"; DO NOT EDIT
+// Code generated by "stringer -type=Status,ErrorCode -output const_string.go"; DO NOT EDIT.
 
 package api
 
-import "fmt"
+import "strconv"
 
 const _Status_name = "OKNOK"
 
@@ -10,19 +10,19 @@ var _Status_index = [...]uint8{0, 2, 5}
 
 func (i Status) String() string {
        if i >= Status(len(_Status_index)-1) {
-               return fmt.Sprintf("Status(%d)", i)
+               return "Status(" + strconv.FormatInt(int64(i), 10) + ")"
        }
        return _Status_name[_Status_index[i]:_Status_index[i+1]]
 }
 
-const _ErrorCode_name = "BadRequestEmailExistsExternalServiceTimeoutInternalErrorInvalidCredentialsInvalidEmailInvalidMarketCredentialsIPRestrictedApiKeyInvalidOtpInvalidPasswordNeedOtpValidationNotAuthorizedNotFoundOtpAlreadySetupOtpNotSetupUserNotConfirmed"
+const _ErrorCode_name = "BadRequestEmailExistsExternalServiceTimeoutInternalErrorInvalidCredentialsInvalidEmailInvalidMarketCredentialsMarketCredentialsNotConfiguredIPRestrictedApiKeyInvalidOtpInvalidPasswordNeedOtpValidationNotAuthorizedNotFoundOtpAlreadySetupOtpNotSetupUserNotConfirmed"
 
-var _ErrorCode_index = [...]uint8{0, 10, 21, 43, 56, 74, 86, 110, 128, 138, 153, 170, 183, 191, 206, 217, 233}
+var _ErrorCode_index = [...]uint16{0, 10, 21, 43, 56, 74, 86, 110, 140, 158, 168, 183, 200, 213, 221, 236, 247, 263}
 
 func (i ErrorCode) String() string {
        i -= 3
        if i >= ErrorCode(len(_ErrorCode_index)-1) {
-               return fmt.Sprintf("ErrorCode(%d)", i+3)
+               return "ErrorCode(" + strconv.FormatInt(int64(i+3), 10) + ")"
        }
        return _ErrorCode_name[_ErrorCode_index[i]:_ErrorCode_index[i+1]]
 }
index e7b2341760f0d4a64df680767de8b0ea2afeb5ff..09eb8a9c00ea2aa22a4521bea2f382afd4c9442a 100644 (file)
@@ -76,7 +76,7 @@ func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) {
        }
 
        if config == nil || config.Config["key"] == "" || config.Config["secret"] == "" {
-               return nil, &Error{InvalidMarketCredentials, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)}
+               return nil, &Error{MarketCredentialsNotConfigured, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)}
        }
 
        resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error {
index 03ca117b756db7a222ddf21b5d1e76d7ecc0ff13..c80be33ecfa605878648d1c9b15983c643121a80 100644 (file)
@@ -4,17 +4,20 @@ import React from 'react';
 class PoloniexConfiguration extends React.Component {
   constructor(props) {
     super(props);
-    this.state = {'apiKey': '', 'apiSecret': '', 'apiRequested': false, 'status': 'loading'};
+    this.state = {'apiKey': '', 'apiSecret': '', 'apiRequested': false, 'status': 'loading', 'editMode': false};
   }
 
   checkCredentials = () => {
     Api.Call('MARKET_TEST_CREDENTIALS', {'name': 'poloniex'}, function(err, status, data) {
+      this.setState({'apiRequested': true});
       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;
       }
@@ -28,7 +31,7 @@ class PoloniexConfiguration extends React.Component {
   }
 
   handleCredentialsSubmit = () => {
-    this.setState({'status': 'loading'});
+    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);
@@ -40,14 +43,17 @@ class PoloniexConfiguration extends React.Component {
         return;
       }
 
-      this.setState({'status': 'loading'});
       this.checkCredentials();
     }.bind(this));
   }
 
   componentDidMount = () => {
-    Api.Call('MARKET', {'name': 'poloniex'}, function(err, status, data) {
-      this.setState({'apiRequested': true});
+    this.checkCredentials();
+  }
+
+  onEditClick = () => {
+   Api.Call('MARKET', {'name': 'poloniex'}, function(err, status, data) {
+      this.setState({'apiRequested': true, 'editMode': true});
       if (err) {
         console.error(err, data);
         return;
@@ -56,8 +62,6 @@ class PoloniexConfiguration extends React.Component {
       var newStatus = this.state.status;
       if (!data.key || !data.secret) {
         newStatus = 'emptyCredentials';
-      } else {
-        this.checkCredentials();
       }
 
       this.setState({'apiKey': data.key, 'apiSecret': data.secret, 'status': newStatus});
@@ -97,21 +101,17 @@ class PoloniexConfiguration extends React.Component {
                                  apiSecret={this.state.apiSecret}
                                  apiKey={this.state.apiKey}
                                  status={this.state.status}
-                                 statusMessage={displayText}/>
+                                 statusMessage={displayText}
+                                 editMode={this.state.editMode}
+                                 onEditClick={this.onEditClick}/>
       </div>
     );
   }
 }
 
 class PoloniexCredentialsForm extends React.Component {
-  constructor(props) {
-    super(props);
-    this.state = {'editMode': false};
-  }
-
   handleSubmit = (e) => {
     this.props.onCredentialsSubmit();
-    this.setState({'editMode': false});
     e.preventDefault();
   }
 
@@ -123,15 +123,11 @@ class PoloniexCredentialsForm extends React.Component {
     this.props.onCredentialsChange(this.props.apiKey, event.target.value);
   }
 
-  onEditClick = () => {
-    this.setState({'editMode': true});
-  }
-
   render = () => {
-    var submitType      = this.state.editMode === true ? 'submit' : 'hidden';
-    var buttonDisplay   = this.state.editMode === true ? 'none' : 'inline';
-    var secretDisplayed = this.state.editMode === true ? this.props.apiSecret : 'XXXXXXX';
-    var keyDisplayed    = this.state.editMode === true ? this.props.apiKey : 'XXXXXXX';
+    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) {
@@ -157,13 +153,13 @@ class PoloniexCredentialsForm extends React.Component {
               <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.state.editMode}/>
+                    <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.state.editMode}/>
+                    <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.onEditClick} type="button">Show/Edit</button>
+                  <button className="form-control submit" style={{display: buttonDisplay}} onSubmit={null} onClick={this.props.onEditClick} type="button">Show/Edit</button>
                 </form>
               </div>
             </div>