UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

27 lines 2.15 kB
import{createSelector}from'reselect';import{generateProductRelationsHash}from"../helpers";import{getProducts}from"./product";/** * Returns productRelations state. * @param {Object} state State. * @returns {Object} * @deprecated */export var getProductRelationsState=function getProductRelationsState(state){return state.product.productRelationsByHash;};/** * Factory of a function that returns selector of product relations state for given hash. * @param {string} hash State hash. * @returns {Function} * @deprecated */export var getProductRelationsByHash=function getProductRelationsByHash(hash){return createSelector(getProductRelationsState,function(state){if(state[hash]){return state[hash].productIds;}return undefined;});};/** * Selector factory. * Accepts parameters as arguments. When used within `mapStateToProps` factory won't be called * as long as `state` and `props` didn't change (shallow comparison). * @param {Object} params Params * @param {string} params.productId Product id. * @param {string} type Relation type (see constants) * @param {number} limit Query limit. * @returns {Function} Selector. * @deprecated Use `makeGetProductRelations` from `@shopgate/engage/products` instead. */export var getProductRelations=function getProductRelations(_ref){var productId=_ref.productId,type=_ref.type,limit=_ref.limit;return function(state){var hash=generateProductRelationsHash({productId:productId,type:type,limit:limit});return getProductRelationsByHash(hash)(state);};};/** * Returns products data for product id. * * @param {Object} params Params, see `.getProductRelations` * @returns {Array} * @deprecated Use `makeGetRelatedProducts` from `@shopgate/engage/products` instead. */export var getRelatedProducts=function getRelatedProducts(params){return createSelector(getProductRelations(params),getProducts,function(){var relations=arguments.length>0&&arguments[0]!==undefined?arguments[0]:[];var productsById=arguments.length>1?arguments[1]:undefined;return relations.filter(function(id){return productsById[id]&&productsById[id].productData;}).map(function(id){return productsById[id].productData;});});};