UNPKG

@shopgate/pwa-common-commerce

Version:

Commerce library for the Shopgate Connect PWA.

88 lines 7.3 kB
import{createSelector}from'reselect';import{getCurrentRoute,getCurrentParams}from'@shopgate/pwa-common/selectors/router';import{hex2bin}from'@shopgate/pwa-common/helpers/data';/** * Retrieves the category state from the state. * @param {Object} state The application state. * @param {Object} props The component props. * @returns {Object} The category state. */var getCategoryState=function getCategoryState(state){return state.category;};/** * Retrieves the categoriesById state from the category state. * @param {Object} state The application state. * @param {Object} props The component props. * @returns {Object} The categoriesById state. */var getCategoriesByIdState=createSelector(getCategoryState,function(){var state=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};return state.categoriesById||{};});/** * Retrieves the childrenByCategoryId state from the category state. * @param {Object} state The application state. * @param {Object} props The component props. * @returns {Object} The childrenByCategoryId state. */var getChildrenByCategoryIdState=createSelector(getCategoryState,function(){var state=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};return state.childrenByCategoryId||{};});/** * Retrieves the rootCategories state from the category state. * @param {Object} state The application state. * @returns {Object} */export var getRootCategoriesState=createSelector(getCategoryState,function(){var state=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};return state.rootCategories||{};});/** * Retrieves a category id either from the selector props, or the params of the active route. * @param {Object} state The application state. * @param {Object} props The component props. * @returns {string|null} The category ID. */export var getCategoryId=createSelector(function(state){var props=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};return props.categoryId;},getCurrentParams,function(categoryId,routeParams){if(typeof categoryId!=='undefined'){return categoryId;}if(!routeParams||!routeParams.categoryId){return null;}return hex2bin(routeParams.categoryId);});/** * Retrieves the child categories for a specific parent category from the state. * @param {Object} state The application state. * @param {Object} props The component props. * @returns {Object} */export var getChildCategoriesForCategory=createSelector(getCategoryId,getChildrenByCategoryIdState,function(categoryId,childCategories){return childCategories[categoryId];});/** * Retrieves a category from state. * The category can either be referenced by a categoryId within the props, * or by a categoryId within the params of the active route state. * @param {Object} state The current application state. * @param {Object} props The component props. * @return {Object|null} */export var getCategory=createSelector(getCategoryId,getCategoriesByIdState,function(categoryId,categoriesById){if(!categoriesById||!categoriesById[categoryId]){return null;}return categoriesById[categoryId];});/** * Retrieves the root categories. * @param {Object} state The application state. * @param {Object} props The component props. * @returns {Array} */export var getRootCategories=createSelector(getRootCategoriesState,getCategoriesByIdState,function(rootCategories,categoriesById){if(Object.keys(rootCategories).length===0){return null;}if(!rootCategories.categories){return null;}return rootCategories.categories.map(function(id){return categoriesById[id];}).filter(Boolean);});/** * Retrieves the number of child categories for a category. * The category can either be referenced by a categoryId within the props, * or by a categoryId within the params of the active route state. * @param {Object} state The current application state. * @param {Object} props The component props. * @returns {number|null} */export var getCategoryChildCount=createSelector(getCategory,function(category){return category?category.childrenCount:null;});/** * Determines if a category has child categories. * The category can either be referenced by a categoryId within the props, * or by a categoryId within the params of the active route state. * @param {Object} state The application state. * @param {Object} props The component props. * @returns {boolean} */export var hasCategoryChildren=createSelector(getCategoryChildCount,function(count){return count>0;});/** * Retrieves the child categories of a category. * The category can either be referenced by a categoryId within the props, * or by a categoryId within the params of the active route state. * @param {Object} state The application state. * @param {Object} props The component props. * @returns {Array|null} */export var getCategoryChildren=createSelector([getChildCategoriesForCategory,getCategoriesByIdState],function(childCategories,categoriesById){if(!childCategories||!childCategories.children){return null;}return childCategories.children.map(function(id){return categoriesById[id];}).filter(Boolean);});export var areCategoryChildrenFetching=createSelector([getChildCategoriesForCategory],function(childCategories){if(!childCategories||!childCategories.isFetching){return false;}return childCategories.isFetching;});/** * Retrieves the number of products inside a category. * The category can either be referenced by a categoryId within the props, * or by a categoryId within the params of the active route state. * @param {Object} state The application state. * @param {Object} props The component props. * @returns {number|null} */export var getCategoryProductCount=createSelector(getCategoriesByIdState,getCategoryId,function(categoriesById,categoryId){if(!categoriesById[categoryId]||!categoriesById[categoryId].productCount&&categoriesById[categoryId].productCount!==0){return null;}return categoriesById[categoryId].productCount;});/** * Determines if a category has products. * The category can either be referenced by a categoryId within the props, * or by a categoryId within the params of the active route state. * @param {Object} state The application state. * @param {Object} props The component props. * @returns {boolean} */export var hasCategoryProducts=createSelector(getCategoryProductCount,function(count){return count>0;});/** * Retrieves the name of a category. When no category can be determined it return the title of * the active route. * The category can either be referenced by a categoryId within the props, * or by a categoryId within the params of the active route state. * @param {Object} state The application state. * @param {Object} props The component props. * @returns {number|null} */export var getCategoryName=createSelector(getCurrentRoute,getCategory,function(route,category){if(category){return category.name;}if(route.state.title){return route.state.title;}return null;});/** * Selector mappings for PWA < 6.10 * @deprecated */export var getCategoryById=getCategory;export var getCurrentCategoryId=getCategory;export var getCurrentCategory=getCategory;export var getCurrentCategoryChildCount=getCategoryChildCount;export var getCurrentCategories=getCategoryChildren;export var getChildCategoriesById=getCategoryChildren;export var hasChildren=hasCategoryChildren;export var hasProducts=hasCategoryProducts;