UNPKG

@shopgate/engage

Version:
11 lines 2.99 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{appWillInit$}from'@shopgate/engage/core/streams';import{PERMISSION_ID_PUSH}from'@shopgate/engage/core/constants';import{appInitialization}from'@shopgate/engage/core/collections';import{requestAppPermissionStatus,showModal}from'@shopgate/engage/core/actions';import{fetchBackInStockSubscriptions}from'@shopgate/engage/back-in-stock/actions';import{addBackInStockReminderSuccess$,backInStockReminderNeedsFetch$}from'@shopgate/engage/back-in-stock/streams';import{getIsBackInStockEnabled,getAreBackInStockRequestsPossible}from'@shopgate/engage/back-in-stock/selectors';/** * @param {Function} subscribe The subscribe function. */export default function backInStock(subscribe){subscribe(appWillInit$,function(){/** * Back-in-stock requests are only possible when users granted push permissions. Otherwise * the backend will not be able to assign the subscriptions to a device. * * So in the next block we register a new handler to the "appInitialization" collection which * will retrieve the current status of the push permission before the React app starts. * * The back-in-stock reducer will store the status, so that selectors can work with it. */appInitialization.set('back-in-stock',/*#__PURE__*/function(){var _ref2=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref){var dispatch,getState,isEnabled;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:dispatch=_ref.dispatch,getState=_ref.getState;isEnabled=getIsBackInStockEnabled(getState());if(!isEnabled){_context.next=5;break;}_context.next=5;return dispatch(requestAppPermissionStatus({permissionId:PERMISSION_ID_PUSH}));case 5:case"end":return _context.stop();}},_callee);}));return function(_x){return _ref2.apply(this,arguments);};}());});subscribe(backInStockReminderNeedsFetch$,function(_ref3){var dispatch=_ref3.dispatch,getState=_ref3.getState;var requestsPossible=getAreBackInStockRequestsPossible(getState());if(requestsPossible){dispatch(fetchBackInStockSubscriptions());}});subscribe(addBackInStockReminderSuccess$,function(_ref4){var dispatch=_ref4.dispatch;dispatch(showModal({title:'back_in_stock.add_back_in_stock_success_modal.title',message:'back_in_stock.add_back_in_stock_success_modal.message',confirm:'modal.confirm',dismiss:null}));});}