import React, {Component} from 'react'; import {connect} from 'react-redux'; import {Input, Tooltip, Row, Col} from 'reactstrap'; import '../style/Cart.css'; import {updateMessages} from '../../../actions/notification/notificationActions'; import {updateQuantity, removeCartItem} from '../../../actions/cart/cartActions'; import {setDialogContent, setDialogOpenFlag} from '../../../actions/dialog/dialogActions'; import {cartMessages, cartTexts} from '../../../constants/cartConstants'; import PackageBids from './PackageBids.jsx'; import {coMarketTexts} from "../../../constants/coMarketConstants"; class CartItem extends Component { constructor(props) { super(props); this.state = { tooltipOpen: false, itemQuantity: 1, key: 0 }; this.toggleTooltip = this.toggleTooltip.bind(this); this.removeItemFromCart = this.removeItemFromCart.bind(this); this.resetQuantity = this.resetQuantity.bind(this); this.sumPrices = this.sumPrices.bind(this); } componentWillReceiveProps(nextProps) { this.setState({itemQuantity: nextProps.cartItem.quantity}); this.sumPrices(nextProps.cartItem, nextProps.cartItem.quantity); } componentDidMount() { this.setState({itemQuantity: this.props.cartItem.quantity}); this.sumPrices(this.props.cartItem, this.props.cartItem.quantity); } isQuantityValid(quantity) { return quantity > 0 && quantity <= 65000; } updateQuantity(cartItem, quantity) { if(quantity) { if(this.isQuantityValid(quantity)) { this.setState({itemQuantity: quantity}); this.sumPrices(cartItem, quantity); this.props.dispatch(updateQuantity(cartItem, quantity)); } else { this.props.dispatch(updateMessages([{code: 'error', message: 'INVALID_QUANTITY'}], cartMessages)); } } else { this.setState({itemQuantity: ''}); } } resetQuantity(cartItem) { if(!this.state.itemQuantity || !this.isQuantityValid(this.state.itemQuantity)) { this.updateQuantity(cartItem, 1); } } removeItemFromCart() { this.props.dispatch(removeCartItem(this.state.key)); } setDialogParams(cartItem, dialogContent) { this.setState({key: cartItem.key}); this.props.dispatch(setDialogOpenFlag(true)); dialogContent.bodyVariable = cartItem.packageName; this.props.dispatch(setDialogContent(dialogContent)); } toggleTooltip() { this.setState({ tooltipOpen: !this.state.tooltipOpen }); } sumPrices(cartItem, quantity) { let total = 0; let oldTotal = null; let totalRecurrent = 0; let oldTotalRecurrent = null; const selectedBid = cartItem.bids.find((bid) => { return cartItem.idSelectedBid === bid.idBid; }); if(selectedBid){ total += selectedBid.fixedPrice; totalRecurrent += selectedBid.servicesPrice + selectedBid.recurrentPrice; oldTotal = cartItem.fixedPrice; oldTotalRecurrent = cartItem.servicesPrice + cartItem.recurrentPrice; }else{ total += cartItem.fixedPrice; totalRecurrent += cartItem.servicesPrice + cartItem.recurrentPrice; } if(cartItem.options.length) { cartItem.options.forEach((packageOption) => { if(Object.keys(packageOption.prices).length) { total += parseFloat(packageOption.prices.fixedExtra); totalRecurrent += parseFloat(packageOption.prices.recurrentExtra) + parseFloat(packageOption.prices.servicesExtra); } }); } if(cartItem.additionalPackages.length) { cartItem.additionalPackages.forEach((additionalPackage) => { if(Object.keys(additionalPackage).length) { total += parseFloat(additionalPackage.prices.fixedExtra); totalRecurrent += parseFloat(additionalPackage.prices.recurrentExtra) + parseFloat(additionalPackage.prices.servicesExtra); } }); } this.setState({ fixedPrice: total * quantity, recurrentPrice: totalRecurrent * quantity, fixedOldPrice: oldTotal !== null ? oldTotal * quantity : null, recurrentOldPrice: oldTotalRecurrent !== null ? oldTotalRecurrent * quantity: null }); } getPriceClass(price){ return price !== null ? 'new-price' : 'price'; } render() { const dialogContent = { buttons: [ { color: 'success', action: this.removeItemFromCart, name: cartTexts.buttons.YES, id: 'remove-item-confirmation' }, { color: 'secondary', name: cartTexts.buttons.CANCEL, id: 'cancel-remove-item' } ], header: cartTexts.labels.REMOVE_ITEM_HEADER, body: cartTexts.labels.REMOVE_ITEM_TEXT }; const {cartItem, isFormDisabled} = this.props; const cartItemNo = this.props.cartItemNo ? this.props.cartItemNo + 1 : 1; return ( {cartItem.packageName} { isFormDisabled ? cartItem.quantity === 1 ? cartItem.quantity + ' pc' : cartItem.quantity + ' pcs' : this.updateQuantity(cartItem, e.target.value)} onBlur={e => this.resetQuantity(cartItem, e.target.value)} /> } { !isFormDisabled &&
} { !isFormDisabled && ( {cartTexts.labels.REMOVE_FROM_CART} ) }
{ (!cartItem.areAdditionalAvailable || !cartItem.areOptionsAvailable || cartItem.status === 'not-available') && {cartTexts.labels.PACKAGE_UNAVAILABLE} } {cartItem.options.length > 0 && cartItem.options.map((packageOption) => {packageOption.groupName}: {packageOption.packageName} ) } { cartItem.additionalPackages.length > 0 && {cartTexts.labels.ADDITIOONAL_PACKAGE}: { cartItem.additionalPackages.map((additionalPackage) => {additionalPackage.packageName} ) } } {cartItem.payType}:
{cartTexts.labels.ON_DELIVERY}: {cartTexts.labels.MONTHLY}:
{ this.state.fixedPrice &&
{ this.state.fixedOldPrice !== null &&
{this.state.fixedOldPrice}
}
{this.state.fixedPrice.toLocaleString() + ' ' + cartItem.country.currency}
} { this.state.recurrentPrice &&
{ this.state.recurrentOldPrice !== null &&
{this.state.recurrentOldPrice}
}
{this.state.recurrentPrice.toLocaleString() + ' ' + cartItem.country.currency}
}
); } } export default connect()(CartItem);