Skip to content

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

Refactor filter components; use normal list for returning categories from API

Marcin Gębalaf23883f4e012
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index 6a628de524..249bdaac1b 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -51,7 +51,7 @@ ProductType.Connection = connection_with_count(ProductType)
class CategoryType(DjangoObjectType):
- url = graphene.String()
+ children = graphene.List(lambda: CategoryType)
products = relay.ConnectionField(
ProductType,
attributes=graphene.Argument(
@@ -68,14 +68,15 @@ class CategoryType(DjangoObjectType):
price_gte=graphene.Argument(
graphene.Float, description="""Get the products with price greater
than or equal to the given value"""))
+ products_count = graphene.Int()
+ url = graphene.String()
class Meta:
model = Category
interfaces = (relay.Node, DjangoPkInterface)
- @graphene.resolve_only_args
- def resolve_url(self):
- return self.get_absolute_url()
+ def resolve_children(self, args, context, info):
+ return self.children.all()
def resolve_products(self, args, context, info):
qs = self.products.prefetch_for_api()
@@ -114,6 +115,13 @@ class CategoryType(DjangoObjectType):
qs = qs.filter(price__gte=price_gte)
return qs.distinct()
+ def resolve_products_count(self, args, context, info):
+ return self.products.count()
+
+ @graphene.resolve_only_args
+ def resolve_url(self):
+ return self.get_absolute_url()
+
class ProductVariantType(DjangoObjectType):
stock_quantity = graphene.Int()
@@ -172,7 +180,7 @@ class Viewer(graphene.ObjectType):
product = graphene.Field(
ProductType, pk=graphene.Argument(graphene.Int, required=True))
attributes = graphene.List(ProductAttributeType)
- categories = relay.ConnectionField(CategoryType)
+ categories = graphene.List(CategoryType)
def resolve_category(self, args, context, info):
return get_object_or_none(Category, pk=args.get('pk'))
diff --git a/saleor/static/js/components/CategoryFilter.js b/saleor/static/js/components/CategoryFilter.js
new file mode 100644
index 0000000000..8e98d2b688
--- /dev/null
+++ b/saleor/static/js/components/CategoryFilter.js
@@ -0,0 +1,24 @@
+import React, { Component, PropTypes } from 'react'
+
+
+export default class CategoryFilter extends Component {
+
+ static propTypes = {
+ category: PropTypes.object.isRequired
+ }
+
+ render() {
+ const { category } = this.props;
+ return (
+ <ul className="categories list-group">
+ <li className="list-group-item active">{category.name}</li>
+ {category.children && (category.children.map((subcategory) => {
+ return (
+ <li key={subcategory.pk} className="list-group-item"><a href={subcategory.url}>{subcategory.name}</a></li>
+ );
+ })
+ )}
+ </ul>
+ );
+ }
+}
diff --git a/saleor/static/js/components/CategoryPage.js b/saleor/static/js/components/CategoryPage.js
index 8d0b52d051..bfabee8a95 100644
--- a/saleor/static/js/components/CategoryPage.js
+++ b/saleor/static/js/components/CategoryPage.js
@@ -1,7 +1,10 @@
-import React, { Component, PropTypes } from 'react'
-import Relay from 'react-relay'
-import ProductList from './ProductList'
-import ProductFilters from './ProductFilters'
+import React, { Component, PropTypes } from 'react';
+import Relay from 'react-relay';
+
+import CategoryFilter from './CategoryFilter';
+import PriceFilter from './PriceFilter';
+import ProductFilters from './ProductFilters';
+import ProductList from './ProductList';
const PAGINATE_BY = 20;
@@ -14,45 +17,49 @@ class CategoryPage extends Component {
relay: PropTypes.object
}
- onFilterChanged = (attributes) => {
- if (attributes) {
- this.props.relay.setVariables({
- attributesFilter: attributes
- });
- }
+ loadMore = () => {
+ this.props.relay.setVariables({
+ count: this.props.relay.variables.count + PAGINATE_BY
+ })
}
- onPriceFilterChanged = (minPrice, maxPrice) => {
+ setAttributesFilter = (attributes) => {
this.props.relay.setVariables({
- minPrice: minPrice,
- maxPrice: maxPrice
+ attributesFilter: attributes
});
}
- onLoadMore = () => {
+ setPriceFilter = (minPrice, maxPrice) => {
this.props.relay.setVariables({
- count: this.props.relay.variables.count + PAGINATE_BY
- })
+ minPrice: minPrice,
+ maxPrice: maxPrice
+ });
}
render() {
const category = this.props.category;
const attributes = this.props.attributes;
-
return (
<div className="row">
<div className="col-md-3">
- <ProductFilters
- attributes={attributes}
- categories={category}
- onFilterChanged={this.onFilterChanged}
- onPriceFilterChanged={this.onPriceFilterChanged}
- />
+ <h2>Filters:</h2>
+ <div className="product-filters">
+ <CategoryFilter
+ category={category}
+ />
+ <ProductFilters
+ attributes={attributes}
+ onFilterChanged={this.setAttributesFilter}
+ />
+ <PriceFilter
+ onFilterChanged={this.setPriceFilter}
+ />
+ </div>
</div>
<div className="col-md-9">
<div className="row">
<ProductList
- onLoadMore={this.onLoadMore}
+ onLoadMore={this.loadMore}
products={category.products}
/>
</div>
@@ -72,17 +79,13 @@ export default Relay.createContainer(CategoryPage, {
fragments: {
category: () => Relay.QL`
fragment on CategoryType {
- id
+ pk
name
url
- children(first: 20) {
- edges {
- node {
- id
- name
- url
- }
- }
+ children {
+ pk
+ name
+ url
}
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
index 6aedb47cdd..325e1c96e8 100644
--- a/saleor/static/js/components/PriceFilter.js
+++ b/saleor/static/js/components/PriceFilter.js
@@ -47,28 +47,29 @@ export default class PriceFilter extends Component {
render() {
return (
<div className="price-range">
- <input
- id="minPrice"
- className="form-control"
- name="minPrice"
- onChange={this.onChange}
- onKeyUp={this.checkKey}
- placeholder="min"
- type="number"
- value={this.state.minPrice}
- />
- <span>&#8212;</span>
- <input
- id="maxPrice"
- name="maxPrice"
- className="form-control"
- onChange={this.onChange}
- onKeyUp={this.checkKey}
- placeholder="max"
- type="number"
- value={this.state.maxPrice}
- />
- <button className="btn" onClick={this.updateFilter}>Update</button>
+ <h3>Price range</h3>
+ <input
+ id="minPrice"
+ className="form-control"
+ name="minPrice"
+ onChange={this.onChange}
+ onKeyUp={this.checkKey}
+ placeholder="min"
+ type="number"
+ value={this.state.minPrice}
+ />
+ <span>&#8212;</span>
+ <input
+ id="maxPrice"
+ name="maxPrice"
+ className="form-control"
+ onChange={this.onChange}
+ onKeyUp={this.checkKey}
+ placeholder="max"
+ type="number"
+ value={this.state.maxPrice}
+ />
+ <button className="btn" onClick={this.updateFilter}>Update</button>
</div>
);
}
diff --git a/saleor/static/js/components/ProductFilters.js b/saleor/static/js/components/ProductFilters.js
index aa777441e0..a5aae9b066 100644
--- a/saleor/static/js/components/ProductFilters.js
+++ b/saleor/static/js/components/ProductFilters.js
@@ -2,16 +2,12 @@ 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,
- onPriceFilterChanged: PropTypes.func.isRequired
};
constructor(props) {
@@ -87,28 +83,10 @@ class ProductFilters extends Component {
this.props.onFilterChanged(enabled);
}
- onPriceFilterChanged = (minPrice, maxPrice) => {
- this.props.onPriceFilterChanged(minPrice, maxPrice)
- }
-
render() {
- const categoryName = this.props.categories.name;
- const subCategories = this.props.categories.children.edges;
const { attributes } = this.props;
-
return (
- <div className="product-filters">
- <h2>Filters:</h2>
- <ul className="categories list-group">
- <li className="list-group-item active">{categoryName}</li>
- {subCategories && (subCategories.map((item) => {
- const category = item.node;
- return (
- <li key={category.id} className="list-group-item"><a href={category.url}>{category.name}</a></li>
- );
- })
- )}
- </ul>
+ <div>
{attributes && (attributes.map((attribute) => {
return (
<div key={attribute.id} className="attribute">
@@ -134,8 +112,6 @@ class ProductFilters extends Component {
</div>
)
}))}
- <h3>Price range</h3>
- <PriceFilter onFilterChanged={this.onPriceFilterChanged} />
</div>
)
}