UNPKG

@shopgate/engage

Version:
43 lines 5.41 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{createSelector}from'reselect';import{makeGetProductByCharacteristics}from'@shopgate/engage/product';import{appConfig}from'@shopgate/engage';import{hasNewServices,hasSGJavaScriptBridge,hasWebBridge}from'@shopgate/engage/core/helpers';import{getClientInformation}from'@shopgate/engage/core/selectors';import{PERMISSION_STATUS_GRANTED,PERMISSION_STATUS_NOT_SUPPORTED}from'@shopgate/engage/core/constants';/** * @param {Object} state The application state. * @returns {Object} */export var getBackInStockState=function getBackInStockState(state){return state.backInStock;};/** * Selector to retrieve the back-in-stock subscriptions list * @returns {Array} */export var getBackInStockSubscriptions=createSelector(getBackInStockState,function(state){return state.subscriptions;});/** * Selector to retrieve the current fetching state of back-in-stock subscriptions * @returns {boolean} */export var getBackInStockSubscriptionsFetching=createSelector(getBackInStockState,function(state){return state.isFetching;});/** * Selector to determine if back-in-stock subscriptions have been fetched before * @returns {boolean} */export var getBackInStockSubscriptionsInitial=createSelector(getBackInStockState,function(state){return state.isInitial;});/** * Selector to retrieve the current status of the push app permission * @returns {string} */export var getBackInStockPushPermissionStatus=createSelector(getBackInStockState,function(state){return state.pushPermissionStatus;});/** * Creates a selector that retrieves the subscription of * a product / variant or null by its variantId / productId * @param {Object} params Params * @param {string} params.status Get subscription for a specific status * @returns {Function} */export var makeGetSubscriptionByProduct=function makeGetSubscriptionByProduct(){var _ref=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{},status=_ref.status;return createSelector(function(state){var props=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};return props.variantId?props.variantId:props.productId;},getBackInStockSubscriptions,function(requestedProductCode,subscriptions){if(!requestedProductCode){return false;}return subscriptions.find(function(_ref2){var productCode=_ref2.productCode,subscriptionStatus=_ref2.status;var match=productCode===requestedProductCode;if(match&&status){// When the selector factory is created for a specific status, the subscription also // needs to fulfill this condition match=subscriptionStatus===status;}return match;})||null;});};/** * Creates a selector that retrieves the subscription of * a product / variant by its characteristics * @returns {Function} */export var makeGetSubscriptionByCharacteristics=function makeGetSubscriptionByCharacteristics(){var getProductByCharacteristics=makeGetProductByCharacteristics();return createSelector(getProductByCharacteristics,getBackInStockSubscriptions,function(product,subscriptions){if(!product){return null;}return subscriptions.find(function(_ref3){var productCode=_ref3.productCode;return productCode===product.id;})||null;});};/** * Selector to determine if the back-in-stock feature is enabled * @returns {boolean} */export var getIsBackInStockEnabled=createSelector(getClientInformation,getBackInStockPushPermissionStatus,function(clientInformation,pushPermissionStatus){// Feature is not supported with the new services right now, since non ROPE inventory is // not fully supported. if(hasNewServices()||hasWebBridge()){return false;}// Perform app version checks when PWA runs outside the browser if(hasSGJavaScriptBridge()){var _clientInformation$co2=clientInformation.codebaseVersion,codebaseVersion=_clientInformation$co2===void 0?'0.0.0':_clientInformation$co2;var _codebaseVersion$spli3=codebaseVersion.split('.'),_codebaseVersion$spli4=_slicedToArray(_codebaseVersion$spli3,1),major=_codebaseVersion$spli4[0];/** * The feature can be enabled on react-native-engage based apps (>= 11.0.0) with proper * support for push permissions checks */if(parseInt(major,10)<11||pushPermissionStatus===PERMISSION_STATUS_NOT_SUPPORTED){return false;}}return(appConfig===null||appConfig===void 0?void 0:appConfig.showBackInStock)||false;});/** * Selector to determine if back-in-stock related requests are currently possible. * @returns {boolean} */export var getAreBackInStockRequestsPossible=createSelector(getIsBackInStockEnabled,getBackInStockPushPermissionStatus,function(featureEnabled,pushPermissionStatus){return featureEnabled&&pushPermissionStatus===PERMISSION_STATUS_GRANTED;});/** * Returns if subscription list is in use * @returns {boolean} */export var getHasBackInStockSubscriptions=createSelector(getBackInStockSubscriptions,function(subscriptions){return!!subscriptions.length;});