Skip to content

Commit

Permalink
merge: qa1011 (#106)
Browse files Browse the repository at this point in the history
merge: qa1011 (#106)
  • Loading branch information
d0422 authored Oct 11, 2024
2 parents 0444939 + 6d423b7 commit 696c82b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServerResponse<MySpotResponse[]>>(
`/api/spot/nearby?latitude=${res.latitude}&longitude=${res.longitude}`,
`/api/spot/nearby?latitude=${location.latitude}&longitude=${location.longitude}`,
);

return response.data.result;
Expand Down
1 change: 1 addition & 0 deletions packages/react-native/src/components/common/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function SearchBar({
if (!searchKeyword) return;

handleSearch(searchKeyword);
setSearchKeyword('');
};

return (
Expand Down
13 changes: 11 additions & 2 deletions packages/react-native/src/hooks/useGeolocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -48,7 +53,7 @@ export default function useGeolocation() {
return checkLocationPermission(permission) === 'granted';
};

const getGeolocation = async () => {
const getGeolocation: () => Promise<Location | null> = async () => {
const hasPermission = await hasLocationPermission();
if (!hasPermission) {
return null;
Expand All @@ -57,7 +62,11 @@ export default function useGeolocation() {
const result = await new Promise<GeolocationResponse>((resolve) => {
Geolocation.getCurrentPosition((pos) => resolve(pos));
});
return result;

return {
latitude: result.coords.latitude,
longitude: result.coords.longitude,
};
};

return { getGeolocation };
Expand Down
26 changes: 11 additions & 15 deletions packages/react-native/src/hooks/useLocation.ts
Original file line number Diff line number Diff line change
@@ -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<Location | undefined> => {
const res = await getGeolocation();
if (!res) return undefined;
const [location, setLocation] = useState<Location>();

return {
latitude: res.coords.latitude,
longitude: res.coords.longitude,
};
};
useEffect(() => {
getGeolocation().then((res) => {
if (res) {
setLocation(res);
}
});
}, []);

return fetchLocation(); // Promise 반환
return location;
}
12 changes: 10 additions & 2 deletions packages/react-native/src/pages/Detail/DetailSpot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export default withSuspense(
const { list, toggleItem, reset } = useArrayToggle<SpotResponse>();
const navigation = useNavigation<StackNavigation<'Home/Detail'>>();

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 });

Expand Down Expand Up @@ -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,
});
}}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/src/routes/DetailTabNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
/>
</Tab.Navigator>
);
Expand Down

0 comments on commit 696c82b

Please sign in to comment.