-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25990 from huzaifa-99/25855-get-user-location
Allow user to get current location
- Loading branch information
Showing
21 changed files
with
9,743 additions
and
401 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
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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
99 changes: 99 additions & 0 deletions
99
src/components/LocationErrorMessage/BaseLocationErrorMessage.js
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import {View} from 'react-native'; | ||
import {withOnyx} from 'react-native-onyx'; | ||
import ONYXKEYS from '../../ONYXKEYS'; | ||
import compose from '../../libs/compose'; | ||
import colors from '../../styles/colors'; | ||
import styles from '../../styles/styles'; | ||
import Icon from '../Icon'; | ||
import * as Expensicons from '../Icon/Expensicons'; | ||
import Text from '../Text'; | ||
import TextLink from '../TextLink'; | ||
import withLocalize, {withLocalizePropTypes} from '../withLocalize'; | ||
import Tooltip from '../Tooltip'; | ||
import PressableWithoutFeedback from '../Pressable/PressableWithoutFeedback'; | ||
import * as User from '../../libs/actions/User'; | ||
import CONST from '../../CONST'; | ||
|
||
const propTypes = { | ||
/** The location error code from onyx */ | ||
locationErrorCode: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf([null])]), | ||
|
||
/** A callback that runs when 'allow location permission' link is pressed */ | ||
onAllowLocationLinkPress: PropTypes.func.isRequired, | ||
|
||
...withLocalizePropTypes, | ||
}; | ||
|
||
const defaultProps = { | ||
locationErrorCode: undefined, | ||
}; | ||
|
||
function BaseLocationErrorMessage({locationErrorCode, onAllowLocationLinkPress, translate}) { | ||
if (!locationErrorCode) { | ||
return null; | ||
} | ||
|
||
const isPermissionDenied = locationErrorCode === 1; | ||
|
||
/** | ||
* Clears the location error on press of close icon | ||
*/ | ||
const dismissError = () => { | ||
User.clearLocationError(); | ||
}; | ||
|
||
return ( | ||
<View style={[styles.dotIndicatorMessage, styles.mt4]}> | ||
<View style={styles.offlineFeedback.errorDot}> | ||
<Icon | ||
src={Expensicons.DotIndicator} | ||
fill={colors.red} | ||
/> | ||
</View> | ||
<View style={styles.offlineFeedback.textContainer}> | ||
{/* | ||
Show appropriate error msg on location issues | ||
- errorCode = -1 -> location not supported (web only) | ||
- errorCode = 1 -> location permission is not enabled | ||
- errorCode = 2 -> location is unavailable or there is some connection issue | ||
- errorCode = 3 -> location fetch timeout | ||
*/} | ||
{isPermissionDenied ? ( | ||
<Text style={styles.offlineFeedback.text}> | ||
<Text>{`${translate('location.permissionDenied')} ${translate('common.please')}`}</Text> | ||
<TextLink onPress={onAllowLocationLinkPress}>{` ${translate('location.allowPermission')} `}</TextLink> | ||
<Text>{translate('location.tryAgain')}</Text> | ||
</Text> | ||
) : ( | ||
<Text style={styles.offlineFeedback.text}>{translate('location.notFound')}</Text> | ||
)} | ||
</View> | ||
<View> | ||
<Tooltip text={translate('common.close')}> | ||
<PressableWithoutFeedback | ||
onPress={dismissError} | ||
style={[styles.touchableButtonImage]} | ||
accessibilityRole={CONST.ACCESSIBILITY_ROLE.BUTTON} | ||
accessibilityLabel={translate('common.close')} | ||
> | ||
<Icon src={Expensicons.Close} /> | ||
</PressableWithoutFeedback> | ||
</Tooltip> | ||
</View> | ||
</View> | ||
); | ||
} | ||
|
||
BaseLocationErrorMessage.displayName = 'BaseLocationErrorMessage'; | ||
BaseLocationErrorMessage.propTypes = propTypes; | ||
BaseLocationErrorMessage.defaultProps = defaultProps; | ||
export default compose( | ||
withOnyx({ | ||
locationErrorCode: { | ||
key: ONYXKEYS.LOCATION_ERROR_CODE, | ||
}, | ||
}), | ||
withLocalize, | ||
)(BaseLocationErrorMessage); |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react'; | ||
import {Linking} from 'react-native'; | ||
import CONST from '../../CONST'; | ||
import BaseLocationErrorMessage from './BaseLocationErrorMessage'; | ||
|
||
function LocationErrorMessage() { | ||
/** opens expensify help site in a new browser tab */ | ||
const navigateToExpensifyHelpSite = () => { | ||
Linking.openURL(CONST.NEWHELP_URL); | ||
}; | ||
|
||
return <BaseLocationErrorMessage onAllowLocationLinkPress={navigateToExpensifyHelpSite} />; | ||
} | ||
|
||
LocationErrorMessage.displayName = 'LocationErrorMessage'; | ||
export default LocationErrorMessage; |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react'; | ||
import {Linking} from 'react-native'; | ||
import BaseLocationErrorMessage from './BaseLocationErrorMessage'; | ||
|
||
function LocationErrorMessage() { | ||
/** opens app level settings from the system settings */ | ||
const openAppSettings = () => { | ||
Linking.openSettings(); | ||
}; | ||
|
||
return <BaseLocationErrorMessage onAllowLocationLinkPress={openAppSettings} />; | ||
} | ||
|
||
LocationErrorMessage.displayName = 'LocationErrorMessage'; | ||
export default LocationErrorMessage; |
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import PropTypes from 'prop-types'; | ||
import React, {useEffect, useRef} from 'react'; | ||
import {Text} from 'react-native'; | ||
import getCurrentPosition from '../libs/getCurrentPosition'; | ||
import * as User from '../libs/actions/User'; | ||
import styles from '../styles/styles'; | ||
import Icon from './Icon'; | ||
import * as Expensicons from './Icon/Expensicons'; | ||
import LocationErrorMessage from './LocationErrorMessage'; | ||
import withLocalize, {withLocalizePropTypes} from './withLocalize'; | ||
import colors from '../styles/colors'; | ||
import PressableWithFeedback from './Pressable/PressableWithFeedback'; | ||
|
||
const propTypes = { | ||
/** Callback that runs when location data is fetched */ | ||
onLocationFetched: PropTypes.func.isRequired, | ||
|
||
...withLocalizePropTypes, | ||
}; | ||
|
||
const defaultProps = {}; | ||
|
||
function UserCurrentLocationButton({onLocationFetched, translate}) { | ||
const isFetchingLocation = useRef(false); | ||
|
||
/** | ||
* handles error when failed to get user's current location | ||
* @param {Object} errorData | ||
* @param {Number} errorData.code | ||
*/ | ||
const onError = (errorData) => { | ||
isFetchingLocation.current = false; | ||
|
||
User.setLocationError(errorData.code); | ||
}; | ||
|
||
/** | ||
* handles success after getting user's current location | ||
* @param {Object} successData | ||
* @param {Object} successData.coords | ||
* @param {Number} successData.coords.longitude | ||
* @param {Number} successData.coords.latitude | ||
* @param {Number} successData.timestamp | ||
*/ | ||
const onSuccess = (successData) => { | ||
isFetchingLocation.current = false; | ||
|
||
User.clearLocationError(); | ||
|
||
onLocationFetched(successData); | ||
}; | ||
|
||
/** Gets the user's current location and registers success/error callbacks */ | ||
const useCurrentLocation = () => { | ||
if (isFetchingLocation.current) return; | ||
|
||
isFetchingLocation.current = true; | ||
|
||
getCurrentPosition(onSuccess, onError); | ||
}; | ||
|
||
useEffect(() => { | ||
// clear location errors on mount | ||
User.clearLocationError(); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<PressableWithFeedback | ||
style={[styles.flexRow, styles.mt4]} | ||
onPress={useCurrentLocation} | ||
accessibilityLabel={translate('location.useCurrent')} | ||
> | ||
<Icon | ||
src={Expensicons.Location} | ||
fill={colors.green} | ||
/> | ||
<Text style={[styles.textLabel, styles.mh2]}>{translate('location.useCurrent')}</Text> | ||
</PressableWithFeedback> | ||
<LocationErrorMessage /> | ||
</> | ||
); | ||
} | ||
|
||
UserCurrentLocationButton.displayName = 'UserCurrentLocationButton'; | ||
UserCurrentLocationButton.propTypes = propTypes; | ||
UserCurrentLocationButton.defaultProps = defaultProps; | ||
export default withLocalize(UserCurrentLocationButton); |
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
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
Oops, something went wrong.