Created
November 6, 2016 06:15
-
-
Save petyappetrov/db05685b6385e35e486a01371923df56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
// | |
// Copyright (C) 2008-2016 Quantron Systems LLC. | |
// All Rights Reserved. | |
// | |
// This file is part of the Pakmil project. | |
// For conditions of distribution and use, | |
// please contact sales@quantron-systems.com | |
// | |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
import React from 'react'; | |
import bindStore from '../../stores/bindStores'; | |
import {getProductPrice} from 'components/common'; | |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
const SetProductLogic = ComposedComponent => React.createClass({ | |
getInitialState() { | |
return { | |
counter: 1, | |
counterWasInitialized: false, | |
clickedBuy: false | |
}; | |
}, | |
componentWillReceiveProps(props) { | |
if(this.state.counterWasInitialized) return; | |
const {basket, product} = props; | |
if(basket && product) this.setState({ | |
counterWasInitialized: true, | |
counter: basket[product._id] || this.state.counter | |
}); | |
}, | |
increment(event) { | |
event.preventDefault(); | |
this.setCounter(this.state.counter + 1); | |
}, | |
decrement(event) { | |
event.preventDefault(); | |
this.setCounter(this.state.counter - 1); | |
}, | |
setCounter(counter) { | |
const {product, stores, forceCounterUpdate} = this.props; | |
if(counter < 1) { | |
counter = 1; | |
} else if(counter > 99999) { | |
counter = 99999; | |
} | |
if(this.state.clickedBuy || forceCounterUpdate) { | |
stores.basket.setProduct(product._id, counter); | |
} | |
this.setState({counter}); | |
}, | |
blurEnterCounter() { | |
if(this.state.counter.length == 0 || this.state.counter == 0) { | |
this.setCounter(1); | |
} | |
}, | |
getPrice() { | |
return getProductPrice(this.props.product, this.state.counter); | |
}, | |
onClickBuy(event) { | |
event.preventDefault(); | |
const {product, stores} = this.props; | |
stores.basket.setProduct(product._id, this.state.counter); | |
this.setState({ | |
clickedBuy: true | |
}); | |
}, | |
onClickContinueShopping(event) { | |
event.preventDefault(); | |
this.setState({ | |
clickedBuy: false | |
}); | |
}, | |
onClickRemove(event) { | |
event.preventDefault(); | |
const {stores, product} = this.props; | |
stores.basket.removeProduct(product._id); | |
}, | |
render() { | |
if(!this.props.product) { | |
return null; | |
} | |
const {clickedBuy, counter} = this.state; | |
const sum = Math.round((counter * this.getPrice()) * 10) / 10; | |
const productLogic = { | |
clickedBuy, | |
counterProps: { | |
counter, | |
decrement: this.decrement, | |
increment: this.increment, | |
handleOnEnterCounter: this.setCounter, | |
blurEnterCounter: this.blurEnterCounter, | |
sum: parseInt(sum) | |
}, | |
onClickBuy: this.onClickBuy, | |
onClickRemove: this.onClickRemove, | |
onClickContinueShopping: this.onClickContinueShopping | |
}; | |
return ( | |
<ComposedComponent | |
{...this.props} | |
productLogic={productLogic} | |
activePrice={getProductPrice(this.props.product, this.state.counter)} | |
/> | |
); | |
} | |
}); | |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
export default SetProductLogicComponent => | |
bindStore({ | |
type: 'basket', | |
propName: 'basket' | |
}, SetProductLogic(SetProductLogicComponent)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment