@@ -42,17 +42,21 @@ function requestMoreProducts() {
4242 }
4343}
4444
45- function receiveProductsMore ( items ) {
45+ function receiveProductsMore ( { has_more , total_count , data } ) {
4646 return {
4747 type : t . PRODUCTS_MORE_RECEIVE ,
48- items
48+ has_more,
49+ total_count,
50+ data
4951 }
5052}
5153
52- function receiveProducts ( items ) {
54+ function receiveProducts ( { has_more , total_count , data } ) {
5355 return {
5456 type : t . PRODUCTS_RECEIVE ,
55- items
57+ has_more,
58+ total_count,
59+ data
5660 }
5761}
5862
@@ -89,38 +93,10 @@ export function selectAllProduct() {
8993 }
9094}
9195
92- export function setFilterSearch ( value ) {
96+ export function setFilter ( filter ) {
9397 return {
94- type : t . PRODUCTS_FILTER_SET_SEARCH ,
95- search : value
96- }
97- }
98-
99- export function setFilterStock ( value ) {
100- return {
101- type : t . PRODUCTS_FILTER_SET_STOCK ,
102- stock_status : value
103- }
104- }
105-
106- export function setFilterEnabled ( value ) {
107- return {
108- type : t . PRODUCTS_FILTER_SET_ENABLED ,
109- enabled : value
110- }
111- }
112-
113- export function setFilterDiscontinued ( value ) {
114- return {
115- type : t . PRODUCTS_FILTER_SET_DISCONTINUED ,
116- discontinued : value
117- }
118- }
119-
120- export function setFilterOnSale ( value ) {
121- return {
122- type : t . PRODUCTS_FILTER_SET_ONSALE ,
123- on_sale : value
98+ type : t . PRODUCTS_SET_FILTER ,
99+ filter : filter
124100 }
125101}
126102
@@ -162,49 +138,49 @@ function successCreateProduct(id) {
162138 }
163139}
164140
165- export function fetchProducts ( ) {
166- return ( dispatch , getState ) => {
167- const state = getState ( ) ;
168- if ( ! state . products . isFetching ) {
169- dispatch ( requestProducts ( ) ) ;
170- dispatch ( deselectAllProduct ( ) ) ;
171-
172- let filter = { limit : 20 , fields : 'id,name,category_id,category_name,sku,images,enabled,discontinued,stock_status,stock_quantity,price,on_sale,regular_price' } ;
173- filter . search = state . products . filter_search ;
141+ const getFilter = ( state , offset = 0 ) => {
142+ let filter = {
143+ limit : 50 ,
144+ fields : 'id,name,category_id,category_name,sku,images,enabled,discontinued,stock_status,stock_quantity,price,on_sale,regular_price' ,
145+ search : state . products . filter . search ,
146+ offset : offset
147+ }
174148
175- if ( state . products . filter_stock_status && state . products . filter_stock_status !== 'all' ) {
176- filter . stock_status = state . products . filter_stock_status ;
177- }
149+ if ( state . productCategories . selectedId !== null && state . productCategories . selectedId !== 'all' ) {
150+ filter . category_id = state . productCategories . selectedId ;
151+ }
178152
179- if ( state . productCategories . selectedId ) {
180- filter . category_id = state . productCategories . selectedId ;
181- }
153+ if ( state . products . filter . stockStatus !== null ) {
154+ filter . stock_status = state . products . filter . stockStatus ;
155+ }
182156
183- if ( state . products . filter_enabled ) {
184- filter . enabled = true ;
185- }
157+ if ( state . products . filter . enabled !== null ) {
158+ filter . enabled = state . products . filter . enabled ;
159+ }
186160
187- if ( state . products . filter_discontinued ) {
188- filter . discontinued = true ;
189- }
161+ if ( state . products . filter . discontinued !== null ) {
162+ filter . discontinued = state . products . filter . discontinued ;
163+ }
190164
191- if ( state . products . filter_on_sale ) {
192- filter . on_sale = true ;
193- }
165+ if ( state . products . filter . onSale !== null ) {
166+ filter . on_sale = state . products . filter . onSale ;
167+ }
194168
195- return api . products . list ( filter )
196- . then ( ( { status, json} ) => {
197- let products = json . data ;
169+ return filter ;
170+ }
198171
199- products = product . sort ( ( a , b ) => ( a . position - b . position ) ) ;
172+ export function fetchProducts ( ) {
173+ return ( dispatch , getState ) => {
174+ const state = getState ( ) ;
175+ if ( ! state . products . loadingItems ) {
176+ dispatch ( requestProducts ( ) ) ;
177+ dispatch ( deselectAllProduct ( ) ) ;
200178
201- products . forEach ( ( element , index , theArray ) => {
202- if ( theArray [ index ] . name === '' ) {
203- theArray [ index ] . name = `<${ messages . draft } >` ;
204- }
205- } )
179+ let filter = getFilter ( state ) ;
206180
207- dispatch ( receiveProducts ( products ) )
181+ return api . products . list ( filter )
182+ . then ( ( { status, json} ) => {
183+ dispatch ( receiveProducts ( json ) )
208184 } )
209185 . catch ( error => {
210186 dispatch ( receiveProductsError ( error ) ) ;
@@ -216,49 +192,19 @@ export function fetchProducts() {
216192export function fetchMoreProducts ( ) {
217193 return ( dispatch , getState ) => {
218194 const state = getState ( ) ;
219- dispatch ( requestMoreProducts ( ) ) ;
220-
221- let filter = { limit : 50 , fields : 'id,name,category_id,category_name,sku,images,enabled,discontinued,stock_status,stock_quantity,price,on_sale,regular_price' } ;
222- filter . offset = state . products . items . length ;
223- filter . search = state . products . filter_search ;
224-
225- if ( state . products . filter_stock_status && state . products . filter_stock_status !== 'all' ) {
226- filter . stock_status = state . products . filter_stock_status ;
227- }
195+ if ( ! state . products . loadingItems ) {
196+ dispatch ( requestMoreProducts ( ) ) ;
228197
229- if ( state . productCategories . selectedId ) {
230- filter . category_id = state . productCategories . selectedId ;
231- }
232-
233- if ( state . products . filter_enabled ) {
234- filter . enabled = true ;
235- }
198+ let filter = getFilter ( state , state . products . items . length ) ;
236199
237- if ( state . products . filter_discontinued ) {
238- filter . discontinued = true ;
239- }
240-
241- if ( state . products . filter_on_sale ) {
242- filter . on_sale = true ;
243- }
244-
245- return api . products . list ( filter )
246- . then ( ( { status, json} ) => {
247- let products = json . data ;
248-
249- products = products . sort ( ( a , b ) => ( a . position - b . position ) ) ;
250-
251- products . forEach ( ( element , index , theArray ) => {
252- if ( theArray [ index ] . name === '' ) {
253- theArray [ index ] . name = `<${ messages . draft } >` ;
254- }
200+ return api . products . list ( filter )
201+ . then ( ( { status, json} ) => {
202+ dispatch ( receiveProductsMore ( json ) )
255203 } )
256-
257- dispatch ( receiveProductsMore ( products ) )
258- } )
259- . catch ( error => {
260- dispatch ( receiveProductsError ( error ) ) ;
261- } ) ;
204+ . catch ( error => {
205+ dispatch ( receiveProductsError ( error ) ) ;
206+ } ) ;
207+ }
262208 }
263209}
264210
0 commit comments