From b25683abca5a71616d1506e1640d7f06f20d44ab Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Sun, 8 Feb 2015 15:17:35 +0100 Subject: [PATCH] created cart icon with number of items notification - it's still buggy --- front-ui/app/actions/cartActions.js | 7 ++++ front-ui/app/components/cart/addToCart.js | 10 +++-- front-ui/app/components/cart/cartIcon.js | 49 +++++++++++++++++++++++ front-ui/app/components/rootApp.js | 6 ++- front-ui/app/constants/cartConstants.js | 3 +- front-ui/app/css/cart.css | 17 ++++++++ front-ui/app/css/main.css | 5 +++ front-ui/app/stores/cartStore.js | 36 +++++++++++------ front-ui/build/index.html | 1 + 9 files changed, 114 insertions(+), 20 deletions(-) create mode 100644 front-ui/app/components/cart/cartIcon.js diff --git a/front-ui/app/actions/cartActions.js b/front-ui/app/actions/cartActions.js index 7b6e62a..8a16fd3 100644 --- a/front-ui/app/actions/cartActions.js +++ b/front-ui/app/actions/cartActions.js @@ -28,6 +28,13 @@ var CartActions = { AppDispatcher.handleAction({ actionType: CartConstants.CART_DATA_LOADED }); + }, + + saveCartStateForItem: function(itemId) { + AppDispatcher.handleAction({ + actionType: CartConstants.SAVE_CART_STATE_FOR_ITEM, + itemId: itemId + }); } }; diff --git a/front-ui/app/components/cart/addToCart.js b/front-ui/app/components/cart/addToCart.js index 6cd2c83..ad4b84b 100644 --- a/front-ui/app/components/cart/addToCart.js +++ b/front-ui/app/components/cart/addToCart.js @@ -17,7 +17,6 @@ var AddToCart = React.createClass({
-
); }, @@ -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; diff --git a/front-ui/app/components/cart/cartIcon.js b/front-ui/app/components/cart/cartIcon.js new file mode 100644 index 0000000..cb02be9 --- /dev/null +++ b/front-ui/app/components/cart/cartIcon.js @@ -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 ( +
+ +
{this.state.count}
+
+ ); + }, + + // 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; + + + diff --git a/front-ui/app/components/rootApp.js b/front-ui/app/components/rootApp.js index 55a9425..f317e39 100644 --- a/front-ui/app/components/rootApp.js +++ b/front-ui/app/components/rootApp.js @@ -3,6 +3,8 @@ var React = require('react'), Router = require('react-router'), RouteHandler = Router.RouteHandler; +var CartIcon = require('./cart/cartIcon'); + var RootApp = React.createClass({ render: function() { @@ -11,8 +13,8 @@ var RootApp = React.createClass({
-
-

Ribica.ba

+
+

Ribica.ba