UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

68 lines 5.76 kB
import{createSelector}from'reselect';import MobileDetect from'mobile-detect';import{hasSGJavaScriptBridge}from'@shopgate/pwa-core/helpers';import{isVersionAtLeast}from'@shopgate/pwa-core/helpers/version';import{SCANNER_MIN_APP_LIB_VERSION}from'@shopgate/pwa-core/constants/Scanner';import{hasWebBridge}from'@shopgate/engage/core/helpers/bridge';import{OS_ANDROID,OS_IOS,MODEL_NAMES_IPHONE_X,PAGE_INSETS_ANDROID,PAGE_INSETS_IOS,PAGE_INSETS_IPHONE_X,TYPE_TABLET}from"../constants/Device";var md=new MobileDetect(navigator.userAgent);/** * Returns the client state (state.client) * @param {Object} state The application state. * @returns {Object} */export var getClientState=function getClientState(state){return state.client;};/** * Returns the client info state (state.client.info) * @param {Object} state The application state. * @returns {Object} */export var getClientInformation=createSelector(getClientState,function(state){return state.info;});/** * Returns the client connectivity state (state.client.connectivity) * @param {Object} state The application state. * @returns {Object} */export var getClientConnectivity=createSelector(getClientState,function(state){return state.connectivity;});/** * Returns the device information. * @param {Object} state The application state. * @return {Object|null} */export var getDeviceInformation=createSelector(getClientInformation,function(clientInformation){var device=clientInformation.device;return device||null;});/** * Creates the `getSupportedIdentityServices()` selector. * @returns {Function} */export function makeGetSupportedIdentityServices(){return createSelector(getClientInformation,function(info){return info.supportedIdentityServices||[];});}/** * Creates the `supportsIdentityService()` selector. * @param {string} service The identity service to check. * @returns {Function} */export function makeSupportsIdentityService(service){var getSupportedIdentityServices=makeGetSupportedIdentityServices();return createSelector(getSupportedIdentityServices,function(services){return services.includes(service);});}/** * Returns the device platform. * @param {Object} state The application state. * @return {string|null} */export var getPlatform=createSelector(getDeviceInformation,function(deviceInformation){var _ref=deviceInformation||{},_ref$os=_ref.os,_ref$os2=_ref$os===void 0?{}:_ref$os,_ref$os2$platform=_ref$os2.platform,platform=_ref$os2$platform===void 0?null:_ref$os2$platform;return platform;});/** * Returns the os version. * @return {string|null} */export var getOSVersion=createSelector(getDeviceInformation,function(deviceInformation){var _ref2=deviceInformation||{},_ref2$os=_ref2.os,_ref2$os2=_ref2$os===void 0?{}:_ref2$os,_ref2$os2$ver=_ref2$os2.ver,ver=_ref2$os2$ver===void 0?null:_ref2$os2$ver;return ver;});/** * Returns the device model. * @param {Object} state The application state. * @return {string|null} */export var getDeviceModel=createSelector(getDeviceInformation,function(deviceInformation){var _ref3=deviceInformation||{},_ref3$model=_ref3.model,model=_ref3$model===void 0?null:_ref3$model;return model;});/** * Check if the platform is Android. * @param {Object} state The application state. * @return {boolean} */export var isAndroid=createSelector(getPlatform,function(platform){if(hasWebBridge()){return md.os()==='AndroidOS';}return platform===OS_ANDROID;});/** * Check if the platform is iOS. * @param {Object} state The application state. * @return {boolean} */export var isIos=createSelector(getPlatform,function(platform){return platform===OS_IOS;});/** * Checks if the currently stored lib version is one that supports the scanner. * @param {Object} state The application state. * @returns {boolean} */export var hasScannerSupport=createSelector(getClientInformation,getDeviceInformation,isIos,function(clientInformation,deviceInformation,deviceIsIos){var _ref4=deviceInformation||{},type=_ref4.type;var isIpad=type===TYPE_TABLET&&deviceIsIos;if(isVersionAtLeast('11.0.0',clientInformation.appVersion)){// scanner is supported on all apps based on the react-native based apps return true;}// scanner is not supported on iPads with the not react-native based app return isVersionAtLeast(SCANNER_MIN_APP_LIB_VERSION,clientInformation.libVersion)&&!isIpad;});/** * Determines page insets for the current device * @param {Object} state The application state. * @returns {Object} */export var getPageInsets=createSelector(getClientInformation,getDeviceModel,isIos,function(clientInformation,model,iOS){if(iOS){if(!hasSGJavaScriptBridge()){return PAGE_INSETS_ANDROID;}if(MODEL_NAMES_IPHONE_X.includes(model)){return PAGE_INSETS_IPHONE_X;}return PAGE_INSETS_IOS;}return PAGE_INSETS_ANDROID;});/** * Determines what os version handles the insets differently compared to other os versions. * @returns {boolean} */export var considerNativeInset=createSelector(isIos,getOSVersion,function(ios,ver){return ios&&ver&&ver.indexOf('10.')===0;});/** * Checks if the client is connected. * @param {Object} state The application state. * @returns {boolean} */export var getIsConnected=createSelector(getClientConnectivity,function(connectivity){return connectivity.connected;});/** * Determines the network type of the client connection e.g. LTE or UMTS. * @param {Object} state The application state. * @returns {string} */export var getClientConnectivityNetwork=createSelector(getClientConnectivity,function(connectivity){return connectivity.network;});/** * Determines the type of the client connection e.g. WIFI or 4G. * @param {Object} state The application state. * @returns {string} */export var getClientConnectivityType=createSelector(getClientConnectivity,function(connectivity){return connectivity.type;});