UNPKG

@shopgate/engage

Version:
12 lines 3.79 kB
import _regeneratorRuntime from"@babel/runtime/regenerator";function _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArrayLimit(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally{try{if(!_n&&_i["return"]!=null)_i["return"]();}finally{if(_d)throw _e;}}return _arr;}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}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{hasSGJavaScriptBridge,hasWebBridge,createMockedPermissions}from'@shopgate/engage/core/helpers';import{requestAppPermissions}from'@shopgate/pwa-core/commands/appPermissions';import{PERMISSION_STATUS_NOT_SUPPORTED,PERMISSION_STATUS_GRANTED}from'@shopgate/engage/core/constants';import{appPermissionStatusReceived}from"../action-creators";/** * Requests an additional app permission from the operating system. * Additionally it propagates the received status via the APP_PERMISSION_STATUS_RECEIVED action. * @param {Object} params The action params * @param {string} params.permissionId The desired app permission id * @param {Function} [params.dispatchMock=null] An optional mock for the request dispatch logic. * Usually used when PWA is running inside a browser and app command logic can be simulated via * browser APIs like for geolocation access. * @returns {Function} A Redux thunk */var requestAppPermission=function requestAppPermission(_ref){var permissionId=_ref.permissionId,dispatchMockParam=_ref.dispatchMock;return(/*#__PURE__*/function(){var _ref2=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dispatch){var _ref5;var dispatchMock,_ref3,_ref4,_ref4$,_ref4$2,status,options,data;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:dispatchMock=dispatchMockParam;// In development a mock needs to be injected to enable command testing. Command responses // can be mocked via the global MockedAppPermissions object see appPermissions.js if(!dispatchMockParam&&(!hasSGJavaScriptBridge()||hasWebBridge())){dispatchMock=createMockedPermissions(PERMISSION_STATUS_GRANTED);}_context.next=4;return requestAppPermissions([{permissionId:permissionId}],dispatchMock);case 4:_context.t1=_ref5=_context.sent;_context.t0=_context.t1!==null;if(!_context.t0){_context.next=8;break;}_context.t0=_ref5!==void 0;case 8:if(!_context.t0){_context.next=12;break;}_context.t2=_ref5;_context.next=13;break;case 12:_context.t2=[];case 13:_ref3=_context.t2;_ref4=_slicedToArray(_ref3,1);_ref4$=_ref4[0];_ref4$2=_ref4$===void 0?{status:PERMISSION_STATUS_NOT_SUPPORTED}:_ref4$;status=_ref4$2.status;options=_ref4$2.options;data=_ref4$2.data;dispatch(appPermissionStatusReceived({permissionId:permissionId,status:status,options:options}));return _context.abrupt("return",{status:status,data:data});case 22:case"end":return _context.stop();}},_callee);}));return function(_x){return _ref2.apply(this,arguments);};}());};export default requestAppPermission;