UNPKG

@shopgate/engage

Version:
4 lines 1.97 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{mutable}from'@shopgate/engage/core/helpers';import{PipelineRequest}from'@shopgate/engage/core/classes';import selectLocation from"../action-creators/selectLocation";import{getPreferredLocation}from"../selectors";/** * Fetches the default location of the user * @returns {Function} redux thunk */var fetchDefaultLocation=function fetchDefaultLocation(){return(/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dispatch,getState){var _getPreferredLocation;var request,_ref2,location,selectedLocationCode;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:request=new PipelineRequest('shopgate.user.getDefaultLocation').dispatch();_context.next=3;return request;case 3:_ref2=_context.sent;location=_ref2.location;selectedLocationCode=(_getPreferredLocation=getPreferredLocation(getState()))===null||_getPreferredLocation===void 0?void 0:_getPreferredLocation.code;if(selectedLocationCode!==(location===null||location===void 0?void 0:location.code)||location===null){dispatch(selectLocation(location));}return _context.abrupt("return",request);case 8:case"end":return _context.stop();}},_callee);}));return function(_x,_x2){return _ref.apply(this,arguments);};}());};export default mutable(fetchDefaultLocation);