Skip to content

Commit

Permalink
[Chore]: Update user location when fetching current location in Addre…
Browse files Browse the repository at this point in the history
…ssSearch component
  • Loading branch information
fedirjh committed Jun 19, 2024
1 parent 904d49d commit f31abe9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
10 changes: 8 additions & 2 deletions src/components/AddressSearch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import useNetwork from '@hooks/useNetwork';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import * as UserLocation from '@libs/actions/UserLocation';
import * as ApiUtils from '@libs/ApiUtils';
import getCurrentPosition from '@libs/getCurrentPosition';
import type {GeolocationErrorCodeType} from '@libs/getCurrentPosition/getCurrentPosition.types';
Expand Down Expand Up @@ -249,12 +250,17 @@ function AddressSearch(
setIsFetchingCurrentLocation(false);
setLocationErrorCode(null);

const {latitude, longitude} = successData.coords;

const location = {
lat: successData.coords.latitude,
lng: successData.coords.longitude,
lat: latitude,
lng: longitude,
address: CONST.YOUR_LOCATION_TEXT,
name: CONST.YOUR_LOCATION_TEXT,
};

// Update the current user location
UserLocation.setUserLocation({longitude, latitude});
onPress?.(location);
},
(errorData) => {
Expand Down
6 changes: 2 additions & 4 deletions src/components/MapView/MapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type {ComponentProps, MapViewOnyxProps} from './types';
import utils from './utils';

const MapView = forwardRef<MapViewHandle, ComponentProps>(
({accessToken, style, mapPadding, userLocation: cachedUserLocation, styleURL, pitchEnabled, initialState, waypoints, directionCoordinates, onMapReady, interactive = true}, ref) => {
({accessToken, style, mapPadding, userLocation, styleURL, pitchEnabled, initialState, waypoints, directionCoordinates, onMapReady, interactive = true}, ref) => {
const navigation = useNavigation();
const {isOffline} = useNetwork();
const {translate} = useLocalize();
Expand All @@ -34,7 +34,7 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(
const cameraRef = useRef<Mapbox.Camera>(null);
const [isIdle, setIsIdle] = useState(false);
const initialLocation = useMemo(() => initialState && {longitude: initialState.location[0], latitude: initialState.location[1]}, [initialState]);
const [currentPosition, setCurrentPosition] = useState(cachedUserLocation ?? initialLocation);
const currentPosition = userLocation ?? initialLocation;
const [userInteractedWithMap, setUserInteractedWithMap] = useState(false);
const shouldInitializeCurrentPosition = useRef(true);

Expand All @@ -50,7 +50,6 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(
return;
}
UserLocation.clearUserLocation();
setCurrentPosition(initialLocation);
},
[initialLocation],
);
Expand All @@ -74,7 +73,6 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(

getCurrentPosition((params) => {
const currentCoords = {longitude: params.coords.longitude, latitude: params.coords.latitude};
setCurrentPosition(currentCoords);
UserLocation.setUserLocation(currentCoords);
}, setCurrentPositionToInitialState);
}, [isOffline, shouldPanMapToCurrentPosition, setCurrentPositionToInitialState]),
Expand Down
6 changes: 2 additions & 4 deletions src/components/MapView/MapView.website.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(
waypoints,
mapPadding,
accessToken,
userLocation: cachedUserLocation,
userLocation,
directionCoordinates,
initialState = {location: CONST.MAPBOX.DEFAULT_COORDINATE, zoom: CONST.MAPBOX.DEFAULT_ZOOM},
interactive = true,
Expand All @@ -55,7 +55,7 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(

const [mapRef, setMapRef] = useState<MapRef | null>(null);
const initialLocation = useMemo(() => ({longitude: initialState.location[0], latitude: initialState.location[1]}), [initialState]);
const [currentPosition, setCurrentPosition] = useState(cachedUserLocation ?? initialLocation);
const currentPosition = userLocation ?? initialLocation;
const [userInteractedWithMap, setUserInteractedWithMap] = useState(false);
const [shouldResetBoundaries, setShouldResetBoundaries] = useState<boolean>(false);
const setRef = useCallback((newRef: MapRef | null) => setMapRef(newRef), []);
Expand All @@ -73,7 +73,6 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(
return;
}
UserLocation.clearUserLocation();
setCurrentPosition(initialLocation);
},
[initialLocation],
);
Expand All @@ -97,7 +96,6 @@ const MapView = forwardRef<MapViewHandle, ComponentProps>(

getCurrentPosition((params) => {
const currentCoords = {longitude: params.coords.longitude, latitude: params.coords.latitude};
setCurrentPosition(currentCoords);
UserLocation.setUserLocation(currentCoords);
}, setCurrentPositionToInitialState);
}, [isOffline, shouldPanMapToCurrentPosition, setCurrentPositionToInitialState]),
Expand Down

0 comments on commit f31abe9

Please sign in to comment.