created cart icon with number of items notification - it's still buggy

This commit is contained in:
Senad Uka
2015-02-08 15:17:35 +01:00
parent 113b70e8fa
commit b25683abca
9 changed files with 114 additions and 20 deletions

View File

@@ -17,7 +17,6 @@ var AddToCart = React.createClass({
<div className="col-xs-offset-1 col-xs-1"><button className="btn btn-success" onClick={this._onIncreaseClick}>+</button></div>
<div className="col-xs-2"><button className="btn" style={countStyle} > { this.state.item.get('count') } </button></div>
<div className="col-xs-1"><button className="btn btn-success" onClick={this._onDecreaseClick}>-</button></div>
<div className="col-xs-7"><button className="btn btn-warning">U korpu</button></div>
</div>
);
},
@@ -25,14 +24,12 @@ var AddToCart = React.createClass({
// Add change listeners to stores
componentDidMount: function() {
CartStore.addChangeListener(this._onChange);
CartActions.load();
},
getInitialState: function() {
var itemInCart = CartStore.getStateFor(this.props.itemId);
var itemInCart = CartStore.getStateFor(this.props.itemId);
return { item: itemInCart }
},
@@ -50,7 +47,12 @@ var AddToCart = React.createClass({
_onDecreaseClick: function () {
CartActions.takeItemOut(this.props.itemId);
},
componentWillUnmount: function () {
CartStore.removeChangeListener(this._onChange);
}
});
module.exports = AddToCart;

View File

@@ -0,0 +1,49 @@
var React = require('react');
var CartStore = require('../../stores/cartStore.js');
var CartActions = require('../../actions/cartActions.js');
var cartStyle = {
fontSize: '50px'
};
var CartIcon = React.createClass({
render: function() {
var textNotificationStyle = (this.state.count > 0) ? { display: 'inline-block'} : { display: 'none'} ;
return (
<div className="shopping-cart-icon">
<i style={cartStyle} className="fa fa-shopping-cart"></i>
<div style={textNotificationStyle} className="shopping-cart-notification-text">{this.state.count}</div>
</div>
);
},
// Add change listeners to stores
componentDidMount: function() {
CartStore.addChangeListener(this._onChange);
CartActions.load();
},
getInitialState: function() {
var cartState = CartStore.getWholeCartState();
return cartState;
},
_onChange: function () {
if (this.isMounted()) {
this.setState(CartStore.getWholeCartState());
}
}
});
module.exports = CartIcon;