diff --git a/packages/react-native/src/apis/queries/home/useNearbySoptQuery.ts b/packages/react-native/src/apis/queries/home/useNearbySoptQuery.ts index 380465cf..d2a0e3f3 100644 --- a/packages/react-native/src/apis/queries/home/useNearbySoptQuery.ts +++ b/packages/react-native/src/apis/queries/home/useNearbySoptQuery.ts @@ -11,11 +11,10 @@ export default function useNeearbySpotQuery() { const location = useLocation(); const getNearbySpot = async () => { - const res = await location; - if (!res || !res.latitude || !res.longitude) return []; + if (!location || !location.latitude || !location.longitude) return []; const response = await authAxios.get>( - `/api/spot/nearby?latitude=${res.latitude}&longitude=${res.longitude}`, + `/api/spot/nearby?latitude=${location.latitude}&longitude=${location.longitude}`, ); return response.data.result; diff --git a/packages/react-native/src/components/common/SearchBar.tsx b/packages/react-native/src/components/common/SearchBar.tsx index e03b7a16..32eb3b98 100644 --- a/packages/react-native/src/components/common/SearchBar.tsx +++ b/packages/react-native/src/components/common/SearchBar.tsx @@ -17,6 +17,7 @@ export default function SearchBar({ if (!searchKeyword) return; handleSearch(searchKeyword); + setSearchKeyword(''); }; return ( diff --git a/packages/react-native/src/hooks/useGeolocation.ts b/packages/react-native/src/hooks/useGeolocation.ts index 6486a6c1..4c1a7b41 100644 --- a/packages/react-native/src/hooks/useGeolocation.ts +++ b/packages/react-native/src/hooks/useGeolocation.ts @@ -10,6 +10,11 @@ import { request, } from 'react-native-permissions'; +export interface Location { + latitude?: number; + longitude?: number; +} + export default function useGeolocation() { const checkLocationPermission = (result: PermissionStatus) => { switch (result) { @@ -48,7 +53,7 @@ export default function useGeolocation() { return checkLocationPermission(permission) === 'granted'; }; - const getGeolocation = async () => { + const getGeolocation: () => Promise = async () => { const hasPermission = await hasLocationPermission(); if (!hasPermission) { return null; @@ -57,7 +62,11 @@ export default function useGeolocation() { const result = await new Promise((resolve) => { Geolocation.getCurrentPosition((pos) => resolve(pos)); }); - return result; + + return { + latitude: result.coords.latitude, + longitude: result.coords.longitude, + }; }; return { getGeolocation }; diff --git a/packages/react-native/src/hooks/useLocation.ts b/packages/react-native/src/hooks/useLocation.ts index 9feeec9f..996af053 100644 --- a/packages/react-native/src/hooks/useLocation.ts +++ b/packages/react-native/src/hooks/useLocation.ts @@ -1,22 +1,18 @@ -import useGeolocation from './useGeolocation'; - -export interface Location { - latitude?: number; - longitude?: number; -} +import { useEffect, useState } from 'react'; +import useGeolocation, { Location } from './useGeolocation'; export default function useLocation() { const { getGeolocation } = useGeolocation(); - const fetchLocation = async (): Promise => { - const res = await getGeolocation(); - if (!res) return undefined; + const [location, setLocation] = useState(); - return { - latitude: res.coords.latitude, - longitude: res.coords.longitude, - }; - }; + useEffect(() => { + getGeolocation().then((res) => { + if (res) { + setLocation(res); + } + }); + }, []); - return fetchLocation(); // Promise 반환 + return location; } diff --git a/packages/react-native/src/pages/Detail/DetailSpot.tsx b/packages/react-native/src/pages/Detail/DetailSpot.tsx index e15b5cd8..c845a504 100644 --- a/packages/react-native/src/pages/Detail/DetailSpot.tsx +++ b/packages/react-native/src/pages/Detail/DetailSpot.tsx @@ -20,7 +20,12 @@ export default withSuspense( const { list, toggleItem, reset } = useArrayToggle(); const navigation = useNavigation>(); - const { contentId, workId } = route.params; + const { contentId, workId, currentRoute } = route.params as { + contentId: number; + id: number; + workId: number; + currentRoute: string; + }; const { data } = useAroundSpotQuery({ id: contentId, workId }); @@ -135,7 +140,10 @@ export default withSuspense( bottom={16} right={16} onPress={() => { - navigation.navigate('Home/AddSpot', { + const nextRoute = `${currentRoute.split('/')[0]}/AddSpot` as + | 'Home/AddSpot' + | 'Mypage/AddSpot'; + navigation.navigate(nextRoute, { spots: list, }); }} diff --git a/packages/react-native/src/routes/DetailTabNavigator.tsx b/packages/react-native/src/routes/DetailTabNavigator.tsx index f0832f75..089dbcb0 100644 --- a/packages/react-native/src/routes/DetailTabNavigator.tsx +++ b/packages/react-native/src/routes/DetailTabNavigator.tsx @@ -49,7 +49,7 @@ export default function DetailTabNavigator() { name="Detail/Spot" component={DetailSpot} options={{ tabBarLabel: 'SPOT!', swipeEnabled: false }} - initialParams={{ id, contentId, workId }} + initialParams={{ id, contentId, workId, currentRoute: route.name }} /> );