UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

17 lines 7.09 kB
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);}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'rxjs/add/operator/switchMap';import'rxjs/add/observable/of';import{Observable}from'rxjs/Observable';import{ENOTFOUND}from'@shopgate/pwa-core';import{main$}from'@shopgate/pwa-common/streams/main';import{routeWillEnter$,routeWillLeave$,routeDidUpdate$}from'@shopgate/pwa-common/streams/router';import{pwaDidAppear$}from'@shopgate/pwa-common/streams';import{getCurrentRoute}from'@shopgate/pwa-common/selectors/router';import{hex2bin}from'@shopgate/pwa-common/helpers/data';import{getBaseProduct}from"../selectors/product";import{ITEM_PATTERN,ITEM_GALLERY_PATTERN,RECEIVE_PRODUCT,RECEIVE_PRODUCT_CACHED,RECEIVE_PRODUCT_RELATIONS,ERROR_PRODUCT,ERROR_PRODUCT_DESCRIPTION,ERROR_PRODUCT_IMAGES,ERROR_PRODUCT_VARIANTS,ERROR_PRODUCT_PROPERTIES,ERROR_PRODUCT_OPTIONS,ERROR_PRODUCT_SHIPPING,PRODUCT_NOT_AVAILABLE,RECEIVE_PRODUCTS_CACHED,RECEIVE_PRODUCTS,PROVIDE_PRODUCT_BUFFER_TIME,PROVIDE_PRODUCT}from"../constants";/** * Emits when the category route comes active again after a legacy page was active. */export var productRouteReappeared$=pwaDidAppear$.filter(function(_ref){var action=_ref.action;return action.route.pattern===ITEM_PATTERN;});export var productWillEnter$=routeWillEnter$.merge(routeDidUpdate$).filter(function(_ref2){var action=_ref2.action;return action.route.pattern===ITEM_PATTERN;});export var variantWillUpdate$=routeDidUpdate$.filter(function(_ref3){var action=_ref3.action;return action.route.pattern===ITEM_PATTERN;});export var galleryWillEnter$=routeWillEnter$.filter(function(_ref4){var action=_ref4.action;return action.route.pattern===ITEM_GALLERY_PATTERN;});export var galleryWillLeave$=routeWillLeave$.filter(function(_ref5){var action=_ref5.action;return action.route.pattern===ITEM_GALLERY_PATTERN;});export var productReceived$=main$.filter(function(_ref6){var action=_ref6.action;return action.type===RECEIVE_PRODUCT;}).distinctUntilChanged();/** Dispatched when ERROR_PRODUCT received */export var errorProduct$=main$.filter(function(_ref7){var action=_ref7.action;return action.type===ERROR_PRODUCT;});/** Dispatched when ERROR_PRODUCT_* (resources) is received */export var errorProductResources$=main$.filter(function(_ref8){var action=_ref8.action;return[ERROR_PRODUCT_DESCRIPTION,ERROR_PRODUCT_IMAGES,ERROR_PRODUCT_VARIANTS,ERROR_PRODUCT_PROPERTIES,ERROR_PRODUCT_OPTIONS,ERROR_PRODUCT_SHIPPING].includes(action.type);});/** Dispatched when ERROR_PRODUCT ENOTFOUND received */export var errorProductNotFound$=errorProduct$.filter(function(_ref9){var action=_ref9.action;return action.errorCode===ENOTFOUND;});/** Dispatched when ERROR_PRODUCT_* ENOTFOUND received */export var errorProductResourcesNotFound$=errorProductResources$.filter(function(_ref10){var action=_ref10.action;return action.errorCode===ENOTFOUND;});export var cachedProductReceived$=main$.filter(function(_ref11){var action=_ref11.action;return action.type===RECEIVE_PRODUCT_CACHED;}).distinctUntilChanged();export var receivedVisibleProduct$=productReceived$.merge(cachedProductReceived$).filter(function(_ref12){var action=_ref12.action,getState=_ref12.getState;var route=getCurrentRoute(getState());if(typeof action.productData==='undefined'||typeof action.productData.id==='undefined'){return false;}if(!route.params.productId&&!route.state.productId){return false;}if(route.state.productId){return action.productData.id===route.state.productId;}return action.productData.id===hex2bin(route.params.productId);});/** * Emits when a product page was initially opened and its data is present. */export var productIsReady$=productWillEnter$// Take care that the stream only emits when underlying streams emit within the correct order. .switchMap(function(){return receivedVisibleProduct$.first();}).merge(productRouteReappeared$);/** Dispatched when ERROR_PRODUCT ENOTFOUND of visible product is received */export var visibleProductNotFound$=errorProductNotFound$.withLatestFrom(routeWillEnter$).filter(function(_ref13){var _ref14=_slicedToArray(_ref13,2),errorAction=_ref14[0],routeAction=_ref14[1];return routeAction.action.route.pattern===ITEM_PATTERN&&errorAction.action.productId===hex2bin(routeAction.action.route.params.productId);}).map(function(_ref15){var _ref16=_slicedToArray(_ref15,1),errorAction=_ref16[0];return errorAction;});export var variantDidChange$=variantWillUpdate$// Take care that the stream only emits when underlying streams emit within the correct order. .switchMap(function(){return receivedVisibleProduct$.first().switchMap(function(data){var _data$action$productD=data.action.productData,id=_data$action$productD.id,baseProductId=_data$action$productD.baseProductId;var variantId=baseProductId!==null?id:null;var baseProduct=getBaseProduct(data.getState(),{variantId:variantId});if(baseProduct===null){/** * A PDP with a variant product was opened, but the base product is not fetched yet. * So emitting of the stream is postponed till the data is present. */return productReceived$;}return Observable.of(data);});});export var productRelationsReceived$=main$.filter(function(_ref17){var action=_ref17.action;return action.type===RECEIVE_PRODUCT_RELATIONS;});export var productNotAvailable$=main$.filter(function(_ref18){var action=_ref18.action;return action.type===PRODUCT_NOT_AVAILABLE;});/** * Emits when product results has been received. */export var productsReceived$=main$.filter(function(_ref19){var action=_ref19.action;return action.type===RECEIVE_PRODUCTS;});/** * Emits when product results has been received from cache. */export var productsReceivedCached$=main$.filter(function(_ref20){var action=_ref20.action;return action.type===RECEIVE_PRODUCTS_CACHED;});/** * Buffer PROVIDE_PRODUCT action and map to have array of requested products * @type {Observable} */export var fetchProductsRequested$=main$.filter(function(_ref21){var action=_ref21.action;return action.type===PROVIDE_PRODUCT;}).bufferTime(PROVIDE_PRODUCT_BUFFER_TIME).filter(function(actions){return actions.length>0;}).map(function(actions){var productIds=actions.map(function(_ref22){var productId=_ref22.action.productId;return productId;});return _extends({},actions[0],{action:_extends({},actions[0].action,{productId:productIds})});});