@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
9 lines • 7.62 kB
JavaScript
function _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArrayLimit(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally{try{if(!_n&&_i["return"]!=null)_i["return"]();}finally{if(_d)throw _e;}}return _arr;}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}import{hex2bin}from'@shopgate/pwa-common/helpers/data';import showModal from'@shopgate/pwa-common/actions/modal/showModal';import{getThemeSettings,historyPop,historyPush,routeWillEnter$,routeWillLeave$}from'@shopgate/engage/core';import{ToastProvider}from'@shopgate/pwa-common/providers';import{getSearchRoute}from'@shopgate/pwa-common-commerce/search';import fetchProduct from"../actions/fetchProduct";import fetchProductDescription from"../actions/fetchProductDescription";import fetchProductProperties from"../actions/fetchProductProperties";import fetchProductImages from"../actions/fetchProductImages";import fetchProductShipping from"../actions/fetchProductShipping";import fetchProductVariants from"../actions/fetchProductVariants";import fetchProductOptions from"../actions/fetchProductOptions";import fetchProductMedia from"../actions/fetchProductMedia";import{productWillEnter$,galleryWillEnter$,productReceived$,cachedProductReceived$,productRelationsReceived$,receivedVisibleProduct$,errorProductResourcesNotFound$,visibleProductNotFound$,productNotAvailable$,fetchProductsRequested$}from"../streams";import fetchProductsById from"../actions/fetchProductsById";import{getProductRelationsByHash}from"../selectors/relations";import{checkoutSucceeded$}from"../../checkout/streams";import expireProductById from"../action-creators/expireProductById";import{ITEM_PATTERN,NOT_AVAILABLE_EFFECTIVITY_DATES}from"../constants";import{getProductName}from"../selectors/product";/**
* Product subscriptions.
* @param {Function} subscribe The subscribe function.
*/function product(subscribe){var processProduct$=productReceived$.merge(cachedProductReceived$);subscribe(productWillEnter$,function(_ref){var action=_ref.action,dispatch=_ref.dispatch;var productId=action.route.params.productId;var variantId=action.route.state.productId;var id=variantId||hex2bin(productId);dispatch(fetchProduct(id));dispatch(fetchProductDescription(id));dispatch(fetchProductProperties(id));dispatch(fetchProductImages(id));dispatch(fetchProductShipping(id));/**
* This feature is currently in BETA testing.
* It should only be used for approved BETA Client Projects
*/dispatch(fetchProductMedia(id));});subscribe(galleryWillEnter$,function(_ref2){var action=_ref2.action,dispatch=_ref2.dispatch;var productId=action.route.params.productId;dispatch(fetchProductImages(hex2bin(productId)));dispatch(fetchProductMedia(hex2bin(productId)));});subscribe(processProduct$,function(_ref3){var action=_ref3.action,dispatch=_ref3.dispatch;var _action$productData=action.productData,id=_action$productData.id,_action$productData$f=_action$productData.flags,flags=_action$productData$f===void 0?{hasVariants:false,hasOptions:false}:_action$productData$f,baseProductId=_action$productData.baseProductId,active=_action$productData.active;if(baseProductId){dispatch(fetchProduct(baseProductId));dispatch(fetchProductImages(baseProductId));}if(active&&flags.hasVariants){dispatch(fetchProductVariants(id));}if(active&&flags.hasOptions){dispatch(fetchProductOptions(id));}});var errorProductResourcesNotFoundFirst$=receivedVisibleProduct$.filter(function(_ref4){var _action$productData2;var action=_ref4.action;var active=action===null||action===void 0?void 0:(_action$productData2=action.productData)===null||_action$productData2===void 0?void 0:_action$productData2.active;return!!active;}).switchMap(function(){return errorProductResourcesNotFound$.first();});/** Refresh product data after some of resources has ENOTFOUND code */subscribe(errorProductResourcesNotFoundFirst$,function(_ref5){var action=_ref5.action,dispatch=_ref5.dispatch;var productId=action.productId;dispatch(fetchProduct(productId,true));});var productNotFound$=visibleProductNotFound$.withLatestFrom(receivedVisibleProduct$.startWith({action:{productData:{}}}));/** Visible product is no more available */subscribe(productNotFound$,function(_ref6){var _ref7=_slicedToArray(_ref6,2),_ref7$=_ref7[0],action=_ref7$.action,dispatch=_ref7$.dispatch,productData=_ref7[1].action.productData;var productId=action.productId;var name=productData.id===productId?productData.name:productId;dispatch(showModal({confirm:null,dismiss:'modal.ok',title:'modal.title_error',message:'product.no_more_found',params:{name:name}}));dispatch(historyPop());dispatch(expireProductById(productId));});subscribe(productRelationsReceived$,function(_ref8){var dispatch=_ref8.dispatch,getState=_ref8.getState,action=_ref8.action;var hash=action.hash;var productIds=getProductRelationsByHash(hash)(getState());dispatch(fetchProductsById(productIds));});/**
* Expire products after checkout, fetch updated data
*/subscribe(checkoutSucceeded$,function(_ref9){var dispatch=_ref9.dispatch,action=_ref9.action;var products=action.products;var productIds=products.map(function(p){return p.product.id;});productIds.forEach(function(id){return dispatch(expireProductById(id));});dispatch(fetchProductsById(productIds));});var productNotAvailableEffDates$=productNotAvailable$.filter(function(_ref10){var action=_ref10.action;return action.reason===NOT_AVAILABLE_EFFECTIVITY_DATES;});/** PDP expired effectivity dates */var productNotAvailableEffDatesPDP$=productNotAvailableEffDates$.withLatestFrom(routeWillEnter$).filter(function(_ref11){var _ref12=_slicedToArray(_ref11,2),notAvailable=_ref12[0],willEnter=_ref12[1];return willEnter.action.route.pattern===ITEM_PATTERN&¬Available.action.productId===willEnter.action.route.state.productId;}).map(function(_ref13){var _ref14=_slicedToArray(_ref13,1),notAvailable=_ref14[0];return notAvailable;});subscribe(productNotAvailableEffDatesPDP$,function(_ref15){var action=_ref15.action,getState=_ref15.getState,dispatch=_ref15.dispatch,events=_ref15.events;var _ref16=getThemeSettings('product')||{},_ref16$effectivityDat=_ref16.effectivityDates,_ref16$effectivityDat2=_ref16$effectivityDat===void 0?{}:_ref16$effectivityDat,accessExpired=_ref16$effectivityDat2.accessExpired;if(accessExpired===false){var productId=action.productId;dispatch(historyPop());var name=getProductName(getState(),{productId:productId});events.emit(ToastProvider.ADD,{id:'product.available.not_search_similar',message:'product.available.not_search_similar',messageParams:{name:name},action:function action(){return dispatch(historyPush({pathname:getSearchRoute(name)}));}});}});/** Expired effectivity dates products after PLP leave */var productNotAvailableEffDatesPLP$=productNotAvailableEffDates$.buffer(routeWillLeave$).filter(function(buffered){return buffered.length;});subscribe(productNotAvailableEffDatesPLP$,function(buffered){var _buffered=_slicedToArray(buffered,1),dispatch=_buffered[0].dispatch;var productIds=buffered.map(function(params){return params.action.productId;});dispatch(expireProductById(productIds,true));});subscribe(fetchProductsRequested$,function(_ref17){var dispatch=_ref17.dispatch,productId=_ref17.action.productId;dispatch(fetchProductsById(Array.isArray(productId)?productId:[productId]));});}export default product;