UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

56 lines • 8.7 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 queryString from'query-string';import{ACTION_PUSH,ACTION_REPLACE}from'@virtuous/conductor';import{main$}from'@shopgate/pwa-common/streams/main';import{routeWillEnter$,routeDidEnter$,routeWillLeave$,routeDidLeave$,navigate$}from'@shopgate/pwa-common/streams/router';import{getCurrentPathname}from'@shopgate/pwa-common/selectors/router';import{pushNotificationOpened$}from'@shopgate/pwa-common/streams/app';import{INDEX_PATH_DEEPLINK,INDEX_PATH}from'@shopgate/pwa-common/constants/RoutePaths';import{getIsFetching}from"../selectors";import{CART_PATH,REQUEST_CART,RECEIVE_CART,ERROR_CART,ADD_PRODUCTS_TO_CART,SUCCESS_ADD_PRODUCTS_TO_CART,ERROR_ADD_PRODUCTS_TO_CART,UPDATE_PRODUCTS_IN_CART,SUCCESS_UPDATE_PRODUCTS_IN_CART,ERROR_UPDATE_PRODUCTS_IN_CART,DELETE_PRODUCTS_FROM_CART,SUCCESS_DELETE_PRODUCTS_FROM_CART,ERROR_DELETE_PRODUCTS_FROM_CART,ADD_COUPONS_TO_CART,SUCCESS_ADD_COUPONS_TO_CART,ERROR_ADD_COUPONS_TO_CART,DELETE_COUPONS_FROM_CART,ERROR_DELETE_COUPONS_FROM_CART,SUCCESS_DELETE_COUPONS_FROM_CART,DEEPLINK_CART_ADD_PRODUCT_PATTERN,SET_FULFILLMENT_SLOT}from"../constants";/** * Specialized stream to handle push notifications that redirect to index screen with a coupon code. * Default logic would dispatch a historyReset in that case without link in the action payload, * so we need to hook onto the pushNotificationOpen stream to inspect the link. * @type {Observable} */var pushNotificationWithIndexCouponOpened$=pushNotificationOpened$.filter(function(_ref){var action=_ref.action;if(!action.link){// stop execution when push contains no link return false;}var _action$link$split=action.link.split('?'),_action$link$split2=_slicedToArray(_action$link$split,1),pathname=_action$link$split2[0];if(pathname!==INDEX_PATH&&pathname!==INDEX_PATH_DEEPLINK){// stop execution when link doesn't reference index path return false;}var parsed=queryString.parseUrl(action.link);if(!parsed||!parsed.query||!parsed.query.coupon){// stop execution when link doesn't contain a coupon return false;}return true;}).map(function(input){return _extends({},input,{action:{historyAction:ACTION_PUSH,route:{query:{coupon:queryString.parseUrl(input.action.link).query.coupon}}}});});/** * @type {Observable} */export var routeWithCouponWillEnter$=routeWillEnter$.merge(pushNotificationWithIndexCouponOpened$).filter(function(_ref2){var action=_ref2.action;return[ACTION_PUSH,ACTION_REPLACE].includes(action.historyAction)&&action.route.query.coupon;});/** * @type {Observable} */export var cartWillEnter$=routeWillEnter$.filter(function(_ref3){var action=_ref3.action;return action.route.pattern===CART_PATH;});/** * @type {Observable} */export var cartDidEnter$=routeDidEnter$.filter(function(_ref4){var action=_ref4.action;return action.route.pattern===CART_PATH;});/** * @type {Observable} */export var cartWillLeave$=routeWillLeave$.filter(function(_ref5){var action=_ref5.action;return action.route.pattern===CART_PATH;});/** * @type {Observable} */export var cartDidLeave$=routeDidLeave$.filter(function(_ref6){var action=_ref6.action;return action.route.pattern===CART_PATH;});/** * @type {Observable} */export var cartRequesting$=main$.filter(function(_ref7){var action=_ref7.action;return action.type===REQUEST_CART;});export var cartWillEnterIdle$=cartWillEnter$.filter(function(_ref8){var getState=_ref8.getState;return!getIsFetching(getState());});/** * @type {Observable} */export var cartReceived$=main$.filter(function(_ref9){var action=_ref9.action;return action.type===RECEIVE_CART||action.type===ERROR_CART;});/** * Gets triggered when the user tried to add a coupon to the cart. * @type {Observable} */export var couponsAdded$=main$.filter(function(_ref10){var action=_ref10.action;return action.type===ADD_COUPONS_TO_CART;});/** * Gets triggered when the user tried to add a coupon to the cart. * @type {Observable} */export var couponsDeleted$=main$.filter(function(_ref11){var action=_ref11.action;return action.type===DELETE_COUPONS_FROM_CART;});/** * Gets triggered when the user tried to add a coupon to the cart. * @type {Observable} */export var couponsUpdated$=main$.filter(function(_ref12){var action=_ref12.action;return action.type===SUCCESS_ADD_COUPONS_TO_CART||action.type===ERROR_ADD_COUPONS_TO_CART||action.type===SUCCESS_DELETE_COUPONS_FROM_CART||action.type===ERROR_DELETE_COUPONS_FROM_CART;});/** * Gets triggered when the user tried to add a product to the cart. * @type {Observable} */export var productsAdded$=main$.filter(function(_ref13){var action=_ref13.action;return action.type===ADD_PRODUCTS_TO_CART;});/** * Gets triggered when the user tried to add a product to the cart. * @type {Observable} */export var productsModified$=main$.filter(function(_ref14){var action=_ref14.action;return action.type===UPDATE_PRODUCTS_IN_CART;});/** * Gets triggered when the user tried to add a product to the cart. * @type {Observable} */export var productsDeleted$=main$.filter(function(_ref15){var action=_ref15.action;return action.type===DELETE_PRODUCTS_FROM_CART;});/** * Gets triggered when the cart has been updated while the cart route was visible. * @type {Observable} */export var cartUpdatedWhileVisible$=cartReceived$.filter(function(_ref16){var getState=_ref16.getState;return getCurrentPathname(getState())===CART_PATH;});/** * Gets triggered when the user tried to add a coupon to the cart. * @type {Observable} */export var productsUpdated$=main$.filter(function(_ref17){var action=_ref17.action;return action.type===SUCCESS_ADD_PRODUCTS_TO_CART||action.type===ERROR_ADD_PRODUCTS_TO_CART||action.type===SUCCESS_UPDATE_PRODUCTS_IN_CART||action.type===ERROR_UPDATE_PRODUCTS_IN_CART||action.type===SUCCESS_DELETE_PRODUCTS_FROM_CART||action.type===ERROR_DELETE_PRODUCTS_FROM_CART;});/** * Gets triggered when the remote cart was updated. * @type {Observable} */export var remoteCartDidUpdate$=main$.filter(function(_ref18){var action=_ref18.action;return action.type===SUCCESS_ADD_PRODUCTS_TO_CART||action.type===ERROR_ADD_PRODUCTS_TO_CART||action.type===SUCCESS_UPDATE_PRODUCTS_IN_CART||action.type===ERROR_UPDATE_PRODUCTS_IN_CART||action.type===SUCCESS_DELETE_PRODUCTS_FROM_CART||action.type===ERROR_DELETE_PRODUCTS_FROM_CART||action.type===SUCCESS_ADD_COUPONS_TO_CART||action.type===ERROR_ADD_COUPONS_TO_CART||action.type===SUCCESS_DELETE_COUPONS_FROM_CART||action.type===ERROR_DELETE_COUPONS_FROM_CART;});/** * Gets triggered whenever any cart update request fails. * @type {Observable} */export var cartUpdateFailed$=main$.filter(function(_ref19){var action=_ref19.action;return action.type===ERROR_ADD_PRODUCTS_TO_CART||action.type===ERROR_UPDATE_PRODUCTS_IN_CART||action.type===ERROR_DELETE_PRODUCTS_FROM_CART||action.type===ERROR_ADD_COUPONS_TO_CART||action.type===ERROR_DELETE_COUPONS_FROM_CART;});/** * Gets triggered when /cart_add_product/123/COUPON route is navigated * @type {Observable} */export var routeAddProductNavigate$=navigate$.filter(function(_ref20){var _ref20$action$params=_ref20.action.params,_ref20$action$params2=_ref20$action$params===void 0?{}:_ref20$action$params,_ref20$action$params3=_ref20$action$params2.pathname,pathname=_ref20$action$params3===void 0?'':_ref20$action$params3;return pathname.includes(DEEPLINK_CART_ADD_PRODUCT_PATTERN.split('/')[1]);}).map(function(params){var _params$action$params=params.action.params.pathname.split('/'),_params$action$params2=_slicedToArray(_params$action$params,4),productId=_params$action$params2[2],couponCode=_params$action$params2[3];return _extends({},params,{action:_extends({},params.action,{productId:decodeURIComponent(productId),couponCode:couponCode?decodeURIComponent(couponCode):undefined})});});export var setFulfillmentSlot$=main$.filter(function(_ref21){var action=_ref21.action;return action.type===SET_FULFILLMENT_SLOT;});