@@ -252,74 +252,80 @@ export const sendPaymentToken = (token) => async dispatch => {
252252}
253253
254254
255- export const getDataViaAPI = ( type , uri , query ) => async dispatch => {
256- if ( uri ) {
257- if ( query ) {
258- uri += query
255+ export const getDataViaAPI = ( type , route , query , synchronous = true ) => async dispatch => {
256+ if ( route ) {
257+ if ( query ) {
258+ route += query
259259 }
260260
261- log . info ( `[ACTION]: invokeAndDispatchAPIData Calling API = ${ uri } .` )
262-
263- // uri = uri.replace(/\s/g, '')
264- let responseError = false
265- const response = await commonServiceAPI . get ( uri )
266- . catch ( err => {
267- log . info ( `[ACTION]: unable to fetch response for API = ${ uri } ` )
268- dispatch ( { type : type , payload : { isLoading : false , statusCode : INTERNAL_SERVER_ERROR_CODE } } ) ;
269- responseError = true
270- } ) ;
271-
272- if ( responseError ) {
273- return
274- }
275-
276- if ( response != null ) {
277- log . debug ( `[ACTION]: Data = ${ JSON . parse ( JSON . stringify ( response . data ) ) } .` )
278- let payload = { isLoading : false , data : JSON . parse ( JSON . stringify ( response . data ) ) }
279- if ( query ) {
280- dispatch ( {
281- type : type , payload :
282- { ...payload , query : query }
261+ log . info ( `[ACTION]: invokeAndDispatchAPIData Calling API = ${ route } .` )
262+ let isFetchError = false
263+ if ( synchronous ) {
264+ await commonServiceAPI . get ( route )
265+ . then ( response => processResponse ( response , query , type , route ) )
266+ . catch ( err => {
267+ isFetchError = true
283268 } ) ;
284- } else {
285- dispatch ( {
286- type : type , payload : payload
269+ } else {
270+ commonServiceAPI . get ( route )
271+ . then ( response => processResponse ( response , query , type , route , dispatch ) )
272+ . catch ( err => {
273+ isFetchError = true
287274 } ) ;
288- }
275+ }
289276
277+ if ( isFetchError ) {
278+ log . info ( `[ACTION]: unable to fetch response for API = ${ route } ` )
279+ dispatch ( { type : type , payload : { isLoading : false , statusCode : INTERNAL_SERVER_ERROR_CODE } } ) ;
280+ }
281+ }
282+ }
290283
291- if ( LOAD_FILTER_PRODUCTS . localeCompare ( type ) === 0 &&
292- window . location . search . localeCompare ( uri . split ( "/products" ) [ 1 ] ) !== 0 ) {
293- history . push ( uri )
294- }
284+ export const processResponse = ( response , query , type , uri , dispatch ) => {
285+ log . debug ( `[ACTION]: Data = ${ JSON . parse ( JSON . stringify ( response . data ) ) } .` )
286+ if ( response . data !== null ) {
287+ let payload = { isLoading : false , data : JSON . parse ( JSON . stringify ( response . data ) ) }
288+ if ( query ) {
289+ dispatch ( {
290+ type : type , payload :
291+ { ...payload , query : query }
292+ } ) ;
295293 } else {
296- dispatch ( { type : type , payload : { isLoading : false , statusCode : BAD_REQUEST_ERROR_CODE } } ) ;
294+ dispatch ( {
295+ type : type , payload : payload
296+ } ) ;
297+ }
298+
299+ if ( LOAD_FILTER_PRODUCTS . localeCompare ( type ) === 0 &&
300+ window . location . search . localeCompare ( uri . split ( "/products" ) [ 1 ] ) !== 0 ) {
301+ history . push ( uri )
297302 }
303+ } else {
304+ dispatch ( { type : type , payload : { isLoading : false , statusCode : BAD_REQUEST_ERROR_CODE } } ) ;
298305 }
299306}
300307
301- export const loadFilterAttributes = filterQuery => async dispatch => {
308+ export const loadFilterAttributes = filterQuery => dispatch => {
302309 log . info ( `[ACTION]: loadFilterAttributes Calling Filter API filterQuery = ${ filterQuery } ` )
303310
304311 if ( filterQuery ) {
305312 let uri = `/filter${ filterQuery } `
306- const response = await commonServiceAPI . get ( uri ) ;
307- if ( response != null ) {
308- log . trace ( `[ACTION]: Filter = ${ JSON . stringify ( response . data ) } ` )
313+ commonServiceAPI . get ( uri )
314+ . then ( response => {
315+ dispatch ( {
316+ type : LOAD_FILTER_ATTRIBUTES ,
317+ payload : JSON . parse ( JSON . stringify (
318+ {
319+ ...response . data ,
320+ "query" : filterQuery . slice ( 3 )
321+ } ) )
322+ } ) ;
309323
310- dispatch ( {
311- type : LOAD_FILTER_ATTRIBUTES ,
312- payload : JSON . parse ( JSON . stringify (
313- {
314- ...response . data ,
315- "query" : filterQuery . slice ( 3 )
316- } ) )
324+ return JSON . parse ( JSON . stringify ( response . data ) )
325+ } )
326+ . catch ( error => {
327+ log . info ( `[ACTION]: unable to fetch response for Filter API` )
317328 } ) ;
318-
319- return JSON . parse ( JSON . stringify ( response . data ) )
320- } else {
321- log . info ( `[ACTION]: unable to fetch response for Filter API` )
322- }
323329 }
324330} ;
325331
0 commit comments