Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { Table, Icon, Segment, Grid } from "semantic-ui-react";
  3. import Button from '@material-ui/core/Button';
  4. import TablePagination from '@material-ui/core/TablePagination';
  5.  
  6. class ExpandableTable extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. data: props.orders.slice(props.page * props.rowsPerPage, (props.page + 1) * props.rowsPerPage),
  11. expandedRows: []
  12. };
  13. }
  14.  
  15. handleRowClick(event,rowId) {
  16. const currentExpandedRows = this.state.expandedRows;
  17. const isRowCurrentlyExpanded = currentExpandedRows.includes(rowId);
  18. const newExpandedRows = isRowCurrentlyExpanded
  19. ? currentExpandedRows.filter(id => id !== rowId)
  20. : currentExpandedRows.concat(rowId);
  21. this.setState({ expandedRows: newExpandedRows });
  22. }
  23.  
  24. renderItemCaret(rowId) {
  25. const currentExpandedRows = this.state.expandedRows;
  26. const isRowCurrentlyExpanded = currentExpandedRows.includes(rowId);
  27.  
  28. if (isRowCurrentlyExpanded) {
  29. return <Icon name="caret down" />;
  30. } else {
  31. return <Icon name="caret right" />;
  32. }
  33. }
  34.  
  35. renderItemDetails(item) {
  36. return (
  37. <Segment basic>
  38. <Grid columns={this.props.role===2?8:7}>
  39. <Grid.Column>
  40. <div className="expanded-content">
  41. <h5>Order: </h5>
  42. {item.meals.map((meal)=>{
  43. return <div className="details" key={meal._id}>
  44. <span><b>Quantity: </b>{meal.quantity} </span><span><b>Meal:</b> {meal.name}</span> <span><b>Price:</b> {meal.price/100+"€"}</span>
  45. </div>
  46. })
  47. }
  48. </div>
  49. <div className="expanded-content">
  50. {item.history.length>0&&<h5>Status history: </h5>}
  51. {item.history.map((entry)=>{
  52. return <div className="details" key={entry._id}>
  53. <span>{entry.time.replace('T',' ').split('.')[0]} </span><span><b>:&emsp;</b> { entry.status}</span>
  54. </div>
  55. })
  56. }
  57. </div>
  58. </Grid.Column>
  59. </Grid>
  60. </Segment>
  61. );
  62. }
  63.  
  64. renderItem(item, index) {
  65. const clickCallback = (event) => this.handleRowClick(event,index);
  66. const itemRows = [
  67. <Table.Row onClick={clickCallback} key={"row-data-" + index}>
  68. <Table.Cell>{this.renderItemCaret(index)}</Table.Cell>
  69. <Table.Cell>{item.createdAt.replace('T',' ').split('.')[0]}</Table.Cell>
  70. <Table.Cell>{item.updatedAt.replace('T',' ').split('.')[0]}</Table.Cell>
  71. <Table.Cell>{item.restaurant.name}</Table.Cell>
  72. <Table.Cell>{item.totalAmmount/100+"€"}</Table.Cell>
  73. {this.props.role===2&&<Table.Cell>{item.customer.name}</Table.Cell>}
  74. <Table.Cell>{item.status}</Table.Cell>
  75. <Table.Cell>
  76. {item.status==="Placed"&&this.props.role===3&&
  77. <Button variant="contained" className="cancel" onClick={(event)=>{event.stopPropagation();this.props.handleStatus(item._id,"Canceled")}}>
  78. Cancel
  79. </Button>}
  80. {item.status==="Placed"&&this.props.role===2&&
  81. <Button variant="contained" className="processing" onClick={(event)=>{event.stopPropagation();this.props.handleStatus(item._id,"Processing")}}>
  82. Processing
  83. </Button>}
  84. {item.status==="Processing"&&this.props.role===2&&
  85. <Button variant="contained" className="inroute" onClick={(event)=>{event.stopPropagation();this.props.handleStatus(item._id,"In Route")}}>
  86. In Route
  87. </Button>}
  88. {item.status==="In Route"&&this.props.role===2&&
  89. <Button variant="contained" className="delivered" onClick={(event)=>{event.stopPropagation();this.props.handleStatus(item._id,"Delivered")}}>
  90. Delivered
  91. </Button>}
  92. {item.status==="Delivered"&&this.props.role===3&&
  93. <Button variant="contained" className="received" onClick={(event)=>{event.stopPropagation();this.props.handleStatus(item._id,"Received")}}>
  94. Received
  95. </Button>}
  96. {(item.status==="Received"||item.status==="Canceled")&&this.props.role===2&&!item.restaurant.blockedUsers.includes(item.customer._id)&&
  97. <Button variant="contained" className="block" onClick={(event)=>{event.stopPropagation();this.props.blockUser("block",item.customer._id, item.restaurant._id)}}>
  98. Block user
  99. </Button>}
  100. {(item.status==="Received"||item.status==="Canceled")&&this.props.role===2&&item.restaurant.blockedUsers.includes(item.customer._id)&&
  101. <Button variant="contained" className="block" onClick={(event)=>{event.stopPropagation();this.props.blockUser("unblock", item.customer._id, item.restaurant._id)}}>
  102. Unblock user
  103. </Button>}
  104. </Table.Cell>
  105. </Table.Row>
  106. ];
  107.  
  108. if (this.state.expandedRows.includes(index)) {
  109. itemRows.push(
  110. <Table.Row key={"row-expanded-" + index}>
  111. <Table.Cell colSpan={this.props.role===2?"8":"7"}>{this.renderItemDetails(item)}</Table.Cell>
  112. </Table.Row>
  113. );
  114. }
  115. return itemRows;
  116. }
  117.  
  118. render() {
  119. let allItemRows = [];
  120. this.state.data.forEach((item, index) => {
  121. const perItemRows = this.renderItem(item, index);
  122. allItemRows = allItemRows.concat(perItemRows);
  123. });
  124.  
  125. return (
  126. <Table selectable>
  127. <Table.Header>
  128. <Table.Row>
  129. <Table.HeaderCell />
  130. <Table.HeaderCell>Date</Table.HeaderCell>
  131. <Table.HeaderCell>Last update</Table.HeaderCell>
  132. <Table.HeaderCell>Restaurant</Table.HeaderCell>
  133. <Table.HeaderCell>Total Ammount</Table.HeaderCell>
  134. {this.props.role===2&&<Table.HeaderCell>User</Table.HeaderCell>}
  135. <Table.HeaderCell>Status</Table.HeaderCell>
  136. <Table.HeaderCell>Actions</Table.HeaderCell>
  137. </Table.Row>
  138. </Table.Header>
  139. <Table.Body>{allItemRows}</Table.Body>
  140. <Table.Footer>
  141. <Table.Row>
  142. <TablePagination
  143. count={this.props.orders.length}
  144. page={this.props.page}
  145. rowsPerPage={this.props.rowsPerPage}
  146. onChangePage={this.props.onChangePage}
  147. rowsPerPageOptions={[10, 25]}
  148. onChangeRowsPerPage={this.props.onChangeRowsPerPage}
  149. />
  150. </Table.Row>
  151. </Table.Footer>
  152. </Table>
  153. );
  154. }
  155. }
  156.  
  157. export default ExpandableTable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement