-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
refact: Location 조회 api 호출 훅 리팩토링
- Loading branch information
Showing
4 changed files
with
43 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useQuery } from 'react-query'; | ||
import useGeoLocation from 'src/assets/hooks/useGeoLocation'; | ||
|
||
import { LocationService } from '../service/LocationService'; | ||
|
||
export const useFindAllLocations = () => { | ||
const location = useGeoLocation(); | ||
|
||
const longitude = location?.coordinates?.lng; | ||
const latitude = location?.coordinates?.lat; | ||
|
||
return useQuery({ | ||
queryFn: () => LocationService.getAllLocations({ latitude, longitude }), | ||
queryKey: ['locations'], | ||
enabled: !!longitude && !!latitude, | ||
onError: () => alert('위치 정보를 불러오는데 실패했습니다.'), | ||
}); | ||
}; |
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,12 @@ | ||
import apiV1Instance from 'src/api/api-instance'; | ||
|
||
export class LocationService { | ||
public static getAllLocations = async (options: { | ||
latitude?: number; | ||
longitude?: number; | ||
}) => { | ||
return apiV1Instance.get( | ||
`/address?latitude=${options.latitude}&longitude=${options.longitude}` | ||
); | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
import PlaceIcon from '@mui/icons-material/Place'; | ||
import { Toolbar, Typography } from '@mui/material'; | ||
import { useRecoilValue } from 'recoil'; | ||
|
||
import { locationDataState } from '../../../utils/Recoil'; | ||
import { useEffect, useState } from 'react'; | ||
import { useFindAllLocations } from 'src/api/hook/LocationHook'; | ||
import { LocationType } from 'src/utils/types'; | ||
|
||
export default function PlaceBox() { | ||
const locationData = useRecoilValue(locationDataState); | ||
const [location, setLocation] = useState<LocationType>(); | ||
const { data: locationData } = useFindAllLocations(); | ||
useEffect(() => { | ||
if (locationData?.data) { | ||
setLocation(locationData?.data?.data); | ||
} | ||
}, [locationData]); | ||
|
||
return ( | ||
<Toolbar sx={{ justifyContent: 'center', mt: 10 }}> | ||
<PlaceIcon sx={{ mr: 0.7 }} /> | ||
<Typography sx={{ fontSize: '16pt' }}> | ||
{locationData?.fullAddress} | ||
</Typography> | ||
<Typography sx={{ fontSize: '16pt' }}>{location?.fullAddress}</Typography> | ||
</Toolbar> | ||
); | ||
} |