import React, {Component} from 'react'; import {History, LocationState} from 'history'; import classes from "./index.css"; type Props = { getPageName: (pageName: string) => void; auth: any; getBuyingReports: (id: string) => void; reports: any; history: History; }; type State = { reports: any; timeout: any; inputValue: string; searchBy: string; chunkArray: any; pageNum: number; } class ShowReports extends Component { constructor(props: Props) { super(props); this.state = { reports: [], timeout: 0, inputValue: "", searchBy: "invoice_number", chunkArray: [], pageNum: 1 }; } componentDidMount() { this.props.getPageName("reports | buying"); if(this.props.auth.authenticateUser) { this.props.getBuyingReports(this.props.auth.authenticateUser.UsersId); // const reOrdered = this.props.reports.sort((a: number, b: number) => a > b) this.chunkArray(this.props.reports, 8); } } componentDidUpdate(prevProps: Props) { if(prevProps.reports.length !== this.props.reports.length){ this.chunkArray(this.props.reports, 8); } } 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}); }; onInputChange = (e: any) => { const reports = this.props.reports; if(reports.length > 0) { let value = e.target.value.replace(new RegExp("\\\\", "g"), ""); this.checkValue(value, 8) } }; checkValue = (value: string, numOfItems: number) => { const reports = this.props.reports; 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.setState({pageNum: 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.reports, numOfItems); this.setState({inputValue: ""}); } }; renderHeader = () => { return (
) }; onReportClick = (invoice: number, total: number): void => { this.props.history.push(`/logged/reports/buying/${invoice}/${total}`); }; renderContent = () => { if (this.state.chunkArray.length > 0) { if (this.state.chunkArray[Number(this.state.pageNum) - 1]) { return (
{this.state.chunkArray[this.state.pageNum - 1].map((report: any) => { const date = `${new Date(report.purchase_date * 1000).getFullYear()}/ ${new Date(report.purchase_date * 1000).getMonth() + 1}/ ${new Date(report.purchase_date * 1000).getDate()}`; return (
this.onReportClick(report.invoice_number, report.total_invoice_price)} >
#{report.invoice_number}

Store: {report.store_name} Price: {Number(report.total_invoice_price).toFixed(2)} Store invoice: #{report.store_sale_invoice} Price: {Number(report.total_invoice_price).toFixed(2)} Date: {date}

) })}
) } return ""; } return ""; }; navigatePrev = () => { if(this.state.pageNum > 1) { this.setState({pageNum: this.state.pageNum - 1}) } }; navigateFor = () =>{ if (this.state.pageNum < this.state.chunkArray.length) { this.setState({pageNum: this.state.pageNum + 1}) } }; renderNumber = () => { const { pageNum } = this.state; if(this.props.reports) { if (this.state.chunkArray. length > 0 ) { return( <> {pageNum > 3 &&
  • this.setState({pageNum:1 })}> 1 ...
  • } {pageNum - 1 > 0 &&
  • this.setState({pageNum: pageNum - 1})}> {pageNum - 1}
  • }
  • this.setState({pageNum: pageNum})}> { pageNum }
  • {this.state.chunkArray[pageNum] &&
  • this.setState({pageNum: pageNum + 1})}> {pageNum + 1}
  • } {this.state.chunkArray[pageNum + 1] &&
  • this.setState({pageNum: pageNum + 2})}> {pageNum + 2}
  • } {pageNum === 1 && this.state.chunkArray.length > 3 &&
  • this.setState({pageNum: pageNum + 3})}> {pageNum + 3}
  • } ) } return ""; } else { return ""; } }; renderPagination = () => { return (