import React, {Component} from 'react'; import { History, LocationState } from 'history'; import moment from 'moment'; import routes from '../../../constants/routes.json' import ProductsForm from "./ProductsForm"; import StoreForm from "./StoreForm"; import classes from "./index.css"; type Props = { getPageName: (name: string) => void; auth: any; createProductDetails: (values: any, products: any, callback: () => void) => void; buyInvoice: number; getBuyInvoice: (pharmacyId: string) => void; history: History; getProducts: (usersId: number) => void; products: any; getAllPharmaciesItems: (id: number) => void; }; interface State { addedProducts: any; totalCash: number; isHide: boolean; searchBy: string; value: string; foundProducts: any; timeout: any; inputValue: string; clickedItem: any; } class ProductNew extends Component { constructor(props: Props) { super(props); this.state = { addedProducts: [], totalCash: 0, isHide: false, searchBy: "barcode", value: "", foundProducts: [], timeout: 0, inputValue: "", clickedItem: {} } } componentDidMount(): void { const { auth, getPageName, getBuyInvoice, getAllPharmaciesItems } = this.props; getPageName("products | new"); if(auth.authenticateUser) { getBuyInvoice(auth.authenticateUser.UsersId); getAllPharmaciesItems(auth.authenticateUser.UsersId); } setTimeout(() => { window.scrollTo(0, 1); }, 300); } componentDidUpdate(prevProps: Readonly): void { const { auth } = this.props; if(auth.authenticateUser) { if(prevProps.products.length !== this.props.products.length) { this.props.getAllPharmaciesItems(auth.authenticateUser.UsersId); } } } submitHandle = (formValues: any) => { const values = formValues; values.manufacture_date = new Date(formValues.manufacture_date).getTime() / 1000; values.expire_date = new Date(formValues.expire_date).getTime() / 1000; values.purchase_invoice = this.props.buyInvoice; values.id = this.props.auth.authenticateUser.UsersId; this.setState(prevState => ({ addedProducts: [...prevState.addedProducts, {...values}], totalCash: Number(prevState.totalCash + (values.purchase_price * values.purchase_amount)) })); this.setState({clickedItem: []}); setTimeout(() => { window.scrollBy(0, 2000); }, 500); }; renderButton() { return ( ) } renderHeader = () => { if(this.props.buyInvoice) { return (
# {this.props.buyInvoice}
) } return ""; }; renderHead = () => { return ( # ITEM NAME COUNTRY cost COUNT Actions ) }; onDelete = (i: number) => { this.setState(prevState => ({ addedProducts: prevState.addedProducts.filter((_: number, id: number) => id !== i), totalCash: Number(prevState.totalCash - (prevState.addedProducts[i].purchase_price * prevState.addedProducts[i].purchase_amount)) })); }; renderBody = () => { if(this.state.addedProducts.length > 0) { return this.state.addedProducts.map((item: any, i: number) => { return ( {i + 1} {item.trade_name} {item.manufacture} {item.purchase_price} {item.purchase_amount} ) }) } }; onSave = (formValues: any) => { const values = formValues; values.purchase_invoice = this.props.buyInvoice; values.id = this.props.auth.authenticateUser.UsersId; values.total_invoice_price = this.state.totalCash; if(!values.note) { values.note =""; } if(formValues.total_invoice_price && formValues.id) { this.props.createProductDetails(values, this.state.addedProducts, () => { this.props.history.push(routes.PRODUCTS); }); } }; renderFooter = () => { if(this.state.addedProducts.length > 0) { return (
) } return ""; }; handleChange = (e: any) => { const products = this.props.products; if(products.length > 0) { let value = e.target.value.replace(new RegExp("\\\\", "g"), ""); const searchBy = this.state.searchBy; this.setState({value: value}); let timeout; if (this.state.timeout) { clearTimeout(this.state.timeout) } if (value.length > 0 && products) { timeout = setTimeout(() => { this.setState({inputValue: value}); let items = products.filter((item: any) => { return item[searchBy].toLowerCase().search(value.toLowerCase()) !== -1; }); this.setState({foundProducts: items}); }, 300 ); this.setState({timeout: timeout}); } else { this.setState({foundProducts: []}); this.setState({inputValue: ""}); } } }; renderFound = () => { return (
{this.state.foundProducts.map((item: any) => { const time = item.expire_date * 1000; const date = `${new Date(time).getFullYear()}/${new Date(time).getMonth() + 1}`; return(
this.onItemClick(item)} key={item.id} className={classes.hoveredItem}>

{item.trade_name}

{item.sale_price}

{date}

) })}
) }; onItemClick = (item: any) => { this.setState({clickedItem: item}); this.setState({inputValue: ""}); this.setState({foundProducts: []}) }; renderNotFound() { return (

Oops! Item not found

) } renderFoundProducts = ()=> { if(this.state.inputValue.length > 0) { if(this.state.foundProducts.length === 1 && this.state.searchBy === "barcode") { this.onItemClick(this.state.foundProducts[0]); } return (
{ this.state.foundProducts.length > 0 ? this.renderFound(): this.renderNotFound() }
) } return ""; }; onDropDownClick = (by: string) => { this.setState({searchBy: by}); this.setState({isHide: false}); }; renderInput = () => { const selectStyle = this.state.isHide ? "dropdown show": "dropdown"; const dropMenue = this.state.isHide ? "dropdown-menu show" : "dropdown-menu"; return (
this.handleChange(e)} /> {this.renderFoundProducts()}
) }; render() { return (
{this.renderInput()} this.renderButton()} initialValues={this.state.clickedItem.trade_name && { ...this.state.clickedItem, manufacture_date: moment(this.state.clickedItem.manufacture_date * 1000).format('YYYY-MM-DD'), expire_date: moment(this.state.clickedItem.expire_date * 1000).format('YYYY-MM-DD') }} /> { this.state.addedProducts.length > 0 &&
{this.renderHeader()} {this.renderHead()} {this.renderBody()}
{this.renderFooter()}
}
); } } export default ProductNew;