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

refact: Location 조회 api 호출 훅 리팩토링 #24

Merged
merged 3 commits into from
Oct 7, 2023
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
18 changes: 18 additions & 0 deletions src/api/hook/LocationHook.ts
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('위치 정보를 불러오는데 실패했습니다.'),
});
};
12 changes: 12 additions & 0 deletions src/api/service/LocationService.ts
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}`
);
};
}
44 changes: 2 additions & 42 deletions src/components/MainLayout/InfoBox.tsx
Original file line number Diff line number Diff line change
@@ -1,62 +1,22 @@
import { Box } from '@mui/material';
import axios from 'axios';
import { useEffect } from 'react';
import { useSetRecoilState } from 'recoil';
import { useFindAllWeathers } from 'src/api/hook/WeatherHook';
import {token} from 'src/assets/data/token';

import useGeoLocation from '../../assets/hooks/useGeoLocation';
import { locationDataState, weatherDataState } from '../../utils/Recoil';
import { weatherDataState } from '../../utils/Recoil';
import PlaceBox from './InfoBox/PlaceBox';
import WeatherBox from './InfoBox/WeatherBox';

export default function InfoBox() {
const setWeatherData = useSetRecoilState(weatherDataState);
const setLocationData = useSetRecoilState(locationDataState);

const location = useGeoLocation();

let latitude: number | undefined;
let longitude: number | undefined;

const { data: windChillData } = useFindAllWeathers();

const locationAPI = async () => {
if (location !== undefined) {
try {
await axios
.get(`/api/v1/address?latitude=${latitude}&longitude=${longitude}`, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
},
})
.then((response) => {
console.log('response: ', response);
const data = response.data.data;
console.log('[WeatherTemp] locationAPI: ', data);
setLocationData(data);
});
} catch {
console.log('[WeatherTemp] locationAPI: api 불러오기 실패');
}
}
};

useEffect(() => {
if (location?.coordinates) {
latitude = location.coordinates?.lat;
longitude = location.coordinates?.lng;
console.log('[GeoLocation] latitude: ', latitude);
console.log('[GeoLocation] longitude: ', longitude);

locationAPI();
}

if (windChillData?.data) {
setWeatherData(windChillData?.data?.data);
}
}, [location, windChillData]);
}, [windChillData]);

return (
<Box
Expand Down
18 changes: 11 additions & 7 deletions src/components/MainLayout/InfoBox/PlaceBox.tsx
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>
);
}
Loading