Skip to content

Instantly share code, notes, and snippets.

@saurshaz
Created December 1, 2015 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saurshaz/bd71897f00f44a32746b to your computer and use it in GitHub Desktop.
Save saurshaz/bd71897f00f44a32746b to your computer and use it in GitHub Desktop.
react code for product listinhg (incomplete without other components)
var React = require('react/addons');
var { PureRenderMixin } = React.addons;
var Fluxxor = require('fluxxor');
var StoreWatchMixin = Fluxxor.StoreWatchMixin;
var Utils = require ('../../Utils');
var EC = require('../../EventConstants');
const SubItem = require('./SubItem');
var ProductListView = React.createClass({
mixins: [
Fluxxor.FluxMixin(React),
Fluxxor.StoreWatchMixin("product"),
],
propTypes: {
product: React.PropTypes.object.isRequired,
restaurant_id: React.PropTypes.string.isRequired,
},
/*========== lifecycle ==========*/
getStateFromFlux() {
return {
productData: this.getFlux().store("product"),
added:false,
showOptions: false,
};
},
addToCart: function(e) {
this.setState({added:true});
this.getFlux().actions.product.addToCart({"ADDED":
{
restaurant: this.props.restaurant_id,
type: this.props.product._id,
item: this.props.product._id,
name: this.props.product.name,
code: this.props.product.sku,
price: this.props.product.price,
discount: this.props.product.discount,
quantity: 1,
}
});
// this.emit("change");
},
removeFromCart: function(e) {
this.setState({added:false});
this.getFlux().actions.product.deleteFromCart({"REMOVED":
{
code: this.props.product.sku,
quantity: 1,
}
});
// this.emit("change");
},
toggleOptionsStyle: function(e){
this.setState({showOptions:!this.state.showOptions});
},
render() {
let removeButton = (<button className="btn btn-primary" data-productname={this.props.product.name} data-categoryname={this.props.product.category} onClick={this.removeFromCart}>Remove</button>)
let addButton = (<button className="btn btn-primary" data-productname={this.props.product.name} data-categoryname={this.props.product.category} onClick={this.addToCart}>Add To Cart</button>)
this.product_image = this.props.product.image ?
'<img className="product__photo bottom-shade-border" src="../assets/products/"'+ this.props.product.image + 'alt="product image"/>' :
'';
let productSection = (<div className="product__item rounded-bottom-corners rounded-top-corners bottom-shade-border">
<div className="thumbnail">
{this.product_image}
<div className="caption clearfix">
<div className="product__name-wrapper">
<h3>{this.props.product.name}</h3>
<h3>{this.props.product.name_cn}</h3>
</div>
<div className="product__description">
{this.props.product.description}
</div>
<div className="product__price">
<span> $</span>
<span>{this.props.product.price}</span>
</div>
<div className="product__price-discounted">${this.props.product.discount}</div>
<div className="product__button-wrap">
{(this.state.added === true) ? removeButton : addButton}
</div>
</div>
</div>
</div>);
let optionsButtonSection = '';
if(this.props.product.options && (this.props.product.options.length > 0)){
optionsButtonSection = (<button onClick={this.toggleOptionsStyle}> {(!this.state.showOptions) ? 'Show options' : 'Hide Options'} </button>);
};
return (
<span>
<div className="col-md-4" data-productname={this.props.product.name} data-categoryname={this.props.product.category}>
<div id={this.props.product.name} className="product__category rounded-top-corners">
{this.props.product.category}
</div>
{productSection}
<SubItem subitems={this.props.product.options || []} showOptions={this.state.showOptions} />
{optionsButtonSection}
</div>
</span>
);
},
});
module.exports = ProductListView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment