Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear user location cache when they denied location permission #42509

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/AddressSearch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function AddressSearch(
}

setIsFetchingCurrentLocation(false);
setLocationErrorCode(errorData.code);
setLocationErrorCode(errorData?.code ?? null);
},
{
maximumAge: 0, // No cache, always get fresh location info
Expand Down
23 changes: 14 additions & 9 deletions src/components/MapView/MapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {forwardRef, memo, useCallback, useEffect, useImperativeHandle, useRef, u
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import useThemeStyles from '@hooks/useThemeStyles';
import setUserLocation from '@libs/actions/UserLocation';
import * as UserLocation from '@libs/actions/UserLocation';
import compose from '@libs/compose';
import getCurrentPosition from '@libs/getCurrentPosition';
import type {GeolocationErrorCallback} from '@libs/getCurrentPosition/getCurrentPosition.types';
import {GeolocationErrorCode} from '@libs/getCurrentPosition/getCurrentPosition.types';
import CONST from '@src/CONST';
import useLocalize from '@src/hooks/useLocalize';
import useNetwork from '@src/hooks/useNetwork';
Expand Down Expand Up @@ -38,13 +40,16 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(
// if there are one or more waypoints present.
const shouldPanMapToCurrentPosition = useCallback(() => !userInteractedWithMap && (!waypoints || waypoints.length === 0), [userInteractedWithMap, waypoints]);

const setCurrentPositionToInitialState = useCallback(() => {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (cachedUserLocation || !initialState) {
return;
}
setCurrentPosition({longitude: initialState.location[0], latitude: initialState.location[1]});
}, [initialState, cachedUserLocation]);
const setCurrentPositionToInitialState: GeolocationErrorCallback = useCallback(
(error) => {
if (error?.code !== GeolocationErrorCode.PERMISSION_DENIED || !initialState) {
return;
}
UserLocation.clearUserLocation();
setCurrentPosition({longitude: initialState.location[0], latitude: initialState.location[1]});
},
[initialState],
);

useFocusEffect(
useCallback(() => {
Expand All @@ -66,7 +71,7 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(
getCurrentPosition((params) => {
const currentCoords = {longitude: params.coords.longitude, latitude: params.coords.latitude};
setCurrentPosition(currentCoords);
setUserLocation(currentCoords);
UserLocation.setUserLocation(currentCoords);
}, setCurrentPositionToInitialState);
}, [isOffline, shouldPanMapToCurrentPosition, setCurrentPositionToInitialState]),
);
Expand Down
23 changes: 14 additions & 9 deletions src/components/MapView/MapView.website.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {withOnyx} from 'react-native-onyx';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import setUserLocation from '@userActions/UserLocation';
import type {GeolocationErrorCallback} from '@libs/getCurrentPosition/getCurrentPosition.types';
import {GeolocationErrorCode} from '@libs/getCurrentPosition/getCurrentPosition.types';
import * as UserLocation from '@userActions/UserLocation';
import CONST from '@src/CONST';
import useLocalize from '@src/hooks/useLocalize';
import useNetwork from '@src/hooks/useNetwork';
Expand Down Expand Up @@ -62,13 +64,16 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(
// if there are one or more waypoints present.
const shouldPanMapToCurrentPosition = useCallback(() => !userInteractedWithMap && (!waypoints || waypoints.length === 0), [userInteractedWithMap, waypoints]);

const setCurrentPositionToInitialState = useCallback(() => {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (cachedUserLocation || !initialState) {
return;
}
setCurrentPosition({longitude: initialState.location[0], latitude: initialState.location[1]});
}, [initialState, cachedUserLocation]);
const setCurrentPositionToInitialState: GeolocationErrorCallback = useCallback(
(error) => {
if (error?.code !== GeolocationErrorCode.PERMISSION_DENIED || !initialState) {
return;
}
UserLocation.clearUserLocation();
setCurrentPosition({longitude: initialState.location[0], latitude: initialState.location[1]});
},
[initialState],
);

useFocusEffect(
useCallback(() => {
Expand All @@ -90,7 +95,7 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(
getCurrentPosition((params) => {
const currentCoords = {longitude: params.coords.longitude, latitude: params.coords.latitude};
setCurrentPosition(currentCoords);
setUserLocation(currentCoords);
UserLocation.setUserLocation(currentCoords);
}, setCurrentPositionToInitialState);
}, [isOffline, shouldPanMapToCurrentPosition, setCurrentPositionToInitialState]),
);
Expand Down
6 changes: 5 additions & 1 deletion src/libs/actions/UserLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ function setUserLocation({longitude, latitude}: UserLocation) {
Onyx.set(ONYXKEYS.USER_LOCATION, {longitude, latitude});
}

export default setUserLocation;
function clearUserLocation() {
Onyx.set(ONYXKEYS.USER_LOCATION, null);
}

export {setUserLocation, clearUserLocation};
2 changes: 1 addition & 1 deletion src/libs/getCurrentPosition/getCurrentPosition.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type GeolocationSuccessCallback = (position: {

type GeolocationErrorCodeType = ValueOf<typeof GeolocationErrorCode> | null;

type GeolocationErrorCallback = (error: {
type GeolocationErrorCallback = (error?: {
code: GeolocationErrorCodeType;
message: string;
PERMISSION_DENIED: typeof GeolocationErrorCode.PERMISSION_DENIED;
Expand Down
Loading