UNPKG

@shopgate/pwa-tracking

Version:

Tracking library for the Shopgate Connect PWA.

10 lines 1.65 kB
import'rxjs/add/operator/switchMap';import'rxjs/add/operator/filter';import'rxjs/add/observable/of';import{Observable}from'rxjs/Observable';import{getProductsResult}from'@shopgate/pwa-common-commerce/product/selectors/product';import{main$,pwaDidAppear$}from'@shopgate/engage/core/streams';import{getIsAppWebViewVisible,getCurrentSearchQuery}from'@shopgate/engage/core/selectors';import{SEARCH_PATTERN,RECEIVE_SEARCH_RESULTS}from'@shopgate/engage/search/constants';import{searchDidEnter$}from'@shopgate/engage/search/streams';/** * Emits when the search route comes active again after a legacy page was active. */var searchRouteReappeared$=pwaDidAppear$.filter(function(_ref){var action=_ref.action;return action.route.pattern===SEARCH_PATTERN;});/** * Emits when search results are received. */var resultsReceived$=main$.filter(function(_ref2){var action=_ref2.action;return action.type===RECEIVE_SEARCH_RESULTS;});/** * Emits when the search is ready to be tracked and all relevant data is available. */export var searchIsReady$=searchDidEnter$// Do not track while PWA webview is in the background .filter(function(_ref3){var getState=_ref3.getState;return getIsAppWebViewVisible(getState());}).switchMap(function(data){var getState=data.getState;var query=getCurrentSearchQuery(getState());// Check if products for the current route are already available within Redux. var productsLoaded=getProductsResult(getState(),{searchPhrase:query}).totalProductCount!==null;if(!productsLoaded){// Wait for incoming products if they are not available yet. return resultsReceived$.first();}return Observable.of(data);}).merge(searchRouteReappeared$);