Skip to content

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

Add filtering by price in UI

Marcin Gębalaf930865a14a1
diff --git a/saleor/static/js/components/CategoryPage.js b/saleor/static/js/components/CategoryPage.js
index 94726ca759..478fedcdeb 100644
--- a/saleor/static/js/components/CategoryPage.js
+++ b/saleor/static/js/components/CategoryPage.js
@@ -14,10 +14,19 @@ class CategoryPage extends Component {
relay: PropTypes.object
}
- onFilterChanged = (enabled) => {
+ onFilterChanged = (attributes) => {
+ if (attributes) {
+ this.props.relay.setVariables({
+ attributesFilter: attributes
+ });
+ }
+ }
+
+ onPriceFilterChanged = (minPrice, maxPrice) => {
this.props.relay.setVariables({
- attributesFilter: enabled
- })
+ minPrice: minPrice,
+ maxPrice: maxPrice
+ });
}
onLoadMore = () => {
@@ -37,6 +46,7 @@ class CategoryPage extends Component {
attributes={attributes}
categories={category}
onFilterChanged={this.onFilterChanged}
+ onPriceFilterChanged={this.onPriceFilterChanged}
/>
</div>
<div className="col-md-9">
@@ -55,7 +65,9 @@ class CategoryPage extends Component {
export default Relay.createContainer(CategoryPage, {
initialVariables: {
attributesFilter: [],
- count: PAGINATE_BY
+ count: PAGINATE_BY,
+ minPrice: null,
+ maxPrice: null
},
fragments: {
category: () => Relay.QL`
@@ -72,7 +84,7 @@ export default Relay.createContainer(CategoryPage, {
}
}
}
- products (first: $count, attributes: $attributesFilter) {
+ products (first: $count, attributes: $attributesFilter, priceGte: $minPrice, priceLte: $maxPrice) {
${ProductList.getFragment('products')}
}
}
diff --git a/saleor/static/js/components/PriceFilter.js b/saleor/static/js/components/PriceFilter.js
new file mode 100644
index 0000000000..cfa44e2a5f
--- /dev/null
+++ b/saleor/static/js/components/PriceFilter.js
@@ -0,0 +1,70 @@
+import React, { Component, PropTypes } from 'react';
+
+
+export default class PriceFilter extends Component {
+
+ static propTypes = {
+ onFilterChanged: PropTypes.func.isRequired
+ }
+
+ constructor(props) {
+ super(props);
+ this.state = {
+ minPrice: null,
+ maxPrice: null
+ }
+ }
+
+ checkKey = (event) => {
+ if (event.key === 'Enter') {
+ this.updateFilter()
+ }
+ };
+
+ onChange = (event) => {
+ const { name, value } = event.target;
+ this.setState({[name]: value})
+ }
+
+ parseValue = (value) => {
+ if (value) {
+ try {
+ return parseFloat(value);
+ } catch (error) {
+ return null;
+ }
+ }
+ return null;
+ }
+
+ updateFilter = () => {
+ let { minPrice, maxPrice } = this.state;
+ minPrice = this.parseValue(minPrice);
+ maxPrice = this.parseValue(maxPrice);
+ this.props.onFilterChanged(minPrice, maxPrice);
+ }
+
+ render() {
+ return (
+ <div>
+ <input
+ name="minPrice"
+ onChange={this.onChange}
+ onKeyUp={this.checkKey}
+ placeholder="min"
+ type="number"
+ value={this.state.minPrice}
+ />
+ <input
+ name="maxPrice"
+ onChange={this.onChange}
+ onKeyUp={this.checkKey}
+ placeholder="max"
+ type="number"
+ value={this.state.maxPrice}
+ />
+ <button onClick={this.updateFilter}>Update</button>
+ </div>
+ );
+ }
+}
diff --git a/saleor/static/js/components/ProductFilters.js b/saleor/static/js/components/ProductFilters.js
index 8e8b8aab4e..4bf33cd587 100644
--- a/saleor/static/js/components/ProductFilters.js
+++ b/saleor/static/js/components/ProductFilters.js
@@ -2,18 +2,22 @@ import React, { Component, PropTypes } from 'react'
import Relay from 'react-relay';
import queryString from 'query-string';
+import PriceFilter from './PriceFilter';
+
+
class ProductFilters extends Component {
static propTypes = {
attributes: PropTypes.array,
categories: PropTypes.object,
- onFilterChanged: PropTypes.func.isRequired
+ onFilterChanged: PropTypes.func.isRequired,
+ onPriceFilterChanged: PropTypes.func.isRequired
};
constructor(props) {
- super(props);
- this.state = {
- filters: {}
+ super(props);
+ this.state = {
+ filters: {}
};
}
@@ -88,6 +92,10 @@ class ProductFilters extends Component {
}
+ onPriceFilterChanged = (minPrice, maxPrice) => {
+ this.props.onPriceFilterChanged(minPrice, maxPrice)
+ }
+
render() {
const categoryName = this.props.categories.name;
const subCategories = this.props.categories.children.edges;
@@ -124,8 +132,9 @@ class ProductFilters extends Component {
</ul>
</div>
)
- })
- )}
+ }))}
+ <h3>Price range</h3>
+ <PriceFilter onFilterChanged={this.onPriceFilterChanged} />
</div>
)
}
@@ -136,7 +145,7 @@ export default Relay.createContainer(ProductFilters, {
attributes: () => Relay.QL`
fragment on ProductAttributeType @relay(plural: true) {
id
- pk
+ pk
name
display
values {