UNPKG

@shopgate/engage

Version:
24 lines 5.96 kB
import _regeneratorRuntime from"@babel/runtime/regenerator";function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){try{var info=gen[key](arg);var value=info.value;}catch(error){reject(error);return;}if(info.done){resolve(value);}else{Promise.resolve(value).then(_next,_throw);}}function _asyncToGenerator(fn){return function(){var self=this,args=arguments;return new Promise(function(resolve,reject){var gen=fn.apply(self,args);function _next(value){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value);}function _throw(err){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err);}_next(undefined);});};}import{withScope,captureMessage,Severity as SentrySeverity}from'@sentry/browser';import moment from'moment';import{configureStore}from'@shopgate/pwa-common/store';import{appWillInit,appWillStart}from'@shopgate/pwa-common/action-creators/app';import{i18n}from'@shopgate/engage/core';import fetchClientInformation from'@shopgate/pwa-common/actions/client/fetchClientInformation';import appConfig from'@shopgate/pwa-common/helpers/config';import{appInitialization,configuration}from'@shopgate/engage/core/collections';import{CONFIGURATION_COLLECTION_KEY_BASE_URL}from'@shopgate/engage/core/constants';import{getAppBaseUrl,isDev,hasSGJavaScriptBridge,hasWebBridge}from'@shopgate/engage/core/helpers';import{receiveShopSettings,receiveMerchantSettings,errorShopSettings}from"../action-creators";var _appConfig$locales=appConfig.locales,_appConfig$locales2=_appConfig$locales===void 0?{}:_appConfig$locales,_appConfig$locales2$c=_appConfig$locales2.currency,currencyLocale=_appConfig$locales2$c===void 0?null:_appConfig$locales2$c,settingsUrl=appConfig.settingsUrl,merchantSettingsUrl=appConfig.merchantSettingsUrl;var REQUEST_TIMEOUT=3000;/** * Injects a script tag to fetch the shop settings JSONP. * @param {string} id The script id * @param {string} src The script url * @param {Function} onError Callback for error situations. */var injectScript=function injectScript(id,src,onError){var scriptTag=document.querySelector("#".concat(id));if(scriptTag){scriptTag.remove();}scriptTag=document.createElement('script');scriptTag.setAttribute('src',src);scriptTag.setAttribute('id',id);scriptTag.onerror=onError;document.querySelector('head').appendChild(scriptTag);};/** * Fetches settings * @param {Object} store Reference to the store * @returns {Promise} */var fetchSettings=function fetchSettings(store){return new Promise(function(resolve,reject){if(!settingsUrl){reject();return;}var timeout;var merchantReceived=null;var shopReceived=null;/** * Evaluates if completed. */var evaluateEnd=function evaluateEnd(){if(!merchantReceived&&merchantSettingsUrl||!shopReceived){return;}clearTimeout(timeout);timeout=null;resolve();};// Add the callbacks for the JSONP window.setShopSettings=function(settings){store.dispatch(receiveShopSettings(settings));shopReceived=true;evaluateEnd();};window.setMerchantSettings=function(settings){store.dispatch(receiveMerchantSettings(settings));merchantReceived=true;evaluateEnd();};// Error handling injectScript('shop-settings-jsonp',settingsUrl,function(error){withScope(function(scope){scope.setLevel(SentrySeverity.Error);scope.setExtra('settingsUrl',settingsUrl);captureMessage('Fetching shop settings failed');});clearTimeout(timeout);timeout=null;store.dispatch(errorShopSettings(error));reject();});if(merchantSettingsUrl){injectScript('merchant-settings-jsonp',merchantSettingsUrl,function(){withScope(function(scope){scope.setLevel(SentrySeverity.Error);scope.setExtra('settingsUrl',merchantSettingsUrl);captureMessage('Fetching merchant settings failed');});clearTimeout(timeout);timeout=null;reject();});}timeout=setTimeout(function(){withScope(function(scope){scope.setLevel(SentrySeverity.Warning);scope.setExtra('settingsUrl',settingsUrl);scope.setExtra('timeout',REQUEST_TIMEOUT);captureMessage('Fetching shop settings took too long');});},REQUEST_TIMEOUT);});};/** * * @param {Object} locales An object with locales. * @param {Function} reducers The reducers from the theme. * @param {Array} subscribers The subscribers to the streams middleware. */export var initialize=/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(locales,reducers,subscribers){var store;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:moment.locale(process.env.LOCALE);if(isDev){// Inject an object to the window that can be used to check if the PWA is running in dev mode window.SGConnectDev={// Indicates the the current dev app is running in a browser - not in the real app isDevBrowser:!hasWebBridge()&&!hasSGJavaScriptBridge(),// Indicates that the PWA is running in a dev environment isDev:isDev};}i18n.init({locales:locales,lang:process.env.LOCALE,currencyLocale:currencyLocale});store=configureStore(reducers,subscribers);_context.prev=4;_context.next=7;return store.dispatch(fetchClientInformation());case 7:_context.next=11;break;case 9:_context.prev=9;_context.t0=_context["catch"](4);case 11:// Save the base url inside the configuration collection before any other code can apply // url manipulations. configuration.set(CONFIGURATION_COLLECTION_KEY_BASE_URL,getAppBaseUrl());store.dispatch(appWillInit("".concat(window.location.pathname).concat(window.location.search)));_context.prev=13;_context.next=16;return fetchSettings(store);case 16:_context.next=20;break;case 18:_context.prev=18;_context.t1=_context["catch"](13);case 20:_context.next=22;return appInitialization.initialize({dispatch:store.dispatch,getState:store.getState});case 22:store.dispatch(appWillStart("".concat(window.location.pathname).concat(window.location.search)));return _context.abrupt("return",{store:store});case 24:case"end":return _context.stop();}},_callee,null,[[4,9],[13,18]]);}));return function initialize(_x,_x2,_x3){return _ref.apply(this,arguments);};}();