Skip to content

Commit 6c2e21b

Browse files
committed
Changed to arrow function
1 parent e6c137c commit 6c2e21b

9 files changed

Lines changed: 101 additions & 25 deletions

File tree

src/admin/client/modules/products/actions.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,17 @@ export function fetchProducts() {
194194

195195
return api.products.list(filter)
196196
.then(({status, json}) => {
197-
json = json.sort((a,b) => (a.position - b.position ));
197+
let products = json.data;
198198

199-
json.forEach((element, index, theArray) => {
199+
products = product.sort((a,b) => (a.position - b.position ));
200+
201+
products.forEach((element, index, theArray) => {
200202
if(theArray[index].name === '') {
201203
theArray[index].name = `<${messages.draft}>`;
202204
}
203205
})
204206

205-
dispatch(receiveProducts(json))
207+
dispatch(receiveProducts(products))
206208
})
207209
.catch(error => {
208210
dispatch(receiveProductsError(error));
@@ -242,15 +244,17 @@ export function fetchMoreProducts() {
242244

243245
return api.products.list(filter)
244246
.then(({status, json}) => {
245-
json = json.sort((a,b) => (a.position - b.position ));
247+
let products = json.data;
248+
249+
products = products.sort((a,b) => (a.position - b.position ));
246250

247-
json.forEach((element, index, theArray) => {
251+
products.forEach((element, index, theArray) => {
248252
if(theArray[index].name === '') {
249253
theArray[index].name = `<${messages.draft}>`;
250254
}
251255
})
252256

253-
dispatch(receiveProductsMore(json))
257+
dispatch(receiveProductsMore(products))
254258
})
255259
.catch(error => {
256260
dispatch(receiveProductsError(error));

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ProductsService {
2121
const limit = parse.getNumberIfPositive(params.limit) || 1000000;
2222
const offset = parse.getNumberIfPositive(params.offset) || 0;
2323
const projectQuery = this.getProjectQuery(fieldsArray);
24-
const sortQuery = this.getSortQuery(params);
24+
const sortQuery = this.getSortQuery(params); // todo: validate every sort field
2525
const matchQuery = this.getMatchQuery(params, categories);
2626
const matchTextQuery = this.getMatchTextQuery(params);
2727
const thumbnail_width = parse.getNumberIfPositive(params.thumbnail_width);
@@ -43,15 +43,30 @@ class ProductsService {
4343
if(matchTextQuery) {
4444
aggregationCount.push({ $match: matchTextQuery });
4545
}
46+
aggregationCount.push({ $project: projectQuery });
4647
aggregationCount.push({ $match: matchQuery });
47-
aggregationCount.push({$group: {_id: null, count: { $sum: 1 }}});
48+
aggregationCount.push({$group: {_id: null, count: { $sum: 1 }, min_price: { $min: "$price" }, max_price: { $max: "$price" }}});
4849

4950
return mongo.db.collection('products').aggregate(aggregationPipeline).toArray()
5051
.then(items => items.map(item => this.changeProperties(categories, item, thumbnail_width))).then(items => {
5152
return mongo.db.collection('products').aggregate(aggregationCount).toArray().then(countItems => {
53+
let total_count = 0;
54+
let min_price = 0;
55+
let max_price = 0;
56+
57+
if(countItems && countItems.length === 1) {
58+
total_count = countItems[0].count;
59+
min_price = countItems[0].min_price || 0;
60+
max_price = countItems[0].max_price || 0;
61+
}
62+
5263
return {
53-
total_count: countItems[0].count,
54-
has_more: (offset + items.length) < countItems[0].count,
64+
price: {
65+
min: min_price,
66+
max: max_price
67+
},
68+
total_count: total_count,
69+
has_more: (offset + items.length) < total_count,
5570
data: items
5671
}
5772
})

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,18 @@ const staticOptions = {
4141

4242
app.set('trust proxy', 1) // trust first proxy
4343
app.use(express.static(STATIC_ROOT_DIRECTORY, staticOptions))
44+
app.get('/*.ico', (req, res) => {
45+
res.status(404).end();
46+
});
47+
app.get('/assets/*', (req, res) => {
48+
res.status(404).end();
49+
});
4450
app.use(helmet())
4551
app.use(responseTime())
4652
app.use(bodyParser.urlencoded({extended: true}));
4753
app.use(bodyParser.json());
4854
app.use('/api', apiRouter);
49-
app.get('/admin/*', function(req, res) {
55+
app.get('/admin/*', (req, res) => {
5056
res.sendFile(ADMIN_INDEX_PATH)
5157
});
5258
app.use(cookieParser(settings.security.cookieKey));

src/store/server/ajax.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const fillCartItems = (cartResponse) => {
2121
if(cart && cart.items && cart.items.length > 0) {
2222
const productIds = cart.items.map(item => item.product_id);
2323
return api.products.list({ ids: productIds, fields: 'images,enabled,stock_quantity' }).then(({status, json}) => {
24-
const newCartItem = cart.items.map(cartItem => fillCartItemWithProductData(json, cartItem))
24+
const newCartItem = cart.items.map(cartItem => fillCartItemWithProductData(json.data, cartItem))
2525
cartResponse.json.items = newCartItem;
2626
return cartResponse;
2727
})

src/store/shared/actionTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ export const SITEMAP_RECEIVE = 'SITEMAP_RECEIVE'
4848

4949
export const SET_CURRENT_CATEGORY = 'SET_CURRENT_CATEGORY'
5050
export const SET_PRODUCTS_FILTER = 'SET_PRODUCTS_FILTER'
51+
export const SET_PRODUCTS_PRICE_RANGE = 'SET_PRODUCTS_PRICE_RANGE'

src/store/shared/actions.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ export const fetchProducts = () => (dispatch, getState) => {
2121
dispatch(requestProducts());
2222

2323
let filter = app.productsFilter;
24+
filter.offset = 0;
25+
2426
return api.ajax.products.list(filter).then(({status, json}) => {
2527
dispatch(receiveProducts(json))
28+
if(!app.products_min_price && !app.products_max_price) {
29+
dispatch(setProductsPriceRange(json.price.min, json.price.max));
30+
}
2631
}).catch(error => {});
2732
}
2833

@@ -32,13 +37,13 @@ const receiveProducts = (products) => ({type: t.PRODUCTS_RECEIVE, products})
3237

3338
export const fetchMoreProducts = () => (dispatch, getState) => {
3439
const {app} = getState();
35-
if (app.loadingProducts || app.products.length === 0) {
40+
if (app.loadingProducts || app.loadingMoreProducts || app.products.length === 0 || !app.products_has_more) {
3641
return Promise.resolve();
3742
} else {
3843
dispatch(requestMoreProducts());
3944

4045
let filter = app.productsFilter;
41-
filter.limit = 15;
46+
//filter.limit = 15;
4247
filter.offset = app.products.length;
4348

4449
return api.ajax.products.list(filter).then(({status, json}) => {
@@ -154,7 +159,10 @@ export const setCategory = category_id => (dispatch, getState) => {
154159
const category = app.categories.find(c => c.id === category_id);
155160
if (category) {
156161
dispatch(setCurrentCategory(category));
162+
dispatch(setProductsPriceRange(null, null));
163+
dispatch(clearProductsFilter());
157164
dispatch(setProductsFilter({category_id: category_id}));
165+
dispatch(receiveProduct(null));
158166
}
159167
}
160168

@@ -170,17 +178,31 @@ export const setSort = sort => (dispatch, getState) => {
170178
dispatch(fetchProducts());
171179
}
172180

181+
export const setPriceFromAndTo = (price_from, price_to) => (dispatch, getState) => {
182+
if(price_to > 0) {
183+
dispatch(setProductsFilter({price_from: price_from, price_to: price_to}));
184+
dispatch(fetchProducts());
185+
}
186+
}
187+
173188
export const setPriceFrom = price_from => (dispatch, getState) => {
174189
dispatch(setProductsFilter({price_from: price_from}));
175190
dispatch(fetchProducts());
176191
}
177192

178193
export const setPriceTo = price_to => (dispatch, getState) => {
179-
dispatch(setProductsFilter({price_to: price_to}));
180-
dispatch(fetchProducts());
194+
if(price_to > 0) {
195+
dispatch(setProductsFilter({price_to: price_to}));
196+
dispatch(fetchProducts());
197+
}
181198
}
182199

183-
const setProductsFilter = filter => ({type: t.SET_PRODUCTS_FILTER, filter})
200+
const setProductsPriceRange = (min, max) => ({type: t.SET_PRODUCTS_PRICE_RANGE, min, max})
201+
202+
const setProductsFilter = filter => ({type: t.SET_PRODUCTS_FILTER, filter: filter})
203+
204+
const EMPTY_FILTER = { on_sale: null, search: '', price_from: 0, price_to: 0 };
205+
const clearProductsFilter = () => ({type: t.SET_PRODUCTS_FILTER, filter: EMPTY_FILTER});
184206

185207
export const updateCartShippingCountry = country => (dispatch, getState) => {
186208
return [
@@ -305,6 +327,10 @@ export const getInitialState = (req, checkoutFields, currentPage, settings) => {
305327
productDetails: null,
306328
categories: [],
307329
products: [],
330+
products_total_count: 0,
331+
products_has_more: false,
332+
products_min_price: 0,
333+
products_max_price: 0,
308334
paymentMethods: [],
309335
shippingMethods: [],
310336
loadingProducts: false,
@@ -316,8 +342,8 @@ export const getInitialState = (req, checkoutFields, currentPage, settings) => {
316342
on_sale: null,
317343
search: '',
318344
category_id: null,
319-
price_from: null,
320-
price_to: null,
345+
price_from: 0,
346+
price_to: 0,
321347
limit: 30,
322348
sort: settings.default_product_sorting,
323349
fields: 'path,id,name,category_id,category_name,sku,images,enabled,discontinued,stock_status,stock_quantity,price,on_sale,regular_price'
@@ -335,7 +361,15 @@ export const getInitialState = (req, checkoutFields, currentPage, settings) => {
335361
return getCommonData(req, currentPage, initialState.app.productsFilter).then(commonData => {
336362
initialState.app.categories = commonData.categories;
337363
initialState.app.productDetails = commonData.product;
338-
initialState.app.products = commonData.products;
364+
if(commonData.products) {
365+
initialState.app.products = commonData.products.data;
366+
initialState.app.products_total_count = commonData.products.total_count;
367+
initialState.app.products_has_more = commonData.products.has_more;
368+
if(commonData.products.price) {
369+
initialState.app.products_min_price = commonData.products.price.min;
370+
initialState.app.products_max_price = commonData.products.price.max;
371+
}
372+
}
339373
initialState.app.categoryDetails = commonData.categoryDetails;
340374
initialState.app.cart = commonData.cart;
341375
initialState.app.pageDetails = commonData.page;

src/store/shared/containerProps.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {addCartItem, deleteCartItem, updateCartItemQuantiry, fetchMoreProducts, setSearch, setSort, setPriceFrom, setPriceTo} from './actions'
1+
import {addCartItem, deleteCartItem, updateCartItemQuantiry, fetchMoreProducts, setSearch, setSort, setPriceFromAndTo, setPriceFrom, setPriceTo} from './actions'
22

33
export const mapStateToProps = (state, ownProps) => {
44
return {
@@ -26,6 +26,9 @@ export const mapDispatchToProps = (dispatch, ownProps) => {
2626
setSort: (sort) => {
2727
dispatch(setSort(sort));
2828
},
29+
setPriceFromAndTo: (price_from, price_to) => {
30+
dispatch(setPriceFromAndTo(price_from, price_to));
31+
},
2932
setPriceFrom: (price_from) => {
3033
dispatch(setPriceFrom(price_from));
3134
},

src/store/shared/reducers.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ const appReducer = (state = initialState, action) => {
1414
return Object.assign({}, state, {loadingProducts: true})
1515

1616
case t.PRODUCTS_RECEIVE:
17-
return Object.assign({}, state, {loadingProducts: false, products: action.products})
17+
return Object.assign({}, state, {
18+
loadingProducts: false,
19+
products: action.products.data,
20+
products_total_count: action.products.total_count,
21+
products_has_more: action.products.has_more
22+
})
1823

1924
case t.MORE_PRODUCTS_REQUEST:
2025
return Object.assign({}, state, {loadingMoreProducts: true})
@@ -24,8 +29,10 @@ const appReducer = (state = initialState, action) => {
2429
loadingMoreProducts: false,
2530
products: [
2631
...state.products,
27-
...action.products
28-
]
32+
...action.products.data
33+
],
34+
products_total_count: action.products.total_count,
35+
products_has_more: action.products.has_more
2936
})
3037

3138
case t.PAGE_RECEIVE:
@@ -73,6 +80,12 @@ const appReducer = (state = initialState, action) => {
7380
productsFilter: Object.assign({}, state.productsFilter, action.filter)
7481
})
7582

83+
case t.SET_PRODUCTS_PRICE_RANGE:
84+
return Object.assign({}, state, {
85+
products_min_price: action.min || 0,
86+
products_max_price: action.max || 0
87+
})
88+
7689
case LOCATION_CHANGE:
7790
case t.PRODUCT_REQUEST:
7891
case t.PAGE_REQUEST:

src/store/shared/routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ function getComponent(nextState, cb) {
4040
getSitemap(nextState.location.pathname, state, dispatch).then(({currentPage, currentPageAlreadyInState}) => {
4141
if (currentPage) {
4242
if (currentPage.type === PRODUCT_CATEGORY) {
43-
dispatch(setCategory(currentPage.resource))
4443
if(!currentPageAlreadyInState){
44+
dispatch(setCategory(currentPage.resource))
4545
dispatch(fetchProducts());
4646
}
4747
cb(null, props => <CategoryContainer {...props}/>);

0 commit comments

Comments
 (0)