UNPKG

@shopgate/engage

Version:
4 lines 2.81 kB
function _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArrayLimit(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally{try{if(!_n&&_i["return"]!=null)_i["return"]();}finally{if(_d)throw _e;}}return _arr;}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}import React,{useState,useMemo,useEffect}from'react';import{CATEGORY_PATTERN,CATEGORY_FILTER_PATTERN}from'@shopgate/pwa-common-commerce/category/constants';import{ITEM_PATTERN,ITEM_GALLERY_PATTERN,ITEM_REVIEWS_PATTERN,ITEM_WRITE_REVIEW_PATTERN}from'@shopgate/pwa-common-commerce/product/constants';import{hex2bin}from'@shopgate/pwa-common/helpers/data';import Context from"./SideNavigationProvider.context";import connect from"./SideNavigationProvider.connector";var categoryPatterns=[CATEGORY_PATTERN,CATEGORY_FILTER_PATTERN];var itemPatterns=[ITEM_PATTERN,ITEM_GALLERY_PATTERN,ITEM_REVIEWS_PATTERN,ITEM_WRITE_REVIEW_PATTERN];/** * SideNavigation Provider * @returns {JSX} */var SideNavigationProvider=function SideNavigationProvider(_ref){var maxCategoryNesting=_ref.maxCategoryNesting,routePatternBlacklist=_ref.routePatternBlacklist,currentParams=_ref.currentParams,currentPathname=_ref.currentPathname,currentRoute=_ref.currentRoute,isLoggedIn=_ref.isLoggedIn,logout=_ref.logout,children=_ref.children;var _useState=useState(null),_useState2=_slicedToArray(_useState,2),activeCategoryId=_useState2[0],setActiveCategoryId=_useState2[1];var _useState3=useState(true),_useState4=_slicedToArray(_useState3,2),isVisible=_useState4[0],setIsVisible=_useState4[1];useEffect(function(){if(!currentRoute){return;}var pattern=currentRoute.pattern;if(categoryPatterns.includes(pattern)){var categoryId=currentParams.categoryId;setActiveCategoryId(hex2bin(categoryId));}else if(!itemPatterns.includes(pattern)){setActiveCategoryId(null);}setIsVisible(!routePatternBlacklist.includes(pattern));});var value=useMemo(function(){return{maxCategoryNesting:maxCategoryNesting,currentParams:currentParams,currentPathname:currentPathname,currentRoute:currentRoute,isLoggedIn:isLoggedIn,logout:logout,activeCategoryId:activeCategoryId,isVisible:isVisible};},[activeCategoryId,currentParams,currentPathname,currentRoute,isLoggedIn,isVisible,logout,maxCategoryNesting]);return React.createElement(Context.Provider,{value:value},children);};SideNavigationProvider.defaultProps={currentParams:null,currentPathname:'',routePatternBlacklist:[],currentRoute:null};export default connect(SideNavigationProvider);