Skip to content

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

Update the products query on filters' state changes

Marcin Gębala8a471f625b86
diff --git a/saleor/static/js/components/CategoryPage.js b/saleor/static/js/components/CategoryPage.js
index e073a8afe6..be013ca64b 100644
--- a/saleor/static/js/components/CategoryPage.js
+++ b/saleor/static/js/components/CategoryPage.js
@@ -7,23 +7,33 @@ import ProductFilters from './ProductFilters'
class CategoryPage extends Component {
static propTypes = {
+ attributes: PropTypes.array,
category: PropTypes.object,
- attributes: PropTypes.array
+ relay: PropTypes.object
}
+ onFilterChanged = (enabled) => {
+ this.props.relay.setVariables({
+ attributesFilter: enabled
+ })
+ }
+
render() {
const category = this.props.category;
const attributes = this.props.attributes;
- const products = this.props.category.products;
return (
<div className="row">
<div className="col-md-3">
- <ProductFilters categories={category} attributes={attributes} />
+ <ProductFilters
+ attributes={attributes}
+ categories={category}
+ onFilterChanged={this.onFilterChanged}
+ />
</div>
<div className="col-md-9">
<div className="row">
- <ProductList products={products} />
+ <ProductList products={category.products} />
</div>
</div>
</div>
@@ -32,21 +42,25 @@ class CategoryPage extends Component {
}
export default Relay.createContainer(CategoryPage, {
+ initialVariables: {
+ attributesFilter: []
+ },
fragments: {
category: () => Relay.QL`
fragment on CategoryType {
- id,
- name,
+ id
+ name
+ url
children(first: 20) {
edges {
node {
- id,
- name,
- slug
+ id
+ name
+ url
}
}
}
- products (first: 20) {
+ products (first: 20, attributes: $attributesFilter) {
${ProductList.getFragment('products')}
}
}
diff --git a/saleor/static/js/components/ProductFilters.js b/saleor/static/js/components/ProductFilters.js
index 66d007d6da..851cf0effa 100644
--- a/saleor/static/js/components/ProductFilters.js
+++ b/saleor/static/js/components/ProductFilters.js
@@ -5,12 +5,30 @@ import Relay from 'react-relay';
class ProductFilters extends Component {
static propTypes = {
+ attributes: PropTypes.array,
categories: PropTypes.object,
- attributes: PropTypes.array
+ onFilterChanged: PropTypes.func.isRequired
};
- render() {
+ constructor(props) {
+ super(props);
+ this.state = {
+ filters: {}
+ };
+ }
+
+ onClick = (attribute, value) => {
+ const attrValue = `${attribute}:${value}`;
+ this.setState({
+ filters: Object.assign(
+ this.state.filters,
+ {[attrValue]: !this.state.filters[attrValue]})
+ });
+ const enabled = Object.keys(this.state.filters).filter(key => this.state.filters[key] === true);
+ this.props.onFilterChanged(enabled);
+ }
+ render() {
const categoryName = this.props.categories.name;
const subCategories = this.props.categories.children.edges;
const { attributes } = this.props;
@@ -20,15 +38,15 @@ class ProductFilters extends Component {
<h2>Filters:</h2>
<ul className="categories list-group">
<li className="list-group-item active">{categoryName}</li>
- {subCategories ? (subCategories.map((item) => {
+ {subCategories && (subCategories.map((item) => {
const category = item.node;
return (
- <li key={category.id} className="list-group-item"><a href={category.name}>{category.name}</a></li>
+ <li key={category.id} className="list-group-item"><a href={category.url}>{category.name}</a></li>
);
})
- ) : (null)}
+ )}
</ul>
- {attributes ? (attributes.map((attribute) => {
+ {attributes && (attributes.map((attribute) => {
return (
<div key={attribute.id} className="attribute">
<ul className={attribute.name}>
@@ -38,17 +56,16 @@ class ProductFilters extends Component {
backgroundColor: value.color
}
return (
- <li key={value.id} className="item" style={colorStyle}>
+ <button key={value.id} className="item" style={colorStyle} onClick={() => this.onClick(attribute.pk, value.pk)}>
{value.display}
- </li>
+ </button>
)
})}
</ul>
</div>
)
})
- ) : (null)}
-
+ )}
</div>
)
}
@@ -59,10 +76,12 @@ export default Relay.createContainer(ProductFilters, {
attributes: () => Relay.QL`
fragment on ProductAttributeType @relay(plural: true) {
id
+ pk
name
display
values {
id
+ pk
display
color
}