UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

6 lines 4.13 kB
import _regeneratorRuntime from"@babel/runtime/regenerator";function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}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{getCurrentRoute}from'@shopgate/pwa-common/selectors/router';import event from'@shopgate/pwa-core/classes/Event';import registerEvents from'@shopgate/pwa-core/commands/registerEvents';import appConfig from"../helpers/config";import{LoadingProvider}from"../providers";import{fetchUser}from"../actions/user";import{successLogin}from"../action-creators";import{historyPush}from"../actions/router";import logout from"../actions/user/logout";import{appDidStart$,userWillLogin$,userLoginResponse$,userDidLogin$,userDidLogout$,userDidInitialize$,legacyConnectRegisterDidFail$,userSessionExpired$}from"../streams";import showModal from"../actions/modal/showModal";import{LOGIN_PATH}from"../constants/RoutePaths";import{LEGACY_URL_CONNECT_REGISTER}from"../constants/Registration";import{EVENT_USER_INITIALIZED}from"../constants/user";/** * User subscriptions. * @param {Function} subscribe The subscribe function. */export default function user(subscribe){/** * Gets triggered when ever the user data need to be updated. */var userNeedsUpdate$=appDidStart$.merge(userDidLogin$);subscribe(userDidInitialize$,function(_ref){var events=_ref.events;events.emit(EVENT_USER_INITIALIZED);});subscribe(userWillLogin$,function(){LoadingProvider.setLoading(LOGIN_PATH);});subscribe(userLoginResponse$,function(){LoadingProvider.unsetLoading(LOGIN_PATH);});subscribe(userNeedsUpdate$,function(_ref2){var dispatch=_ref2.dispatch;dispatch(fetchUser());});subscribe(userDidLogout$,/*#__PURE__*/function(){var _ref4=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref3){var dispatch,action,isAutoLogout,confirmed;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:dispatch=_ref3.dispatch,action=_ref3.action;if(!(action.notify===false)){_context.next=3;break;}return _context.abrupt("return");case 3:isAutoLogout=action.autoLogout;if(!(isAutoLogout&&(appConfig===null||appConfig===void 0?void 0:appConfig.disableAutoLogoutModal)===true)){_context.next=6;break;}return _context.abrupt("return");case 6:_context.next=8;return dispatch(showModal(_extends({confirm:'modal.ok',dismiss:null,message:'login.logout_message',title:null},isAutoLogout?{message:'login.auto_logout_message',confirm:'modal.login',dismiss:'modal.close'}:null)));case 8:confirmed=_context.sent;if(isAutoLogout&&confirmed){dispatch(historyPush({pathname:LOGIN_PATH}));}case 10:case"end":return _context.stop();}},_callee);}));return function(_x){return _ref4.apply(this,arguments);};}());subscribe(appDidStart$,function(_ref5){var dispatch=_ref5.dispatch,getState=_ref5.getState;registerEvents(['userLoggedIn']);event.addCallback('userLoggedIn',function(){var _getCurrentRoute=getCurrentRoute(getState()),_getCurrentRoute$stat=_getCurrentRoute.state,_getCurrentRoute$stat2=_getCurrentRoute$stat===void 0?{}:_getCurrentRoute$stat,redirect=_getCurrentRoute$stat2.redirect;dispatch(successLogin(redirect));});});subscribe(legacyConnectRegisterDidFail$,function(_ref6){var dispatch=_ref6.dispatch;dispatch(historyPush({pathname:LEGACY_URL_CONNECT_REGISTER}));});subscribe(userSessionExpired$,function(_ref7){var dispatch=_ref7.dispatch;dispatch(logout(undefined,true));});}