Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. import React from 'react';
  2. // import axios from 'axios';
  3. import { withRouter } from 'react-router-dom';
  4. import PropTypes from 'prop-types';
  5. // import classNames from 'classnames';
  6. import {
  7. withStyles,
  8. // Card,
  9. // CardContent,
  10. // Typography,
  11. // Button,
  12. // IconButton,
  13. // Fab,
  14. Paper,
  15. Typography,
  16. // Collapse
  17. } from '@material-ui/core';
  18. // import { showMessage } from 'app/store/actions/fuse';
  19.  
  20. // import EditIcon from '@material-ui/icons/Edit';
  21. // import DeleteIcon from '@material-ui/icons/Delete';
  22. // import PlayArrowIcon from '@material-ui/icons/PlayArrow';
  23. // import CircularProgress from '@material-ui/core/CircularProgress';
  24. // import AddIcon from '@material-ui/icons/Add';
  25. // import ViewListIcon from '@material-ui/icons/ViewList';
  26. // import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
  27. import _ from '@lodash';
  28.  
  29. import { bindActionCreators } from 'redux';
  30. import connect from 'react-redux/es/connect/connect';
  31. import withReducer from 'app/store/withReducer';
  32. import * as Actions from '../store/actions';
  33. import reducer from '../store/reducers';
  34.  
  35. import NewProductTable from './ProductTable';
  36. import styles from '../styles/styles';
  37. import UtilityFunction from '../../../modules/UtilityFunction';
  38. // import { SalesChannelManager } from '../../../context/sales_channel/salesChannelManager';
  39.  
  40. import { withTranslation, Trans } from 'react-i18next';
  41. import i18n from '../../../../i18n';
  42.  
  43. const initialState = {
  44. liveEventProductList: {},
  45. eventID: ''
  46. };
  47.  
  48. class LiveEventProductTable extends React.Component {
  49.  
  50. state = { ...initialState };
  51. tableCol = [];
  52.  
  53. componentDidMount() {
  54.  
  55. this.loadLiveEventProductList();
  56.  
  57. }
  58.  
  59. componentDidUpdate(prevProps, prevState) {
  60.  
  61. if (!_.isEqual(this.props.productList, prevProps.productList)) {
  62. this.loadLiveEventProductList();
  63. }
  64.  
  65. if (!_.isEqual(this.props.products, prevProps.products)) {
  66. this.loadLiveEventProductList();
  67. }
  68.  
  69. if (!_.isEqual(this.props.eventID, prevProps.eventID)) {
  70. this.setState({ eventID: this.props.eventID });
  71. }
  72.  
  73. // if( !_.isEqual(this.props.liveEventProductList, prevProps.liveEventProductList) ) {
  74. // console.log('this.props.liveEventProductList =========> ', this.props.liveEventProductList);
  75. // this.setState({ liveEventProductList: this.props.liveEventProductList });
  76. // }
  77.  
  78. }
  79.  
  80. loadLiveEventProductList = () => {
  81. const data = this.props.productList;
  82.  
  83. const newProductList = {};
  84. Object.keys(data).map((key, index) => {
  85.  
  86. if (this.props.products) {
  87. if (this.props.products.indexOf(data[key].productID) > -1) {
  88. newProductList[this.props.products.indexOf(data[key].productID)] = data[key];
  89. }
  90. }
  91.  
  92. return newProductList;
  93. });
  94.  
  95. this.setState({ liveEventProductList: newProductList });
  96.  
  97. // this.props.getLiveEventProductList({ productList: this.props.products});
  98. };
  99.  
  100. handleEditProductBtnClick = productID => {
  101. this.props.pushTrackingData("Click", "Edit product from LIVE button");
  102. let eventID = this.state.eventID;
  103. this.props.openProductInfoFormPrompt(eventID, productID);
  104. }
  105.  
  106. handleDeleteProductBtnClick = productID => {
  107. //alert('Delete =======> '+productID);
  108.  
  109. this.props.pushTrackingData("Click", "Delete product from LIVE button");
  110. let eventID = this.state.eventID;
  111. this.props.openDeleteProductPrompt(eventID, productID);
  112. }
  113.  
  114. render() {
  115. const { classes, rowHeight, tableHeight } = this.props;
  116.  
  117. let tableSrc = [];
  118. if (this.state.liveEventProductList) {
  119. let data = this.state.liveEventProductList;
  120.  
  121. Object.keys(data).map((key, index) => {
  122.  
  123. //if( liveEvent.products.indexOf( data[key].productID ) > -1 ) {
  124. let product = data[key].productInfo;
  125.  
  126. tableSrc.push({
  127. index: (index + 1),
  128. image: product.productImage ? product.productImage : '',
  129. name: product.productName,
  130. hashtag: '#' + product.productHashtag,
  131. stock: UtilityFunction.getProductStock(product),
  132. // category: product.productTypeOption.label ? product.productTypeOption.label : "N/A",
  133. // brand: product.productBrandName,
  134. price: UtilityFunction.getProductPrice(product),
  135. productOption: data[key].productID
  136. });
  137. //}
  138. return tableSrc;
  139. });
  140.  
  141. }
  142.  
  143. if (this.tableCol.length === 0) {
  144. // สร้าง Table columns
  145. this.tableCol.push({
  146. width: rowHeight ? rowHeight : 40,
  147. label: '',
  148. dataKey: 'image',
  149. image: true,
  150. });
  151.  
  152. if (!UtilityFunction.useMediaQuery('(max-width: 550px)')) {
  153. this.tableCol.push({
  154. width: 100,
  155. flexGrow: 1.0,
  156. label: i18n.t('product.product-name'),
  157. dataKey: 'name',
  158. });
  159.  
  160. this.tableCol.push({
  161. width: 100,
  162. label: i18n.t('product.product-form-input-product-hashtag'),
  163. dataKey: 'hashtag',
  164. });
  165. } else {
  166. this.tableCol.push({
  167. width: 100,
  168. flexGrow: 1.0,
  169. label: i18n.t('product.product-form-input-product-hashtag'),
  170. dataKey: 'hashtag',
  171. });
  172. }
  173.  
  174. this.tableCol.push({
  175. width: 60,
  176. label: i18n.t('product.product-form-input-product-variations-stock'),
  177. dataKey: 'stock',
  178. });
  179.  
  180. this.tableCol.push({
  181. width: 60,
  182. label: i18n.t('product.price'),
  183. dataKey: 'price'
  184. });
  185.  
  186. if (this.props.canEditProducct === true) {
  187. this.tableCol.push({
  188. width: 20,
  189. label: '',
  190. dataKey: 'productOption',
  191. productOption: true,
  192. });
  193. }
  194.  
  195. }
  196.  
  197.  
  198. return (
  199. <React.Fragment>
  200. {tableSrc.length > 0 ? (
  201. <Paper className={classes.productTableWrapper} style={tableHeight && { height: tableHeight }} >
  202. <NewProductTable
  203. rowCount={tableSrc.length}
  204. rowGetter={({ index }) => tableSrc[index]}
  205. items={tableSrc}
  206. onRowClick={this.props.onRowClick !== null ? this.props.onRowClick : null}
  207. columns={this.tableCol}
  208. rowHeight={rowHeight || 50}
  209. colPaddingRight={10}
  210. onEditProductBtnClick={this.handleEditProductBtnClick}
  211. onDeleteProductBtnClick={this.handleDeleteProductBtnClick}
  212. selectedRowIndex={this.props.selectedRowIndex}
  213. />
  214. </Paper>
  215. ) : (
  216. <div className={classes.productTableWrapper} style={{ height: tableHeight }}>
  217. <Typography><Trans i18nKey="live-event.no-product">No product in this LIVE</Trans></Typography>
  218. </div>
  219. )}
  220.  
  221. </React.Fragment>
  222. );
  223.  
  224. }
  225. }
  226.  
  227. LiveEventProductTable.propTypes = {
  228. classes: PropTypes.object.isRequired,
  229. };
  230.  
  231.  
  232. function mapDispatchToProps(dispatch) {
  233. return bindActionCreators({
  234. // openDeleteLiveEventPrompt : Actions.openDeleteLiveEventPrompt,
  235. openDeleteProductPrompt: Actions.openDeleteProductPrompt,
  236. openProductInfoFormPrompt: Actions.openProductInfoFormPrompt,
  237. getLiveEventProductList: Actions.getLiveEventProductList,
  238. // openExistingProductPrompt : Actions.openExistingProductPrompt,
  239. // updateLiveEvent : Actions.updateLiveEvent,
  240. // getProductsList : Actions.getProductsList
  241. }, dispatch);
  242. }
  243.  
  244. function mapStateToProps({ liveEventsApp }) {
  245. return {
  246. productList: liveEventsApp.liveEvents.productList,
  247. liveEventProductList: liveEventsApp.liveEvents.liveEventProductList
  248. // productList : liveEventsApp.liveProductSlider
  249. };
  250. }
  251.  
  252. // ( connect(mapStateToProps, mapDispatchToProps)( withTranslation()(LiveEventProductTable) ) )
  253. export default withReducer('liveEventsApp', reducer)(withStyles(styles, { withTheme: true })(withRouter(connect(mapStateToProps, mapDispatchToProps)(withTranslation()(LiveEventProductTable)))));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement