Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { numberWithDelimiter } from '../../../utilities';
  3. import PropTypes from 'prop-types';
  4. import Pagination from 'material-ui-pagination';
  5. import isEqual from 'lodash.isequal';
  6.  
  7. export default class OrderBookTable extends Component {
  8.  
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. perPage: 10,
  13. displayedData: [],
  14. displayedPage: 0,
  15. previousTable: [],
  16. currentData: []
  17. };
  18. }
  19.  
  20. emptyTableRow(i) {
  21. return (
  22. <tr key={i} className="blank-row hidden-xs">
  23. <td />
  24. <td />
  25. <td />
  26. <td />
  27. </tr>
  28. );
  29. }
  30.  
  31. renderOrderTable(orders, isBuy) {
  32. if (orders) {
  33. const prec = this.props.bidCurrency === 'XBT' || this.props.bidCurrency === 'ETH' ? 8 : 2;
  34. return orders.map((order, i) => {
  35. const newOrder = JSON.stringify(this.props.data[i]) !== JSON.stringify(this.state.previousTable[i]);
  36. const price = parseFloat(order[0]);
  37. const amount = order[1].volume;
  38.  
  39. if (!order) {
  40. return this.emptyTableRow(i);
  41. }
  42.  
  43. if (isBuy) {
  44. return (
  45. <tr key={i} className={( newOrder ? 'updatedOrderTr' : '' )} onClick={() => this.props.populateOrderForm(amount, price, isBuy)}>
  46. <td>{order[1].user_depth}</td>
  47. <td>{numberWithDelimiter((price * amount).toFixed(prec))}</td>
  48. <td>{amount}</td>
  49. <td>{price.toFixed(prec)}</td>
  50. </tr>
  51. );
  52. } else {
  53. return (
  54. <tr key={i} className={( newOrder ? 'updatedOrderTr' : '' )} onClick={() => this.props.populateOrderForm(amount, price, isBuy)}>
  55. <td>{price.toFixed(prec)}</td>
  56. <td>{amount}</td>
  57. <td>{numberWithDelimiter((price * amount).toFixed(prec))}</td>
  58. <td>{order[1].user_depth}</td>
  59. </tr>
  60. );
  61. }
  62. });
  63. }
  64. }
  65.  
  66. componentWillReceiveProps(props) {
  67. this.setState({previousTable: this.props.data});
  68. this.setState({currentData: props.data});
  69. if (props.data && props.data.length || this.props.data.length) this.updateDisplayedData(props.data, this.state.displayedPage);
  70. }
  71.  
  72. updateDisplayedData(data, displayedPage) {
  73. const offset = displayedPage * this.state.perPage;
  74. this.setState({
  75. displayedData: data.slice(offset, offset + this.state.perPage),
  76. pageCount: Math.ceil(data.length / this.state.perPage),
  77. displayedPage
  78. });
  79. }
  80.  
  81. changePage = (displayedPage) => {
  82. this.updateDisplayedData(this.props.data, displayedPage);
  83. }
  84.  
  85. render() {
  86. return (
  87. <div className="buySellOrderContainer">
  88. <div className="row">
  89. <div className="col-md-12">
  90. <h4 className="buySellOrderTitle">{this.props.title}</h4>
  91. </div>
  92. </div>
  93. <table className="table orderbook">
  94. <thead>
  95. {this.props.isBuy ? (
  96. <tr>
  97. <th>Price depth</th>
  98. <th>Total Cost</th>
  99. <th>Amount (NCCO)</th>
  100. <th>Price ({this.props.bidCurrency})</th>
  101. </tr>
  102. ) : (
  103. <tr>
  104. <th>Price ({this.props.bidCurrency})</th>
  105. <th>Amount (NCCO)</th>
  106. <th>Total Cost</th>
  107. <th>Price depth</th>
  108. </tr>
  109. )}
  110. </thead>
  111. <tbody>
  112. {this.renderOrderTable(this.state.displayedData, this.props.isBuy)}
  113. </tbody>
  114. </table>
  115. <Pagination
  116. total={this.state.pageCount}
  117. display={10}
  118. styleRoot={{display: 'flex', justifyContent: 'center'}}
  119. current={this.state.displayedPage + 1}
  120. onChange={(e) => this.changePage(--e)}
  121. />
  122. </div>
  123. );
  124. }
  125. }
  126.  
  127. OrderBookTable.propTypes = {
  128. data: PropTypes.array.isRequired,
  129. title: PropTypes.string.isRequired,
  130. bidCurrency: PropTypes.string.isRequired,
  131. isBuy: PropTypes.bool.isRequired,
  132. populateOrderForm: PropTypes.func.isRequired
  133. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement