Skip to content

Commit d031fe1

Browse files
committed
Added total_count to response
1 parent b6b68a9 commit d031fe1

6 files changed

Lines changed: 35 additions & 8 deletions

File tree

src/api/server/services/products/products.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,24 @@ class ProductsService {
3838
aggregationPipeline.push({ $skip : offset });
3939
aggregationPipeline.push({ $limit : limit });
4040

41+
// get total records count
42+
const aggregationCount = [];
43+
if(matchTextQuery) {
44+
aggregationCount.push({ $match: matchTextQuery });
45+
}
46+
aggregationCount.push({ $match: matchQuery });
47+
aggregationCount.push({$group: {_id: null, count: { $sum: 1 }}});
48+
4149
return mongo.db.collection('products').aggregate(aggregationPipeline).toArray()
42-
.then(items => items.map(item => this.changeProperties(categories, item, thumbnail_width)))
50+
.then(items => items.map(item => this.changeProperties(categories, item, thumbnail_width))).then(items => {
51+
return mongo.db.collection('products').aggregate(aggregationCount).toArray().then(countItems => {
52+
return {
53+
total_count: countItems[0].count,
54+
has_more: (offset + items.length) < countItems[0].count,
55+
data: items
56+
}
57+
})
58+
})
4359
});
4460
}
4561

@@ -299,7 +315,7 @@ class ProductsService {
299315
return Promise.reject('Invalid identifier');
300316
}
301317
return this.getProducts({ ids: id, limit: 1})
302-
.then(items => items.length > 0 ? items[0] : {})
318+
.then(products => products.data.length > 0 ? products.data[0] : {})
303319
}
304320

305321
addProduct(data) {

src/store/shared/actions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ export const getInitialState = (req, checkoutFields, currentPage, settings) => {
319319
price_from: null,
320320
price_to: null,
321321
limit: 30,
322-
sort: 'price,stock_status',
322+
sort: settings.default_product_sorting,
323323
fields: 'path,id,name,category_id,category_name,sku,images,enabled,discontinued,stock_status,stock_quantity,price,on_sale,regular_price'
324324
},
325325
cart: null,

themes/current/config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66
"big_thumbnail_width": 800,
77
"checkout_input_class": "checkout-field",
88
"checkout_button_class": "checkout-button button is-primary",
9-
"imagePlaceholder": "/assets/images/placeholder.svg"
9+
"imagePlaceholder": "/assets/images/placeholder.svg",
10+
"sort_newest": "-date_created",
11+
"sort_price_low": "price",
12+
"sort_price_high": "-price"
1013
}

themes/current/locales/en.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
"relatedProducts": "Related products",
2121
"filterProducts": "Filter products",
2222
"sort": "Sort",
23+
"sortFavorite":"Favorite",
24+
"sortNewest":"Newest",
25+
"sortPriceLow":"Price low to high",
26+
"sortPriceHigh":"Price high to low",
2327
"title404": "Page not found",
2428
"text404": "The page you requested does not exist. Click here to continue shopping.",
2529
"footerCopyright": "Copyright © 2017 Demo Drone Store. All Rights Reserved. EN. "

themes/current/src/components/productsSort.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import React from 'react'
22
import text from '../lib/text'
3+
import config from '../lib/config'
34

45
const Sort = ({ defaultSort, currentSort, setSort }) => {
56
return (
67
<div className="columns">
78
<div className="column is-3">{text.sort}:</div>
89
<div className="column">
910
<span className="select is-fullwidth">
10-
<select>
11-
<option>Not implemented</option>
11+
<select onChange={e => {setSort(e.target.value)}} value={currentSort}>
12+
<option value={defaultSort}>{text.sortFavorite}</option>
13+
<option value={config.sort_newest}>{text.sortNewest}</option>
14+
<option value={config.sort_price_low}>{text.sortPriceLow}</option>
15+
<option value={config.sort_price_high}>{text.sortPriceHigh}</option>
1216
</select>
1317
</span>
1418
</div>

themes/current/src/containers/category.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ProductsSort from '../components/productsSort'
77
import Waypoint from 'react-waypoint'
88

99
const CategoryContainer = (props) => {
10-
const {products, categoryDetails, settings} = props.state;
10+
const {products, categoryDetails, settings, productsFilter} = props.state;
1111

1212
return (
1313
<div>
@@ -48,7 +48,7 @@ const CategoryContainer = (props) => {
4848
<div className="columns">
4949
<div className="column"></div>
5050
<div className="column is-4">
51-
<ProductsSort />
51+
<ProductsSort defaultSort={settings.default_product_sorting} currentSort={productsFilter.sort} setSort={props.setSort} />
5252
</div>
5353
</div>
5454
</MediaQuery>

0 commit comments

Comments
 (0)