@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
6 lines • 3.67 kB
JavaScript
function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}import{ACTION_PUSH,ACTION_REPLACE}from'@virtuous/conductor';import{buildFetchCategoryProductsParams,getProductsResult}from'@shopgate/engage/product';import{getCurrentRoute}from'@shopgate/pwa-common/selectors/router';import{historyPop}from'@shopgate/pwa-common/actions/router';import expireProductsByHash from'@shopgate/pwa-common-commerce/product/action-creators/expireProductsByHash';import fetchCategory from'@shopgate/pwa-common-commerce/category/actions/fetchCategory';import fetchCategoryProducts from'@shopgate/pwa-common-commerce/category/actions/fetchCategoryProducts';import fetchFilters from'@shopgate/pwa-common-commerce/filter/actions/fetchFilters';import{hex2bin}from'@shopgate/pwa-common/helpers/data';import{categoryProductsNeedUpdate$}from'@shopgate/engage/category/streams';import{categoryWillEnter$,categoryDidEnter$,errorVisibleCategory$,categoryFiltersDidUpdateFromFilterPage$}from"../streams";/**
* Category subscriptions.
* @param {Function} subscribe The subscribe function.
*/export default function category(subscribe){subscribe(categoryWillEnter$,function(_ref){var _action$route;var dispatch=_ref.dispatch,action=_ref.action,getState=_ref.getState;if(![ACTION_PUSH,ACTION_REPLACE].includes(action===null||action===void 0?void 0:action.historyAction)){return;}var _action$route$state=action.route.state,filters=_action$route$state.filters,_action$route$state$o=_action$route$state.offset,offset=_action$route$state$o===void 0?0:_action$route$state$o;var categoryId=hex2bin(action.route.params.categoryId);var _getProductsResult=getProductsResult(getState(),_extends({categoryId:categoryId,routeId:action===null||action===void 0?void 0:(_action$route=action.route)===null||_action$route===void 0?void 0:_action$route.id},buildFetchCategoryProductsParams())),hash=_getProductsResult.hash,expired=_getProductsResult.expired;if(expired){dispatch(expireProductsByHash(hash));}dispatch(fetchCategory(categoryId));dispatch(fetchCategoryProducts(_extends({categoryId:categoryId,filters:filters,offset:offset},buildFetchCategoryProductsParams())));});subscribe(categoryDidEnter$,function(_ref2){var dispatch=_ref2.dispatch;dispatch(fetchFilters());});/**
* Gets triggered on pipeline category error.
*/subscribe(errorVisibleCategory$,function(_ref3){var dispatch=_ref3.dispatch;dispatch(historyPop());});subscribe(categoryProductsNeedUpdate$,function(_ref4){var dispatch=_ref4.dispatch,getState=_ref4.getState;var _getCurrentRoute=getCurrentRoute(getState()),params=_getCurrentRoute.params,_getCurrentRoute$stat=_getCurrentRoute.state,_getCurrentRoute$stat2=_getCurrentRoute$stat.offset,offset=_getCurrentRoute$stat2===void 0?0:_getCurrentRoute$stat2,filters=_getCurrentRoute$stat.filters;var categoryId=hex2bin(params.categoryId);dispatch(fetchCategoryProducts(_extends({categoryId:categoryId,filters:filters,offset:offset},buildFetchCategoryProductsParams())));});subscribe(categoryFiltersDidUpdateFromFilterPage$,function(_ref5){var dispatch=_ref5.dispatch,action=_ref5.action;var categoryId=hex2bin(action.route.params.categoryId);var _action$route$state2=action.route.state,filters=_action$route$state2.filters,_action$route$state2$=_action$route$state2.offset,offset=_action$route$state2$===void 0?0:_action$route$state2$;dispatch(fetchCategoryProducts(_extends({categoryId:categoryId,filters:filters,offset:offset},buildFetchCategoryProductsParams())));});}