-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make permissions a soft dependency in fsengage
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
1 parent
a634a46
commit e805007
Showing
2 changed files
with
54 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 51 additions & 19 deletions
70
packages/fsengage/src/modules/cms/requesters/contentManagementSystemLocatorPermission.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |