Skip to content

Commit

Permalink
feat: make permissions a soft dependency in fsengage
Browse files Browse the repository at this point in the history
fsengage is a dependency on a number of fs packages, and fsengage had a hard dependency on react native permissions due to its geolocation handler for the core cms provider. react-native-permissions requires native setup and exposes itself to end users, which is not ideal given that most projects that use fsengage don't need geolocation functionality. This PR makes permissions a peer dependency and updates the geolocation handler to return false if react native permissions isn't available in the project.
  • Loading branch information
bweissbart committed Sep 25, 2020
1 parent a634a46 commit e805007
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
4 changes: 3 additions & 1 deletion packages/fsengage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
"decimal.js": "^10.0.0",
"react": "^16.13.1",
"react-native": "^0.63.2",
"react-native-permissions": "^2.2.0",
"url-parse": "^1.4.3"
},
"devDependencies": {
"sinon": "^8.0.2"
},
"peerDependencies": {
"react-native-permissions": "^2.2.0"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,61 @@
import {check, Permission, PERMISSIONS, request, RESULTS} from 'react-native-permissions';
import { Platform } from 'react-native';

function getPermissionToCheck(): Permission {
// Geolocation is currently bundled with fsengage even though apps may not use location-based
// targeting. Because react-native-permissions requires apps to expose the permissions they use,
// this makes permissions a "soft" dependency that apps can opt into by including rn-permissions
// as a dependency. TODO - refactor fsengage so that location-based permissions are taken out of
// engage core.
let rnPermissions: any = null;

try {
rnPermissions = require('react-native-permissions');
} catch (e) {
console.warn(
'react-native-permissions must be added to your project'
+ ' to enable granular geolocation in fsengage'
);
}

function getPermissionToCheck(): any {
if (!rnPermissions) {
return null;
}

return Platform.OS === 'ios' ?
PERMISSIONS.IOS.LOCATION_WHEN_IN_USE : PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION;
rnPermissions.PERMISSIONS.IOS.LOCATION_WHEN_IN_USE
: rnPermissions.PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION;
}

export async function isGeolocationAllowed(): Promise<boolean> {
return check(getPermissionToCheck())
.then(status => status === RESULTS.GRANTED)
.catch(error => {
if (__DEV__) {
console.log(
`%cLocator\n%c Function: isGeolocationAllowed\n Error: `,
'color: blue',
'color: grey',
error
);
}

throw error;
});
const permissionToCheck = getPermissionToCheck();

if (permissionToCheck) {
return rnPermissions.check(getPermissionToCheck())
.then((status: any) => status === rnPermissions.RESULTS.GRANTED)
.catch((error: Error) => {
if (__DEV__) {
console.log(
`%cLocator\n%c Function: isGeolocationAllowed\n Error: `,
'color: blue',
'color: grey',
error
);
}

throw error;
});
}

return Promise.resolve(false);
}

export async function requestGeolocationPermission(): Promise<boolean> {
return request(getPermissionToCheck())
.then(status => status === RESULTS.GRANTED);
const permissionToCheck = getPermissionToCheck();

if (permissionToCheck) {
return rnPermissions.request(getPermissionToCheck())
.then((status: any) => status === rnPermissions.RESULTS.GRANTED);
}

return Promise.resolve(false);
}

0 comments on commit e805007

Please sign in to comment.