Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Render components based on props from template

Marcin Gębala970e13e3230b
diff --git a/package.json b/package.json
index 671b193ea2..c7b1e5fd0a 100644
--- a/package.json
+++ b/package.json
@@ -17,6 +17,7 @@
},
"dependencies": {
"bootstrap-sass": "^3.3.7",
+ "classnames": "^2.2.5",
"dropzone": "^4.2.0",
"jquery": "^2.1.4",
"jquery-match-height": "^0.7.0",
diff --git a/saleor/product/utils.py b/saleor/product/utils.py
index eb78fa7856..bcd12de5e9 100644
--- a/saleor/product/utils.py
+++ b/saleor/product/utils.py
@@ -3,7 +3,7 @@ from collections import namedtuple
from ..cart.decorators import get_cart_from_request
from ..core.utils import to_local_currency
from .forms import get_form_class_for_product
-from .models import Product, ProductAttribute
+from .models import Product
def products_visible_to_user(user):
@@ -92,8 +92,7 @@ def products_for_cart(user):
return products
-def get_variant_picker_data(variants):
- attributes = ProductAttribute.objects.prefetch_related('values')
+def get_variant_picker_data(variants, attributes):
data = {'attributes': [], 'variants': []}
for attribute in attributes:
data['attributes'].append({
diff --git a/saleor/product/views.py b/saleor/product/views.py
index 3b24df8c0b..56166cc944 100644
--- a/saleor/product/views.py
+++ b/saleor/product/views.py
@@ -63,7 +63,9 @@ def product_details(request, slug, product_id, form=None):
type(product).__name__.lower(),)
templates = [template_name, 'product/details.html']
product_images = get_product_images(product)
- variant_picker_data = get_variant_picker_data(product.variants.all())
+ variant_picker_data = get_variant_picker_data(
+ product.variants.all(),
+ product.product_class.variant_attributes.prefetch_related('values'))
return TemplateResponse(
request, templates,
{'is_visible': is_visible,
diff --git a/saleor/static/js/components/variantPicker/AttributeSelectionWidget.js b/saleor/static/js/components/variantPicker/AttributeSelectionWidget.js
index 43d34effb6..90e2b60781 100644
--- a/saleor/static/js/components/variantPicker/AttributeSelectionWidget.js
+++ b/saleor/static/js/components/variantPicker/AttributeSelectionWidget.js
@@ -4,16 +4,35 @@ import React, { Component, PropTypes } from 'react';
export default class AttributeSelectionWidget extends Component {
static propTypes = {
- name: PropTypes.string.isRequired
+ attribute: PropTypes.object.isRequired,
+ handleChange: PropTypes.func.isRequired
};
+ handleChange = (event) => {
+ const { name, value } = event.target;
+ this.props.handleChange(name, value);
+ }
+
render() {
- const { name } = this.props;
+ const { display, pk, values } = this.props.attribute;
return (
<div className="form-group">
- <label className="control-label">{name}</label>
- <div className="radio">
- <label className="radio-inline"><input type="radio" name="" value="" />Option</label>
+ <label className="control-label">{display}</label>
+ <div className="radio">
+ {values.map((value, i) => {
+ return (
+ <label className="radio-inline" key={i}>
+ <input
+ name={pk}
+ onChange={this.handleChange}
+ type="radio"
+ value={value.pk}
+ disabled={false}
+ />
+ {value.display}
+ </label>
+ );
+ })}
</div>
</div>
);
diff --git a/saleor/static/js/components/variantPicker/VariantPicker.js b/saleor/static/js/components/variantPicker/VariantPicker.js
index abc154eb17..56d18e0a91 100644
--- a/saleor/static/js/components/variantPicker/VariantPicker.js
+++ b/saleor/static/js/components/variantPicker/VariantPicker.js
@@ -1,23 +1,81 @@
-import React, { Component } from 'react';
+import _ from 'lodash';
+import classNames from 'classnames';
+import React, { Component, PropTypes } from 'react';
import AttributeSelectionWidget from './AttributeSelectionWidget';
export default class VariantPicker extends Component {
- submit = () => {}
+ static propTypes = {
+ attributes: PropTypes.array.isRequired,
+ variants: PropTypes.array.isRequired
+ }
+
+ constructor(props) {
+ super(props);
+ this.state = {
+ quantity: 1,
+ selection: {},
+ variant: null
+ };
+ }
+
+ handleAddToCart = () => {}
+
+ handleAttributeChange = (attrId, valueId) => {
+ this.setState({
+ selection: Object.assign(this.state.selection, { [attrId]: valueId })
+ }, () => {
+ let matchedVariant = null;
+ this.props.variants.forEach(variant => {
+ if (_.isEqual(this.state.selection, variant.attributes)) {
+ matchedVariant = variant;
+ }
+ });
+ this.setState({variant: matchedVariant});
+ });
+ }
+
+ handleQuantityChange = (event) => {
+ this.setState({quantity: event.target.value});
+ }
render() {
+ const { attributes } = this.props;
+ const { quantity, variant } = this.state;
+
+ const addToCartBtnClasses = classNames({
+ 'btn btn-lg btn-block btn-primary': true,
+ 'disabled': !variant
+ });
+
return (
<div>
- <AttributeSelectionWidget name="Size" />
- <AttributeSelectionWidget name="Color" />
+ {attributes.map((attribute, i) =>
+ <AttributeSelectionWidget
+ attribute={attribute}
+ handleChange={this.handleAttributeChange}
+ key={i}
+ />
+ )}
<div className="form-group">
<label className="control-label" htmlFor="id_quantity">Quantity</label>
- <input className="form-control" id="id_quantity" max="999" min="0" name="quantity" type="number" value="1" />
+ <input
+ className="form-control"
+ id="id_quantity"
+ max="999"
+ min="0"
+ name="quantity"
+ onChange={this.handleQuantityChange}
+ type="number"
+ value={quantity}
+ />
</div>
<div className="form-group">
- <button className="btn btn-lg btn-block btn-primary" onClick={this.submit}>
+ <button
+ className={addToCartBtnClasses}
+ onClick={this.handleAddToCart}>
Add to cart
</button>
</div>