UNPKG

@shopgate/engage

Version:
41 lines 9.06 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 event from'@shopgate/pwa-core/classes/Event';import{openAppSettings}from'@shopgate/engage/core/commands';import{showModal}from'@shopgate/engage/core/actions';import{PERMISSION_STATUS_DENIED,PERMISSION_STATUS_GRANTED,PERMISSION_STATUS_NOT_DETERMINED,PERMISSION_STATUS_NOT_SUPPORTED,APP_EVENT_APPLICATION_WILL_ENTER_FOREGROUND,availablePermissionsIds}from'@shopgate/engage/core/constants';import{logger,hasSGJavaScriptBridge,hasWebBridge}from'@shopgate/engage/core/helpers';import{softOptInShown,softOptInSelected,hardOptInShown,hardOptInSelected}from"../action-creators";import requestAppPermission from"./requestAppPermission";import requestAppPermissionStatus from"./requestAppPermissionStatus";import{createMockedPermissions}from"../helpers/appPermissions";/** * Determines the current state of a specific permission for an app feature. If not already * happened, the user will be prompted to grant permissions. * The action returns a promise which resolves with a boolean value, that indicates the state. * @param {Object} options Action options. * @param {string} options.permissionId The id of the permission to request. * @param {boolean} [options.useSettingsModal=false] Whether in case of declined permissions a modal * shall be presented, which redirects to the app settings. * @param {boolean} [options.useRationaleModal=false] Whether a rational modal should be shown * @param {Object} [options.rationaleModal={}] Options for the rationale modal. * @param {string} options.rationaleModal.title Modal title. * @param {string} options.rationaleModal.message Modal message. * @param {string} options.rationaleModal.confirm Label for the confirm button. * @param {string} options.rationaleModal.dismiss Label for the dismiss button. * @param {Object} options.rationaleModal.params Additional parameters for i18n strings. * @param {Object} [options.modal={}] Options for the settings modal. * @param {string} options.modal.title Modal title. * @param {string} options.modal.message Modal message. * @param {string} options.modal.confirm Label for the confirm button. * @param {string} options.modal.dismiss Label for the dismiss button. * @param {Object} options.modal.params Additional parameters for i18n strings. * @param {boolean} [options.requestPermissions=true] If set to TRUE no permissions will be * requested if not already granted, * @param {boolean} [options.resolveWithData=false] If set to TRUE the Promise will resolve with * data if available (e.g. geolocation). * @param {Object} [options.meta={}] Additional meta data used for opt-in tracking actions * @return { Function } A redux thunk. */var grantPermissions=function grantPermissions(){var options=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};return function(dispatch){return new Promise(/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(resolve){var permissionId,_options$useSettingsM,useSettingsModal,_options$useRationale,useRationaleModal,_options$rationaleMod,rationaleModalOptions,_options$modal,modalOptions,_options$requestPermi,requestPermissions,_options$resolveWithD,resolveWithData,_options$meta,meta,dispatchMock,fallbackStatus,status,data,_ref2,requestAllowed,_ref3,openSettings,handler;return _regeneratorRuntime.wrap(function _callee2$(_context2){while(1)switch(_context2.prev=_context2.next){case 0:permissionId=options.permissionId,_options$useSettingsM=options.useSettingsModal,useSettingsModal=_options$useSettingsM===void 0?false:_options$useSettingsM,_options$useRationale=options.useRationaleModal,useRationaleModal=_options$useRationale===void 0?false:_options$useRationale,_options$rationaleMod=options.rationaleModal,rationaleModalOptions=_options$rationaleMod===void 0?{}:_options$rationaleMod,_options$modal=options.modal,modalOptions=_options$modal===void 0?{}:_options$modal,_options$requestPermi=options.requestPermissions,requestPermissions=_options$requestPermi===void 0?true:_options$requestPermi,_options$resolveWithD=options.resolveWithData,resolveWithData=_options$resolveWithD===void 0?false:_options$resolveWithD,_options$meta=options.meta,meta=_options$meta===void 0?{}:_options$meta;if(!hasSGJavaScriptBridge()||hasWebBridge()){/** * The fallbackStatus will be used at browsers that don't support the permissions API. By * default it will always resolve with GRANTED, so that further steps still might trigger a * permissions dialog. When permissions are not supposed to be requested, it will resolve with * the NOT_DETERMINED status, to keep logic between browsers / apps in sync as much as possible. */fallbackStatus=requestPermissions?PERMISSION_STATUS_GRANTED:PERMISSION_STATUS_NOT_DETERMINED;dispatchMock=createMockedPermissions(fallbackStatus);}if(availablePermissionsIds.includes(permissionId)){_context2.next=6;break;}logger.error('grandPermissions: %s is no valid permission id',permissionId);resolve(false);return _context2.abrupt("return");case 6:_context2.next=8;return dispatch(requestAppPermissionStatus({permissionId:permissionId,dispatchMock:dispatchMock}));case 8:_ref2=_context2.sent;status=_ref2.status;if(!(status===PERMISSION_STATUS_NOT_SUPPORTED)){_context2.next=13;break;}resolve(false);return _context2.abrupt("return");case 13:if(!(status===PERMISSION_STATUS_NOT_DETERMINED)){_context2.next=36;break;}if(requestPermissions){_context2.next=17;break;}resolve(false);return _context2.abrupt("return");case 17:if(!useRationaleModal){_context2.next=26;break;}dispatch(softOptInShown({meta:meta}));_context2.next=21;return dispatch(showModal({message:rationaleModalOptions.message||'',confirm:rationaleModalOptions.confirm||'',dismiss:rationaleModalOptions.dismiss||'',params:rationaleModalOptions.params}));case 21:requestAllowed=_context2.sent;dispatch(softOptInSelected({selection:requestAllowed?'approved':'later',meta:meta}));if(!(requestAllowed===false)){_context2.next=26;break;}resolve(false);return _context2.abrupt("return");case 26:dispatch(hardOptInShown({permissionId:permissionId,meta:meta}));// Trigger the native permissions dialog. _context2.next=29;return dispatch(requestAppPermission({permissionId:permissionId,dispatchMock:dispatchMock}));case 29:_ref3=_context2.sent;status=_ref3.status;data=_ref3.data;dispatch(hardOptInSelected({permissionId:permissionId,status:status,meta:meta}));// The user denied the permissions within the native dialog. if(![PERMISSION_STATUS_DENIED,PERMISSION_STATUS_NOT_DETERMINED].includes(status)){_context2.next=36;break;}resolve(false);return _context2.abrupt("return");case 36:if(!(status===PERMISSION_STATUS_GRANTED)){_context2.next=39;break;}resolve(resolveWithData&&data?data:true);return _context2.abrupt("return");case 39:if(!(status===PERMISSION_STATUS_DENIED)){_context2.next=52;break;}if(useSettingsModal){_context2.next=43;break;}resolve(false);return _context2.abrupt("return");case 43:_context2.next=45;return dispatch(showModal({title:modalOptions.title||null,message:modalOptions.message,confirm:modalOptions.confirm,dismiss:modalOptions.dismiss,params:modalOptions.params}));case 45:openSettings=_context2.sent;if(openSettings){_context2.next=49;break;}resolve(false);return _context2.abrupt("return");case 49:/** * Handler for the app event. */handler=/*#__PURE__*/function(){var _ref4=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(){var _ref5;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:event.removeCallback(APP_EVENT_APPLICATION_WILL_ENTER_FOREGROUND,handler);_context.next=3;return dispatch(requestAppPermissionStatus({permissionId:permissionId}));case 3:_ref5=_context.sent;status=_ref5.status;resolve(status===PERMISSION_STATUS_GRANTED);case 6:case"end":return _context.stop();}},_callee);}));return function handler(){return _ref4.apply(this,arguments);};}();/** * Register an event handler, so that we can perform the permissions check again, * when the user comes back from the settings. */event.addCallback(APP_EVENT_APPLICATION_WILL_ENTER_FOREGROUND,handler);// Open the settings (protected by a timeout, so that the modal closes before the app is left). setTimeout(function(){openAppSettings();},0);case 52:case"end":return _context2.stop();}},_callee2);}));return function(_x){return _ref.apply(this,arguments);};}());};};export default grantPermissions;