Skip to content

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

Add simple ordering to product list

Krzysztof Białogłowiczb0d5758ddb4a
diff --git a/saleor/static/js/components/CategoryPage.js b/saleor/static/js/components/CategoryPage.js
index 2d96245468..d6263de92f 100644
--- a/saleor/static/js/components/CategoryPage.js
+++ b/saleor/static/js/components/CategoryPage.js
@@ -59,6 +59,12 @@ class CategoryPage extends Component {
});
}
+ orderBy = (event) => {
+ this.props.relay.setVariables({
+ orderBy: event.target.className
+ });
+ }
+
updateAttributesFilter = (key) => {
// Create a new attributesFilter array by cloning the current one to make
// Relay refetch products with new attributes. Passing the same array (even
@@ -82,7 +88,7 @@ class CategoryPage extends Component {
}
persistStateInUrl() {
- const { attributesFilter, count, maxPrice, minPrice } = this.props.relay.variables;
+ const { attributesFilter, count, maxPrice, minPrice, orderBy } = this.props.relay.variables;
let urlParams = {};
if (minPrice) {
urlParams['minPrice'] = minPrice;
@@ -93,6 +99,9 @@ class CategoryPage extends Component {
if (count > PAGINATE_BY) {
urlParams['count'] = count;
}
+ if (orderBy) {
+ urlParams['orderBy'] = orderBy;
+ }
attributesFilter.forEach(filter => {
const [ attributeName, valueSlug ] = filter.split(':');
if (attributeName in urlParams) {
@@ -143,6 +152,7 @@ class CategoryPage extends Component {
<ProductList
onLoadMore={this.incrementProductsCount}
products={category.products}
+ orderBy={this.orderBy}
/>
</div>
</div>
@@ -154,10 +164,11 @@ class CategoryPage extends Component {
export default Relay.createContainer(CategoryPage, {
initialVariables: {
- attributesFilter: getAttributesFromQueryString(['count', 'minPrice', 'maxPrice']),
+ attributesFilter: getAttributesFromQueryString(['count', 'minPrice', 'maxPrice', 'orderBy']),
count: floatOrNull(getVarFromQueryString('count', PAGINATE_BY)),
minPrice: floatOrNull(getVarFromQueryString('minPrice')),
- maxPrice: floatOrNull(getVarFromQueryString('maxPrice'))
+ maxPrice: floatOrNull(getVarFromQueryString('maxPrice')),
+ orderBy: getVarFromQueryString('orderBy')
},
fragments: {
category: () => Relay.QL`
@@ -185,7 +196,7 @@ export default Relay.createContainer(CategoryPage, {
slug
}
}
- products (first: $count, attributes: $attributesFilter, priceGte: $minPrice, priceLte: $maxPrice) {
+ products (first: $count, attributes: $attributesFilter, priceGte: $minPrice, priceLte: $maxPrice, orderBy: $orderBy) {
${ProductList.getFragment('products')}
}
}
diff --git a/saleor/static/js/components/ProductList.js b/saleor/static/js/components/ProductList.js
index b87ea411ca..9f708b7f83 100644
--- a/saleor/static/js/components/ProductList.js
+++ b/saleor/static/js/components/ProductList.js
@@ -8,15 +8,53 @@ class ProductList extends Component {
static propTypes = {
onLoadMore: PropTypes.func.isRequired,
+ orderBy: PropTypes.func,
products: PropTypes.object
};
onLoadMore = () => this.props.onLoadMore();
+ orderBy = (event) => this.props.orderBy(event);
render() {
const { edges, pageInfo: { hasNextPage } } = this.props.products;
return (
<div>
+ <div className="sort-by">
+ <button className="btn btn-link">
+ <span>Sort by: <strong>Price</strong></span>
+ <span className="caret">+</span>
+ </button>
+ <ul className="sort-list">
+ <li className="name">
+ <div className="row">
+ <div className="col-md-6">Name:</div>
+ <div className="col-md-6">
+ <span className="name" onClick={this.orderBy}>ascending</span>
+ </div>
+ </div>
+ <div className="row">
+ <div className="col-md-6"></div>
+ <div className="col-md-6">
+ <span className="-name" onClick={this.orderBy}>descending</span>
+ </div>
+ </div>
+ </li>
+ <li className="price">
+ <div className="row">
+ <div className="col-md-6">Price:</div>
+ <div className="col-md-6">
+ <span className="price" onClick={this.orderBy}>ascending</span>
+ </div>
+ </div>
+ <div className="row">
+ <div className="col-md-6"></div>
+ <div className="col-md-6">
+ <span className="-price" onClick={this.orderBy}>descending</span>
+ </div>
+ </div>
+ </li>
+ </ul>
+ </div>
<div>
{edges && (edges.map((edge, i) => (
<ProductItem key={i} product={edge.node} />
diff --git a/saleor/static/scss/demo/components/_category-page.scss b/saleor/static/scss/demo/components/_category-page.scss
index 945384de8a..63b4be1f2a 100644
--- a/saleor/static/scss/demo/components/_category-page.scss
+++ b/saleor/static/scss/demo/components/_category-page.scss
@@ -59,6 +59,35 @@
}
}
+ .sort-by {
+ text-align: right;
+ position: relative;
+ ul {
+ list-style: none;
+ border: solid 1px $gray;
+ border-radius: 0.5rem;
+ background: $white;
+ position: absolute;
+ top: 2rem;
+ right: 0;
+ z-index: 999;
+ padding-left: $global-padding/2;
+ padding-bottom: $global-padding/2;
+ li {
+ width: 15rem;
+ text-align: left;
+ padding-top: $global-padding/2;
+ overflow: hidden;
+ span {
+ &:hover {
+ cursor: pointer;
+ text-decoration: underline;
+ }
+ }
+ }
+ }
+ }
+
.load-more {
clear: both;
button {