@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
24 lines • 2.72 kB
JavaScript
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 appConfig from'@shopgate/pwa-common/helpers/config';import{generateResultHash}from'@shopgate/pwa-common/helpers/redux';import{bin2hex,hex2bin}from'@shopgate/pwa-common/helpers/data';import{ITEM_PATH,PROPERTIES_FILTER_BLACKLIST,PROPERTIES_FILTER_WHITELIST,PRODUCT_RELATIONS_DEFAULT_LIMIT}from"../constants";import{SHOPGATE_CATALOG_GET_PRODUCT_RELATIONS}from"../constants/Pipelines";/**
* Reads the setting for product properties whitelisting/blacklisting
* and filters the properties accordingly.
* @param {Array} properties Array of properties with label and value attributes.
* @return {Array} Filtered properties.
*/export var filterProperties=function filterProperties(properties){// If there is no setting, we won't filter
if(!appConfig||!appConfig.productPropertiesFilter){return properties;}return properties.filter(function(_ref){var label=_ref.label,code=_ref.code;var inList=!!appConfig.productPropertiesFilter.properties.find(function(p){return p===code||p===label;});if(appConfig.productPropertiesFilter.type===PROPERTIES_FILTER_WHITELIST){// Show item if allowed in whitelist
return inList;}if(appConfig.productPropertiesFilter.type===PROPERTIES_FILTER_BLACKLIST){// Hide item if not allowed in blacklist
return!inList;}return true;});};/**
* Generates getProductRelations hash based on productId, type and given limit.
* It is used in both action and selectors.
* @param {string} productId Product Id.
* @param {string} type Type - see `../constants`.
* @param {number} limit Limit.
* @returns {string}
*/export var generateProductRelationsHash=function generateProductRelationsHash(_ref2){var productId=_ref2.productId,type=_ref2.type,_ref2$limit=_ref2.limit,limit=_ref2$limit===void 0?PRODUCT_RELATIONS_DEFAULT_LIMIT:_ref2$limit;return generateResultHash({pipeline:SHOPGATE_CATALOG_GET_PRODUCT_RELATIONS,productId:productId,type:type,limit:limit},false,false);};/**
* Generate product route for navigation.
* @param {string} id Product Id.
* @returns {string}
*/export var getProductRoute=function getProductRoute(id){return"".concat(ITEM_PATH,"/").concat(bin2hex(id));};/**
* Transform product route.
* @param {Object} route item route to transform.
* @returns {Object}
*/export var transformRoute=function transformRoute(route){if(!route.params.productId||route.state.productId){return route;}return _extends({},route,{state:_extends({},route.state,{productId:hex2bin(route.params.productId)})});};