UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

33 lines 4.66 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,getProductId,getProductState,getProductCurrency}from"./product";import{OPTION_TYPE_SELECT,OPTION_TYPE_TEXT}from"../constants";/** * Retrieves the product options state. * @param {Object} state The application state. * @returns {Object} The product options state. */var getProductOptionsState=createSelector(getProductState,function(state){return state.optionsByProductId;});/** * Finds a product option item by the option id and item id. * @param {Object} options All available options. * @param {Object} optionId The Id of the option. * @param {Object} itemId The Id of the item. * @returns {Object} */var findProductOptionItem=function findProductOptionItem(options,optionId,itemId){return options.find(function(opt){return opt.id===optionId;}).values.find(function(item){return item.id===itemId;});};/** * Retrieves the current products options. * @param {Object} state The application state. * @returns {Object} The product options. */export var getRawProductOptions=createSelector(getProductId,getProductOptionsState,function(productId,productOptionsState){var productOptions=productOptionsState[productId];if(!productOptions||productOptions.isFetching){return null;}return productOptions.options;});// TODO: This needs to be optimized! var getOptionItems=createSelector(function(options){return options;},function(options,values){return values;},function(options,values,option){return option;},function(options,values,option,selected){return selected;},function(options,values,option,selected,currency){return currency;},function(options){var values=arguments.length>1&&arguments[1]!==undefined?arguments[1]:[];var option=arguments.length>2?arguments[2]:undefined;var selected=arguments.length>3?arguments[3]:undefined;var currency=arguments.length>4?arguments[4]:undefined;return values.map(function(value){// Add prices to each item that are relative to the current total product price. if(!selected){return{label:value.label,currency:currency,value:value.id,// Price modifier and difference are equal, when nothing is selected. price:value.unitPriceModifier,priceDifference:value.unitPriceModifier};}var _findProductOptionIte=findProductOptionItem(options,option.id,selected),_findProductOptionIte2=_findProductOptionIte.unitPriceModifier,siblingPrice=_findProductOptionIte2===void 0?0:_findProductOptionIte2;return{label:value.label,currency:currency,value:value.id,// Price which affects the unit price. price:value.unitPriceModifier,// Difference to the currently selected sibling. priceDifference:value.unitPriceModifier-siblingPrice};});});/** * Retrieves the current products options and transforms it to the correct data structure. * @param {Object} state The application state. * @returns {Array} The product options. */export var getProductOptions=createSelector(getProductCurrency,function(state){var props=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};return props.currentOptions;},getRawProductOptions,validateSelectorParams(function(currency,currentOptions,options){return options.map(function(option){return _extends({id:option.id,label:option.label,type:option.type,value:currentOptions[option.id]},option.type===OPTION_TYPE_SELECT&&{items:getOptionItems(options,option.values,option,currentOptions[option.id],currency)},{},option.type===OPTION_TYPE_TEXT&&{info:option.annotation,required:!!option.required,price:option.unitPriceModifier});})// Move select type options on top, keep the rest .sort(function(a,b){if(a.type===b.type){return 0;}if(a.type==='select'){return-1;}return b.type==='select'?1:0;});}));/** * Checks if the product has any options. * @param {Object} state The application state. * @returns {boolean} */export var hasProductOptions=createSelector(getProduct,function(product){if(!product){return false;}return product.flags.hasOptions;});/** * Checks if all product options for the product are set * @param {Object} state The application state. * @returns {boolean} */export var areProductOptionsSet=createSelector(getRawProductOptions,function(state){var props=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};return props.options||false;},validateSelectorParams(function(options,currentOptions){return options.length===Object.keys(currentOptions).length;}));