aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/js/poloniex.jsx
blob: 6019ef8cb59402ac143e1fdc4cf9d1668360b7fa (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
import Api from './api.js';
import React from 'react';
import {PFBalance, Assets} from './balance.js';

class PoloniexController extends React.Component {
  constructor(props) {
    super(props);
    this.state = {'flag': 'loading', 'periodStart': null, 'variationP': null, 'balance': null, 'balances': null};
  }

  testCredentials = () => {
    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({'flag': 'invalidCredentials', 'variationP': null, 'balance': null, 'balances': null, 'periodStart': null});
        } else if (err.code === 'ip_restricted_api_key') {
          this.setState({'flag': 'ipRestricted', 'variationP': null, 'balance': null, 'balances': null, 'periodStart': null});
        }
        return;
      }

      this.loadPortfolio();
    }.bind(this));
  }

  loadPortfolio = () => {
    Api.Call('MARKET_GET_PORTFOLIO', {'name': 'poloniex'}, function(err, status, data) {
      if (err) {
        console.error(err, data);
        if (err.code === 'not_found') {
          this.setState({'flag': 'noReport', 'variationP': null, 'balance': null, 'balances': null, 'periodStart': null});
        }
        return;
      }

      this.setState({'flag': 'ok', 'variationP': data.performance.variationP, 'balance': data.value, 'balances': data.balances, 'periodStart': data.periodStart});
    }.bind(this));
  }

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

  render = () => {
    var displayText = null;
    switch (this.state.flag) {
      case 'loading':
        displayText = 'Loading data from poloniex...';
        break;
      case 'noReport':
        displayText = 'Your account is setup ! Reporting will start next Monday !';
        break;
      case 'invalidCredentials':
      case 'ipRestricted':
      case 'emptyCredentials':
        displayText = <div>Please provide poloniex credentials in <a href="/account"><u>Account</u></a> page.</div>;
        break;
      default:
        displayText = null;
    }
    return (
      <div>
        <PoloniexBalance  balanceCurrency={this.state.valueCurrency}
                          variationP={this.state.variationP}
                          periodStart={this.state.periodStart}
                          balance={this.state.balance}
                          balances={this.state.balances}
                          displayText={displayText}/>
      </div>
    );
  }
}

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

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

class PoloniexBalance extends React.Component {

  render = () => {
    var balancePanel = null;
    var assetsPanel  = null;
    var messagePanel = null;

    if (this.props.variationP !== null) {
      balancePanel =
        <div className="row">
          <div className="col-12 offset-md-1 col-md-10 align-self-center h-100 balances">
            <PFBalance variationP={this.props.variationP} balance={this.props.balance} periodStart={this.props.periodStart}/>
          </div>
        </div>;

      assetsPanel =
        <Assets balances={this.props.balances} />;

    } else {
      messagePanel =
        <div className="row">
          <div className="col-12 text-center">
           <span>{this.props.displayText}</span>
          </div>
        </div>;
    }

    return (
      <React.Fragment>
        <Panel title="Balance" component={balancePanel}/>
        <Panel title="Assets"  component={assetsPanel}/>
        <Panel title="Balance" component={messagePanel}/>
      </React.Fragment>
    );
  }
}

export default PoloniexController;