@@ -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
3338export 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+
173188export const setPriceFrom = price_from => ( dispatch , getState ) => {
174189 dispatch ( setProductsFilter ( { price_from : price_from } ) ) ;
175190 dispatch ( fetchProducts ( ) ) ;
176191}
177192
178193export 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
185207export 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 ;
0 commit comments