UNPKG

@shopgate/engage

Version:
7 lines 3.62 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,{useMemo,useState,useCallback,useEffect}from'react';import PropTypes from'prop-types';import{LoadingProvider}from'@shopgate/pwa-common/providers';import{STORE_FINDER_PATTERN}from"../constants";import{StoreFinderContext}from"../locations.context";import connect from"./StoreFinder.connector";import{useNavigation}from"../../core";/** * @param {Object} props The component props * @returns {JSX} */var StoreFinderProvider=function StoreFinderProvider(_ref){var children=_ref.children,locations=_ref.locations,isFetching=_ref.isFetching,shopSettings=_ref.shopSettings,userSearch=_ref.userSearch,storeFinderSearch=_ref.storeFinderSearch,storeListRef=_ref.storeListRef,selectGlobalLocation=_ref.selectGlobalLocation,selectLocation=_ref.selectLocation;var _useNavigation=useNavigation(),pop=_useNavigation.pop;var _useState=useState(null),_useState2=_slicedToArray(_useState,2),selectedLocation=_useState2[0],setSelectedLocation=_useState2[1];var _useState3=useState(null),_useState4=_slicedToArray(_useState3,2),locationsHash=_useState4[0],setLocationsHash=_useState4[1];var changeLocation=useCallback(function(location){var scrollIntoView=arguments.length>1&&arguments[1]!==undefined?arguments[1]:false;setSelectedLocation(location);if(scrollIntoView&&storeListRef.current){var container=storeListRef.current;var element=container.querySelector("[data-location-code=\"".concat(location.code,"\"]"));var scrollParams={top:element.parentNode.offsetTop-container.offsetTop-10,behavior:'smooth'};var _getComputedStyle2=getComputedStyle(container),overflowY=_getComputedStyle2.overflowY;if(overflowY==='scroll'){container.scroll(scrollParams);}else{window.scroll(scrollParams);}}},[storeListRef]);var selectLocationCb=useCallback(function(location){setSelectedLocation(location);selectLocation(location);selectGlobalLocation(location);// Back navigation pop();},[selectLocation,selectGlobalLocation,pop]);useEffect(function(){var hash=JSON.stringify(locations.map(function(_ref2){var code=_ref2.code;return code;}));if(hash!==locationsHash){setLocationsHash(hash);changeLocation(locations[0]);}});/** * @param {bool} loading */var setIsLoading=useCallback(function(loading){if(loading){LoadingProvider.setLoading(STORE_FINDER_PATTERN);return;}LoadingProvider.unsetLoading(STORE_FINDER_PATTERN);},[]);var value=useMemo(function(){return{locations:locations,selectedLocation:selectedLocation,changeLocation:changeLocation,selectLocation:selectLocationCb,isFetching:isFetching,shopSettings:shopSettings,userSearch:userSearch,storeFinderSearch:storeFinderSearch,setIsLoading:setIsLoading};},[isFetching,locations,changeLocation,selectLocationCb,selectedLocation,shopSettings,storeFinderSearch,userSearch,setIsLoading]);return React.createElement(StoreFinderContext.Provider,{value:value},children);};StoreFinderProvider.defaultProps={children:null,locations:[],storeListRef:null,isFetching:false,shopSettings:null,userSearch:null,storeFinderSearch:null};export default connect(StoreFinderProvider);