import React, {Component} from 'react'; import {Link} from "react-router-dom"; import routes from '../../../constants/routes.json'; import classes from './index.css'; type Props = { products: any; auth: any; pageNum: number; getPageName: (name: string) => void; getProducts: (id: string) => void; getPaginationNum: (num: number) => void; location: any; } interface State { chunkArray: any; numOfItems: number; show: boolean; isHide: boolean; searchBy: string; inputValue: string; timeout: any; isHide2: boolean; sort: string; } class ProductsList extends Component { constructor(props: Props) { super(props); this.state = { chunkArray: [], numOfItems: 5, show: true, isHide: false, isHide2: false, searchBy: "trade_name", inputValue: "", timeout: 0, sort: "" }; } componentDidMount(): void { this.checkPage(); if(this.props.auth.authenticateUser && this.state.show) { this.props.getProducts(this.props.auth.authenticateUser.UsersId); this.chunkArray(this.props.products, this.state.numOfItems); } } componentDidUpdate(prevProps: Props, prevState: State): void { if (prevState.show !== this.state.show) { this.props.getProducts(this.props.auth.authenticateUser.UsersId); this.chunkArray(this.props.products, this.state.numOfItems); } if (prevProps.products.length !== this.props.products.length && this.state.show) { this.props.getProducts(this.props.auth.authenticateUser.UsersId); this.chunkArray(this.props.products, this.state.numOfItems); } if (prevProps.location.pathname !== this.props.location.pathname) this.checkPage(); } checkPage = () :void => { this.props.location.pathname === routes.PRODUCT_NEW || this.props.location.pathname === routes.PRODUCT_EDIT? this.setState({show: false}): this.setState({show: true}); }; chunkArray = (myArray: any, chunk_size: number): void => { let arrayLength = myArray.length; let tempArray = []; let myChunk; for (let index = 0; index < arrayLength; index += chunk_size) { myChunk = myArray.slice(index, index+chunk_size); tempArray.push(myChunk); } this.setState({chunkArray: tempArray}); }; sort = (sortBy: string) => { let products:any = []; for(let i = 0; i < this.state.chunkArray.length; i++) { products = products.concat(this.state.chunkArray[i]) } if (sortBy === "remain_amount" || sortBy === "sale_price" || sortBy === "purchase_price" || sortBy === "expire_date") { if (this.state.sort === "ascending") { products = products.sort((a: any, b: any) => Number(a[sortBy]) - Number(b[sortBy])); this.setState({sort: "descending"}); } else { products = products.sort((a: any, b: any) => Number(b[sortBy]) - Number(a[sortBy])); this.setState({sort: "ascending"}); } } else if (sortBy === "manufacture" || sortBy === "trade_name") { if (this.state.sort === "ascending") { products = products.sort((a: any, b:any) => b[sortBy].localeCompare(a[sortBy])); this.setState({sort: "descending"}); } else { products = products.sort((a: any, b:any) => a[sortBy].localeCompare(b[sortBy])); this.setState({sort: "ascending"}); } } this.chunkArray(products, this.state.numOfItems); }; renderHead = () => { return ( # this.sort("trade_name")}> ITEM NAME this.sort("manufacture")}> COUNTRY {/* this.sort("product_dosage")}>*/} {/* DOSE*/} {/**/} this.sort("sale_price")}> PRICE this.sort("purchase_price")}> COST this.sort("expire_date")}> EXPIRE this.sort("remain_amount")}> COUNT ACTIONS ) }; renderBody = () => { if(this.state.chunkArray.length > 0) { if(this.state.chunkArray[Number(this.props.pageNum) - 1]) { return this.state.chunkArray[Number(this.props.pageNum) - 1].map((item: any, i: number) => { const date = `${new Date(item.expire_date *1000).getFullYear()}/${new Date(item.expire_date *1000).getMonth() + 1}`; const id = i + 1; return ( {((this.props.pageNum - 1) * this.state.numOfItems) + id} {item.trade_name} {item.manufacture} {Number(item.sale_price).toFixed()} {Number(item.purchase_price).toFixed(2)} {date} {item.remain_amount} ) }) } } }; onPaginationClick = (i: number) => { this.props.getPaginationNum(i) }; renderPagination = () => { const { pageNum } = this.props; if(this.props.products) { if (this.state.chunkArray. length > 0 ) { return( <> {pageNum > 3 &&
  • this.onPaginationClick(1)}> 1 ...
  • } {pageNum - 1 > 0 &&
  • this.onPaginationClick(pageNum - 1)}> {pageNum - 1}
  • }
  • this.onPaginationClick(pageNum)}> { pageNum }
  • {this.state.chunkArray[pageNum] &&
  • this.onPaginationClick(pageNum + 1)}> {pageNum + 1}
  • } {this.state.chunkArray[pageNum + 1] &&
  • this.onPaginationClick(pageNum + 2)}> {pageNum + 2}
  • } {pageNum === 1 && this.state.chunkArray.length > 3 &&
  • this.onPaginationClick(pageNum + 3)}> {pageNum + 3}
  • } ) } return ""; } else { return ""; } }; navigatePrev = () => { if(this.props.pageNum > 1) { const pageNum = this.props.pageNum - 1; this.props.getPaginationNum(pageNum) } }; navigateFor = () =>{ if (this.props.pageNum < this.state.chunkArray.length) { const pageNum = this.props.pageNum + 1; this.props.getPaginationNum(pageNum) } }; onDropDownClick = (name: string) => { this.setState({searchBy: name}); this.setState({isHide: false}) }; onInputChange = (e: any) => { const reports = this.props.products; if(reports.length > 0) { let value = e.target.value.replace(new RegExp("\\\\", "g"), ""); this.checkValue(value, this.state.numOfItems) } }; checkValue = (value: string, numOfItems: number) => { const reports = this.props.products; const searchBy = this.state.searchBy; this.setState({inputValue: value}); let timeout; if (this.state.timeout) { clearTimeout(this.state.timeout) } if (value.length > 0 && reports) { timeout = setTimeout(() => { this.onPaginationClick(1); this.setState({inputValue: value}); let items = reports.filter((item: any) => { return item[searchBy].toLowerCase().search(value.toLowerCase()) !== -1; }); this.chunkArray(items, numOfItems); }, 300); this.setState({timeout: timeout}); } else { this.chunkArray(this.props.products, numOfItems); this.setState({inputValue: ""}); } }; onNumberClick = (num: number) => { this.setState({numOfItems: num}); // this.chunkArray(this.props.products, num); this.checkValue(this.state.inputValue, num); this.setState({isHide2: false}); }; renderHeader () { const selectStyle = this.state.isHide ? "dropdown show": "dropdown"; const dropMenue = this.state.isHide ? "dropdown-menu show" : "dropdown-menu"; const selectStyle2 = this.state.isHide2 ? "dropdown show": "dropdown"; const dropMenue2 = this.state.isHide2 ? "dropdown-menu show" : "dropdown-menu"; return (
    e.stopPropagation()}>
    e.stopPropagation()}>
    Add Product
    ) } render() { if (this.state.show) { return (
    { this.setState({isHide2: false}); this.setState({isHide: false}) }} >
    {this.renderHeader()}
    {this.renderHead()} {this.renderBody()}