@shopgate/engage
Version:
Shopgate's ENGAGE library.
95 lines • 9.81 kB
JavaScript
function _typeof(obj){if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj;};}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj;};}return _typeof(obj);}import{createSelector}from'reselect';import{logger}from'@shopgate/pwa-core/helpers';import{filterProperties}from"./helpers";/**
* The following block of selectors was copied over from @shopgate/pwa-common-commerce/product
* to prevent circular dependencies.
*/ /**
* Retrieves the product state from the store.
* @param {Object} state The current application state.
* @return {Object} The product state.
*/export var getProductState=function getProductState(state){return state.product||{};};/**
* Selects the product properties state.
* @param {Object} state The current application state.
* @param {Object} props The component props.
* @return {Object} The product properties state.
*/export var getProductPropertiesState=createSelector(getProductState,function(state){return state.propertiesByProductId||{};});/**
* Selects all products from the store.
* @param {Object} state The current application state.
* @return {Object} The collection of products.
*/export var getProducts=createSelector(getProductState,function(state){return state.productsById||{};});/**
* Retrieves the id of the current selected product from the component props. When the props
* contain a variant id it will return this one instead of the product id.
* @param {Object} state The current application state.
* @param {Object} [props] The component props.
* @return {string|null} The id of the current product.
*/export var getProductId=function getProductId(state,props){if(!state){return null;}if(typeof props==='undefined'){/**
* Before PWA 6.0 some product selectors relied on a "currentProduct" state which doesn't exist
* anymore. Their successors require a props object which contains a productId or a variantId.
* To support debugging an error will be logged, if the props are missing at invocation.
*/logger.error('getProductId() needs to be called with a props object that includes a productId.');return null;}// Since a variantId can have falsy values, we need an "undefined" check here.
if(typeof props.variantId!=='undefined'&&props.variantId!==null){return props.variantId;}return props.productId||null;};/**
* Retrieves the product data for the passed productId from the store.
* @param {Object} state The current application state.
* @param {Object} props The component props.
* @return {Object} The current product.
*/export var getProduct=createSelector(getProducts,getProductId,function(products,productId){var _ref=products[productId]||{},productData=_ref.productData;return productData||null;});/**
* Retrieves a product by id from state. Different to getProduct() which returns the product
* entity data if available, this selector returns the pure state entry for a given productId.
* So the expires and the isFetching property is processable.
* @param {Object} state The current application state.
* @param {Object} props The component props.
* @return {Object|null} The dedicated product.
*/export var getProductById=createSelector(getProducts,function(state,props){return props;},function(products,props){if(_typeof(props)!=='object'){logger.warn('Invocation of getProductById() with a productId will be deprecated soon. Please provide a props object.');return products[props]||null;}if(!props.productId){return null;}return products[props.productId]||null;});export var getProductDataById=createSelector(getProductById,function(product){return product?product.productData:undefined;});/**
* Determines a baseProductId for the products which are referenced within the props.
* When a variantId is passed, the selector will return the id of the related base product.
* @param {Object} state The current application state.
* @param {Object} props The component props.
* @return {string|null}
*/export var getBaseProductId=createSelector(getProduct,function(_){var props=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};return props.productId;},function(_){var props=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};return props.variantId;},function(product,productId,variantId){if(!product){// Return the productId when both ids are present, but no variant product is available yet.
if(typeof productId!=='undefined'&&typeof variantId!=='undefined'){return productId;}return null;}// First try to determine a baseProductId for a selected product
var _product$baseProductI=product.baseProductId,baseProductId=_product$baseProductI===void 0?null:_product$baseProductI;return baseProductId||product.id;});/**
* Retrieves the base product data for the passed productId from the store.
* @param {Object} state The current application state.
* @returns {Object|null} The current product.
*/export var getBaseProduct=createSelector(getProducts,getBaseProductId,function(products,baseProductId){if(!baseProductId){return null;}var _ref2=products[baseProductId]||{},_ref2$productData=_ref2.productData,productData=_ref2$productData===void 0?null:_ref2$productData;return productData;});/**
* END of the copied block
*/ /**
* Selects the is fetching state of the active product.
*/export var getProductIsFetching=createSelector(getProductId,getProducts,function(productId,products){var _products$productId;return((_products$productId=products[productId])===null||_products$productId===void 0?void 0:_products$productId.isFetching)||false;});/**
* Creates the selector to get a product's properties from the state filtered via
* positive / negative list.
* @returns {Function}
*/export function makeGetProductProperties(){return createSelector(getProductPropertiesState,getProductId,function(properties,productId){var entry=properties[productId];if(!entry||!entry.properties){return null;}return filterProperties(entry.properties);});}/**
* Creates the selector to get a product's properties from the state without filtering.
* @returns {Function}
*/export var makeGetProductPropertiesUnfiltered=function makeGetProductPropertiesUnfiltered(){return createSelector(getProductId,getProductPropertiesState,function(productId,properties){var entry=properties[productId];if(!entry||entry.isFetching||typeof entry.properties==='undefined'){return null;}return entry.properties;});};/**
* Creates the selector to get a product's effectivity dates.
* @returns {Function}
*/export function makeGetProductEffectivityDates(){return createSelector(getProduct,function(product){if(!product){return null;}var _product$startDate=product.startDate,startDate=_product$startDate===void 0?null:_product$startDate,_product$endDate=product.endDate,endDate=_product$endDate===void 0?null:_product$endDate;return startDate||endDate?{startDate:startDate,endDate:endDate}:null;});}/**
* Creates a selector to return product characteristics.
* @returns {Function}
*/export function makeGetProductCharacteristics(){return createSelector(getProductDataById,function(product){return!product||!product.characteristics?null:product.characteristics;});}/**
* Creates a selector to return product featured media.
* @returns {Function}
*/export function makeGetProductFeaturedMedia(){return createSelector(getProduct,function(product){if(!product){return null;}return!product.featuredMedia?null:product.featuredMedia;});}/**
* Creates a selector to indicate if a product is active.
*
* @param {boolean} [returnNullIfProductMissing=false] - Flag to determine the return value when
* the product is not found. If set to `true`, the selector will return `null` when the product
* is not found. If `false`, it will return `false`.
*
* @returns {Function}
*/export var makeIsProductActive=function makeIsProductActive(){var returnNullIfProductMissing=arguments.length>0&&arguments[0]!==undefined?arguments[0]:false;return createSelector(getProduct,function(product){if(!product){return returnNullIfProductMissing?null:false;}return(product===null||product===void 0?void 0:product.active)||false;});};/**
* Creates a selector to indicate if the base product is active.
*
* @param {boolean} [returnNullIfProductMissing=false] - Flag to determine the return value when
* the product is not found. If set to `true`, the selector will return `null` when the product
* is not found. If `false`, it will return `false`.
*
* @returns {Function}
*/export var makeIsBaseProductActive=function makeIsBaseProductActive(){var returnNullIfProductMissing=arguments.length>0&&arguments[0]!==undefined?arguments[0]:false;return createSelector(getBaseProduct,function(baseProduct){if(!baseProduct){return returnNullIfProductMissing?null:false;}return(baseProduct===null||baseProduct===void 0?void 0:baseProduct.active)||false;});};/**
* Creates a selector to get the property of a product based on a given label
* @returns {Function}
*/export var makeGetCurrentProductPropertyByLabel=function makeGetCurrentProductPropertyByLabel(){var getProductPropertiesUnfiltered=makeGetProductPropertiesUnfiltered();return createSelector(getProductPropertiesUnfiltered,function(state,props){return props.widgetSettings;},function(currentProductProperties,widgetSettings){if(!currentProductProperties||!widgetSettings||!widgetSettings.propertyLabel){return null;}return currentProductProperties.find(function(_ref3){var label=_ref3.label;return label===widgetSettings.propertyLabel;});});};/**
* Create a selector to retrieve the product type.
* @returns {Function}
*
*/export var makeGetProductType=function makeGetProductType(){return createSelector(getProduct,function(product){return product===null||product===void 0?void 0:product.type;});};