Skip to content

Commit

Permalink
Merge pull request #24 from Fashion-Cloud/refact/#19
Browse files Browse the repository at this point in the history
refact: Location 조회 api 호출 훅 리팩토링
  • Loading branch information
HyunTaek5 authored Oct 7, 2023
2 parents a232abf + c245e31 commit 9c5145f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 49 deletions.
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>
);
}

0 comments on commit 9c5145f

Please sign in to comment.