UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

36 lines 3.85 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{validateSelectorParams}from'@shopgate/pwa-common/helpers/data';import{getProduct,getProductUnitPrice,hasBaseProductVariants,isVariantSelected}from"./product";import{getRawProductOptions,hasProductOptions,areProductOptionsSet}from"./options";/** * Calculates the additional price for the current product. * @param {Object} state The application state. * @returns {number} */export var getProductPriceAddition=createSelector(function(state){var props=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};return props.options;},getRawProductOptions,validateSelectorParams(function(options,productOptions){return Object.keys(options).map(function(optionId){var itemId=options[optionId];var productOption=productOptions.find(function(item){return item.id===optionId;});if(productOption.type==='select'){var optionItems=productOptions.find(function(item){return item.id===optionId;}).values;return optionItems.find(function(item){return item.id===itemId;}).unitPriceModifier;}if(productOption.type==='text'&&itemId){return productOption.unitPriceModifier;}return 0;})// Sum up all active option item modifiers to calculate the additional price of the product. .reduce(function(a,b){return a+b;},0);},// If product has no options return 0 0));/** * Calculates the total price by summing up additional product option costs. * @param {Object} state The application state. * @returns {number} */export var getProductTotalPrice=createSelector(getProductUnitPrice,getProductPriceAddition,validateSelectorParams(function(basePrice,priceAddition){return basePrice+priceAddition;}));/** * Checks if the full price is already available. * When a product has variants with different prices the full price is not available. * If the children is selected the full price is available. * @param {Object} state The application state. * @returns {boolean} The product options. */export var isFullPriceAvailable=createSelector(hasBaseProductVariants,isVariantSelected,hasProductOptions,areProductOptionsSet,function(hasVariants,isChildrenSelected,hasOptions,areOptionsSet){// If product has neither variants nor options the full price is available. if(!hasVariants&&!hasOptions){return true;}/** * If the product has variants and options, * the full price is only available if options and children are set. */if(hasVariants&&hasOptions){return isChildrenSelected&&areOptionsSet;}/** * If the product has only options, * the full price is only available if the options are set. */if(!hasVariants&&hasOptions){return areOptionsSet;}/** * If the product has only variants, * the full price is only available if the children is set. */if(hasVariants&&!hasOptions){return isChildrenSelected;}return false;});/** * Retrieves the current product and adds the total price and manipulates the min price * to be always 0 when it is not needed. * @param {Object} state The application state. * @returns {Object} The extended product data. */export var getCalculatedProduct=createSelector(getProduct,getProductTotalPrice,getProductPriceAddition,validateSelectorParams(function(product,totalPrice,addition){return _extends({},product,{price:_extends({},product.price,{modifier:addition,totalPrice:totalPrice})});}));/** * Retrieves the current product price data. * @param {Object} state The current application state. * @return {Object|null} */export var getProductPrice=createSelector(getCalculatedProduct,function(product){if(!product||!product.price){return null;}return product.price;});