@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
105 lines • 8.72 kB
JavaScript
import{createSelector}from'reselect';import{getBaseProductMetadata}from"../../product/selectors/product";import{getSelectedVariantMetadata}from"../../product/selectors/variants";import{getRawProductOptions,hasProductOptions,areProductOptionsSet}from"../../product/selectors/options";import{CART_ITEM_TYPE_PRODUCT,CART_ITEM_TYPE_COUPON,CART_TOTALS_TYPE_SUB,CART_TOTALS_TYPE_GRAND,CART_TOTALS_TYPE_DISCOUNT,CART_TOTALS_TYPE_SHIPPING,CART_TOTALS_TYPE_TAX}from"../constants";import{getDisplayedProductQuantity}from"../helpers/quantity";/**
* Selects the coupon field data from the store.
* @param {Object} state The current state.
* @return {Object}
*/var getCouponField=function getCouponField(state){return state.cart.couponField;};/**
* Selects the cart data from the store.
* @param {Object} state The current state.
* @return {Object}
*/export 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 the total amounts stack from the cart data.
* @param {Object} state The current application state.
* @return {Object} The total amount stack.
*/var getTotals=createSelector(getCart,function(cart){return cart.totals;});/**
* Selects all items from the cart.
* @param {Object} state The current state.
* @return {Array} The cart items.
*/export var getCartItems=createSelector(cartItemsSelector,function(cartItems){return cartItems;});/**
* Selects the cartItem of the given Id.
* @param {Object} state The current state.
* @param {string} params.cartItemId Id of the cartItem.
* @return {Array} The cart items.
*/export var getCartItemById=createSelector(function(state,_ref){var cartItemId=_ref.cartItemId;return cartItemId;},getCartItems,function(cartItemId,cartItems){return cartItems.find(function(_ref2){var id=_ref2.id;return id===cartItemId;})||null;});/**
* 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;});});/**
* Selects the coupons from the cart.
* @param {Object} state The current state.
* @return {Array} The cart coupons.
*/export var getCartCoupons=createSelector(cartItemsSelector,function(cartItems){return cartItems.filter(function(item){return item.type===CART_ITEM_TYPE_COUPON;});});/**
* Calculates the current number of product in the cart.
* @param {Object} state The current state.
* @return {number} The current number of products within the cart.
*/export var getCartProductCount=createSelector(cartItemsSelector,function(cartItems){return(cartItems===null||cartItems===void 0?void 0:cartItems.reduce(function(count,cartItem){// We don't count coupons etc.
if(cartItem.type!==CART_ITEM_TYPE_PRODUCT){return count;}return count+getDisplayedProductQuantity(cartItem);},0))||0;});/**
* Selects the pending product count from the cart.
* @param {Object} state The current application state.
* @return {number}
*/export var getProductPendingCount=createSelector(getCart,function(cart){return cart.productPendingCount;});/**
* Selects the total, theoretical, number of products in the cart.
* @param {Object} state The current application state.
* @return {number}
*/export var getCartProductDisplayCount=createSelector(getCartProductCount,getProductPendingCount,function(productCount,pendingCount){return productCount+pendingCount;});/**
* Selects the orderable status from the cart data.
* @param {Object} state The current application state.
* @return {boolean}
*/export var getOrderableStatus=createSelector(getCart,function(cart){return cart.isOrderable;});/**
* Selects the currency from the cart data.
* @param {Object} state The current application state.
* @return {string}
*/export var getCurrency=createSelector(getCart,function(cart){return cart.currency;});/**
* Selects the sub total.
* @param {Object} state The current application state.
* @return {string}
*/export var getSubTotal=createSelector(getTotals,function(totals){var _ref3=totals.find(function(total){return total.type===CART_TOTALS_TYPE_SUB;})||{},_ref3$amount=_ref3.amount,amount=_ref3$amount===void 0?0:_ref3$amount;return amount;});/**
* Selects the grand total.
* @param {Object} state The current application state.
* @returns {number}
*/export var getGrandTotal=createSelector(getTotals,function(totals){var _ref4=totals.find(function(total){return total.type===CART_TOTALS_TYPE_GRAND;})||{},_ref4$amount=_ref4.amount,amount=_ref4$amount===void 0?0:_ref4$amount;return amount;});/**
* Selects the grand total data.
* @param {Object} state The current application state.
* @returns {number}
*/export var getGrandTotalData=createSelector(getTotals,function(totals){var totalData=totals.find(function(total){return total.type===CART_TOTALS_TYPE_GRAND;})||null;return totalData;});/**
* Selects the shipping costs.
* @returns {Object|null}
*/export var getShippingCost=createSelector(getTotals,function(totals){return totals.find(function(total){return total.type===CART_TOTALS_TYPE_SHIPPING;})||null;});/**
* Selects the summed up shipping costs of the cart.
* @returns {number}
*/export var getShippingCosts=createSelector(getShippingCost,function(shippingCost){if(!shippingCost){return null;}var _shippingCost$amount=shippingCost.amount,amount=_shippingCost$amount===void 0?0:_shippingCost$amount;return amount;});/**
* Selects the tax value of the cart.
* @returns {Object}
*/export var getTax=createSelector(getTotals,function(totals){return totals.find(function(total){return total.type===CART_TOTALS_TYPE_TAX;})||null;});/**
* Selects applied discounts from the total amounts stack.
* @param {Object} state The current application state.
* @return {Object[]|null}
*/export var getDiscounts=createSelector(getTotals,function(totals){var discounts=totals.filter(function(total){return total.type===CART_TOTALS_TYPE_DISCOUNT;});return discounts.length?discounts:null;});/**
* Selects the discounts value of the cart.
* @returns {number}
*/export var getDiscountsAmount=createSelector(getDiscounts,function(discounts){if(!discounts){return 0;}return discounts.reduce(function(acc,d){return acc+d.amount;},0);});/**
* Selects the received messages from the cart.
* @param {Object} state The current application state.
* @return {Array}
*/export var getCartMessages=createSelector(getCart,function(cart){return cart.messages;});/**
* Selects the cart flags from the cart data.
* @return {Object}
*/export var getFlags=createSelector(getCart,function(_ref5){var flags=_ref5.flags;return flags||{};});/**
* Builds the data for the 'options' property of addProductsToCart pipeline request payload.
* @param {Object} state The application state.
* @returns {Object|null} The data if it was determinable, otherwise NULL.
*/export var getAddToCartOptions=createSelector(hasProductOptions,areProductOptionsSet,getRawProductOptions,function(state,props){return props.options;},function(hasOptions,areOptionsSet,options,currentOptions){// Check if options are ready to be added to a pipeline request.
if(!hasOptions||!areOptionsSet){return null;}// Create the data structure.
return Object.keys(currentOptions).map(function(id){var value=currentOptions[id];var _options$find=options.find(function(option){return option.id===id;}),type=_options$find.type;return{type:type,id:id,value:value};});});/**
* Builds the data for the 'metadata' property of addProductsToCart pipeline request payload.
* @returns {Object|null} The data if it was determinable, otherwise NULL.
*/export var getAddToCartMetadata=createSelector(getSelectedVariantMetadata,getBaseProductMetadata,function(variantMetadata,baseProductMetadata){return variantMetadata||baseProductMetadata||null;});/**
* Checks if the cart supports redemption of coupons.
* @return {boolean}
*/export var hasCouponSupport=createSelector(getFlags,function(_ref6){var coupons=_ref6.coupons;return typeof coupons==='boolean'?coupons:true;});/**
* Checks if the cart is fetching
* @return {boolean}
*/export var getIsFetching=createSelector(getCart,function(_ref7){var isFetching=_ref7.isFetching;return isFetching||false;});export var getCouponFieldError=createSelector(getCouponField,function(data){return(data===null||data===void 0?void 0:data.error)||'';});export var getCouponFieldValue=createSelector(getCouponField,function(data){return(data===null||data===void 0?void 0:data.value)||'';});