UNPKG

@shopgate/engage

Version:
34 lines 7.93 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);}import{createSelector}from'reselect';import{hasProductVariants,isProductOrderable}from'@shopgate/pwa-common-commerce/product/selectors/product';import{makeIsProductActive,makeIsBaseProductActive}from'@shopgate/engage/product/selectors/product';import{CART_ITEM_TYPE_PRODUCT}from'@shopgate/pwa-common-commerce/cart/constants';import{DIRECT_SHIP}from"../locations";import{getLocationsStorage,makeIsRopeProductOrderable,makeIsLocationFulfillmentMethodEnabled,getPreferredLocation,getPreferredFulfillmentMethod}from"../locations/selectors";import{makeGetEnabledFulfillmentMethods,makeUseLocationFulfillmentMethods}from"../core";/** * ######## * The following block of selectors was copied over from @shopgate/pwa-common-commerce/cart * to prevent circular dependencies. * ######## */ /** * Selects the cart data from the store. * @param {Object} state The current state. * @return {Object} */var getCart=function getCart(state){return state.cart.data;};/** * Selects the cartItems from the current cart in the state. * @param {Object} state The current state. * @return {Array} The cartItems. */var cartItemsSelector=function cartItemsSelector(state){return state.cart.data.items;};/** * Selects all items from the cart. * @param {Object} state The current state. * @return {Array} The cart items. */var getCartItems=createSelector(cartItemsSelector,function(cartItems){return cartItems;});/** * Selects the products from the cart. * @param {Object} state The current state. * @return {Object[]} The cart products. */export var getCartProducts=createSelector(cartItemsSelector,function(cartItems){return cartItems.filter(function(item){return item.type===CART_ITEM_TYPE_PRODUCT;});});/** * ##### * END of the copied block * ##### */ /** * Creates a selector that determines if a cart button is supposed to be disabled. * @returns {Function} */export function makeIsAddToCartButtonDisabled(){var isProductActive=makeIsProductActive();var isBaseProductActive=makeIsBaseProductActive();var isRopeProductOrderable=makeIsRopeProductOrderable(function(state,props){var _getPreferredLocation;return(_getPreferredLocation=getPreferredLocation(state,props))===null||_getPreferredLocation===void 0?void 0:_getPreferredLocation.code;},function(state,props){return props.variantId||props.productId||null;});var getEnabledMerchantFulfillmentMethods=makeGetEnabledFulfillmentMethods();var isLocationFulfillmentMethodEnabled=makeIsLocationFulfillmentMethodEnabled(function(state,props){var _getPreferredLocation2;return(_getPreferredLocation2=getPreferredLocation(state,props))===null||_getPreferredLocation2===void 0?void 0:_getPreferredLocation2.code;},function(state,props){return getPreferredFulfillmentMethod(state,props);});var useLocationFulfillmentMethods=makeUseLocationFulfillmentMethods();return createSelector(getEnabledMerchantFulfillmentMethods,getPreferredFulfillmentMethod,hasProductVariants,isProductActive,isBaseProductActive,isProductOrderable,isRopeProductOrderable,isLocationFulfillmentMethodEnabled,useLocationFulfillmentMethods,function(merchantFulfillmentMethods,userLocationFulfillmentMethod,productHasVariants,productActive,baseProductActive,productOrderable,ropeProductOrderable,locationFulfillmentMethodEnabled,locationFulfillmentMethodsUsed){if(!productActive||!baseProductActive){return true;}var isDirectShipSelected=!merchantFulfillmentMethods||!userLocationFulfillmentMethod||userLocationFulfillmentMethod===DIRECT_SHIP;if(isDirectShipSelected){return!productOrderable&&!productHasVariants;}if(locationFulfillmentMethodsUsed&&!locationFulfillmentMethodEnabled){return true;}return ropeProductOrderable===false&&!productHasVariants;});}export var hasLineItemPromotions=createSelector(getCartItems,function(cartItems){return cartItems.some(function(_ref){var _ref$appliedPromotion=_ref.appliedPromotions,appliedPromotions=_ref$appliedPromotion===void 0?[]:_ref$appliedPromotion;return appliedPromotions.length>0;});});export var getCoupons=createSelector(getCart,getCartItems,function(cart,cartItems){return((cart===null||cart===void 0?void 0:cart.coupons)||[]).filter(function(coupon){// Filter coupons which are assigned to line items var code=coupon===null||coupon===void 0?void 0:coupon.code;var lineItemCoupon=cartItems.find(function(cartItem){var lineItemPromotions=(cartItem===null||cartItem===void 0?void 0:cartItem.appliedPromotions)||[];var match=lineItemPromotions.find(function(promotion){var _promotion$coupon;var lineItemCouponCode=promotion===null||promotion===void 0?void 0:(_promotion$coupon=promotion.coupon)===null||_promotion$coupon===void 0?void 0:_promotion$coupon.code;return lineItemCouponCode===code;});return!!match;});return!lineItemCoupon;});});export var getAppliedPromotions=createSelector(getCart,function(cart){return(cart===null||cart===void 0?void 0:cart.appliedPromotions)||[];});export var getAppliedPromotionsWithoutCoupons=createSelector(getAppliedPromotions,function(promotions){return promotions.filter(function(_ref2){var coupon=_ref2.coupon;return coupon===null;});});export var getPromotionCoupons=createSelector(getCoupons,getAppliedPromotions,function(coupons,promotions){return coupons.map(function(coupon){var _coupon$promotion;var code=coupon===null||coupon===void 0?void 0:(_coupon$promotion=coupon.promotion)===null||_coupon$promotion===void 0?void 0:_coupon$promotion.code;var promotion=promotions.find(function(promotionEntry){return promotionEntry.code===code;});return _extends({},coupon,{promotion:_extends({},(coupon===null||coupon===void 0?void 0:coupon.promotion)||{},{},promotion)});});});export var hasDirectShipItems=createSelector(getCartItems,function(cartItems){return cartItems.some(function(item){return!(item===null||item===void 0?void 0:item.fulfillment);});});export var isDirectShipOnly=createSelector(getCartItems,function(cartItems){return cartItems.every(function(item){return!(item===null||item===void 0?void 0:item.fulfillment);});});/** * Returns the currently configured fulfillment slot. * Takes the info from either the first cart item * or from temp cart state. */export var getActiveFulfillmentSlot=createSelector(getCartItems,function(state){return getCart(state).activeFulfillmentSlot;},function(cartItems,fulfillmentSlot){var _cartItems$find,_cartItems$find$,_cartItems$find$$fulf;var firstSlotConfig=(_cartItems$find=cartItems.find(function(cartItem){return cartItem===null||cartItem===void 0?void 0:cartItem.fulfillment;}))===null||_cartItems$find===void 0?void 0:(_cartItems$find$=_cartItems$find[0])===null||_cartItems$find$===void 0?void 0:(_cartItems$find$$fulf=_cartItems$find$.fulfillment)===null||_cartItems$find$$fulf===void 0?void 0:_cartItems$find$$fulf.fulfillmentSlot;if(!firstSlotConfig){return fulfillmentSlot;}return firstSlotConfig;});export var getActiveFulfillmentSlotLocationCode=createSelector(getActiveFulfillmentSlot,getLocationsStorage,function(activeFulfillmentSlot,storage){var slotId=activeFulfillmentSlot===null||activeFulfillmentSlot===void 0?void 0:activeFulfillmentSlot.id;if(!slotId){return null;}var locationId=Object.keys(storage.fulfillmentSlotsByLocation).find(function(locationCode){return!!storage.fulfillmentSlotsByLocation[locationCode].find(function(_ref3){var id=_ref3.id;return id===slotId;});});return locationId||null;});export var checkActiveFulfillmentSlotBelongsToPreferredLocation=createSelector(getPreferredLocation,getActiveFulfillmentSlotLocationCode,function(location,activeFulfillmentSlotLocationCode){var locationCode=location===null||location===void 0?void 0:location.code;if(!locationCode){return false;}return locationCode===activeFulfillmentSlotLocationCode;});