forked from kdt-8-4/Weatherfit_frontend_refactoring
-
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.
Merge pull request kdt-8-4#85 from kdt-8-4/performanceimprove
[Fix] 피드 페이지 날씨 바 로직 개선 PerformanceImprove kdt-8-4#82 Feed kdt-8-4#7
- Loading branch information
Showing
3 changed files
with
132 additions
and
162 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 |
---|---|---|
@@ -1,101 +1,109 @@ | ||
import { ReactNode, createContext, useState } from "react"; | ||
'use client' | ||
import { ReactNode, createContext, useEffect, useState } from 'react' | ||
|
||
interface WeatherProvider { | ||
icon : string | undefined; | ||
tempNow : number | undefined; | ||
tempMax : number | undefined; | ||
tempMin : number | undefined; | ||
icon: string | undefined | ||
tempNow: number | undefined | ||
tempMax: number | undefined | ||
tempMin: number | undefined | ||
} | ||
|
||
export const WeatherContext = createContext<WeatherProvider | undefined>(undefined); | ||
export const WeatherContext = createContext<WeatherProvider | undefined>( | ||
undefined, | ||
) | ||
|
||
export const WeatherProvider = ({children}: {children: ReactNode}) => { | ||
|
||
const [ temperature, setTemp ] = useState<number>(); | ||
const [ temperatureMin, setTempMin ] = useState<number>(); | ||
const [ temperatureMax, setTempMax ] = useState<number>(); | ||
const [ weatherIcon, setIcon ] = useState<string>(); | ||
const [ latitude_state, setLatitude ] = useState<number>(); | ||
const [ longitude_state, setLongitude ] = useState<number>(); | ||
const [address, setAddress] = useState<string | undefined>() | ||
export const WeatherProvider = ({ children }: { children: ReactNode }) => { | ||
const [temperature, setTemp] = useState<number>() | ||
const [temperatureMin, setTempMin] = useState<number>() | ||
const [temperatureMax, setTempMax] = useState<number>() | ||
const [weatherIcon, setIcon] = useState<string>() | ||
const [latitude_state, setLatitude] = useState<number>() | ||
const [longitude_state, setLongitude] = useState<number>() | ||
const [address, setAddress] = useState<string | undefined>() | ||
|
||
// API Keys | ||
// openweathermap | ||
const API_KEY:string = "e88170b7fa2aeadd4ba37bb1561a53f2" | ||
// KaKao | ||
const KAKAO_API_KEY:string = "3a6c3035c801405eaa71ebb9dc7f474b" | ||
// API Keys | ||
// openweathermap | ||
const API_KEY: string = 'e88170b7fa2aeadd4ba37bb1561a53f2' | ||
// KaKao | ||
const KAKAO_API_KEY: string = '3a6c3035c801405eaa71ebb9dc7f474b' | ||
|
||
useEffect(() => { | ||
const getLocation = async () => { | ||
try { | ||
// 위치 노출이 허용되어 있는지 확인하고 현재 위치 가져오기 | ||
const position = await new Promise<GeolocationPosition>( | ||
(resolve, reject) => { | ||
navigator.geolocation.getCurrentPosition(resolve, reject) | ||
}, | ||
) | ||
|
||
const getLocation = async() => { | ||
try { | ||
// 위치 노출이 허용되어 있는지 확인하고 현재 위치 가져오기 | ||
const position = await new Promise<GeolocationPosition>( | ||
(resolve, reject) => { | ||
navigator.geolocation.getCurrentPosition(resolve, reject) | ||
}, | ||
); | ||
|
||
// 위도와 경도 가져오기 | ||
setLatitude(position.coords.latitude) | ||
setLongitude(position.coords.longitude) | ||
|
||
} catch (error) { | ||
console.error("Error getting location:", error); | ||
} | ||
// 위도와 경도 가져오기 | ||
setLatitude(position.coords.latitude) | ||
setLongitude(position.coords.longitude) | ||
} catch (error) { | ||
console.error('Error getting location:', error) | ||
} | ||
} | ||
|
||
const getWeather = async() => { | ||
try { | ||
// OpenWeatherMap에서 위치 기반 날씨 정보 불러오기 | ||
const weatherResponse = await fetch( | ||
`https://api.openweathermap.org/data/2.5/weather?lat=${latitude_state}&lon=${longitude_state}&appid=${API_KEY}&units=metric` | ||
); | ||
const weatherData = await weatherResponse.json(); | ||
|
||
// 날씨 정보 저장하기 | ||
setTemp(weatherData.main.temp.toFixed(1)); | ||
setTempMax(weatherData.main.temp_max.toFixed(1)); | ||
setTempMin(weatherData.main.temp_min.toFixed(1)); | ||
setIcon(weatherData.weather[0].icon); | ||
getLocation() | ||
}, []) | ||
|
||
useEffect(() => { | ||
const getWeather = async () => { | ||
console.log('위도', latitude_state, '경도', longitude_state) | ||
try { | ||
// OpenWeatherMap에서 위치 기반 날씨 정보 불러오기 | ||
const weatherResponse = await fetch( | ||
`https://api.openweathermap.org/data/2.5/weather?lat=${latitude_state}&lon=${longitude_state}&appid=${API_KEY}&units=metric`, | ||
) | ||
const weatherData = await weatherResponse.json() | ||
|
||
} catch (error) { | ||
console.error("Error getting location:", error); | ||
} | ||
// 날씨 정보 저장하기 | ||
setIcon(weatherData.weather[0].icon) | ||
setTemp(weatherData.main.temp.toFixed(1)) | ||
setTempMax(weatherData.main.temp_max.toFixed(1)) | ||
setTempMin(weatherData.main.temp_min.toFixed(1)) | ||
} catch (error) { | ||
console.error('Error getting location:', error) | ||
} | ||
} | ||
|
||
const getAddress = async() => { | ||
try { | ||
const addressResponse = await fetch( | ||
`https://dapi.kakao.com/v2/local/geo/coord2address.json?x=${longitude_state}&y=${latitude_state}`, | ||
{ | ||
method: "GET", | ||
headers: { Authorization: `KakaoAK ${KAKAO_API_KEY}` } | ||
}, | ||
); | ||
const addressData = await addressResponse.json(); | ||
setAddress( | ||
addressData.documents[0].address.region_1depth_name + | ||
" " + | ||
addressData.documents[0].address.region_2depth_name, | ||
); | ||
|
||
} catch (error) { | ||
console.error("Error getting location:", error); | ||
} | ||
if (latitude_state && longitude_state) getWeather() | ||
}, [latitude_state, longitude_state]) | ||
|
||
useEffect(() => { | ||
const getAddress = async () => { | ||
try { | ||
const addressResponse = await fetch( | ||
`https://dapi.kakao.com/v2/local/geo/coord2address.json?x=${longitude_state}&y=${latitude_state}`, | ||
{ | ||
method: 'GET', | ||
headers: { Authorization: `KakaoAK ${KAKAO_API_KEY}` }, | ||
}, | ||
) | ||
const addressData = await addressResponse.json() | ||
setAddress( | ||
addressData.documents[0].address.region_1depth_name + | ||
' ' + | ||
addressData.documents[0].address.region_2depth_name, | ||
) | ||
} catch (error) { | ||
console.error('Error getting location:', error) | ||
} | ||
} | ||
|
||
getLocation(); | ||
getAddress(); | ||
getAddress(); | ||
return ( | ||
<WeatherContext.Provider value={{ | ||
icon : weatherIcon, | ||
tempMax : temperatureMax, | ||
tempMin : temperatureMin, | ||
tempNow : temperature | ||
}}> | ||
{children} | ||
</WeatherContext.Provider> | ||
) | ||
} | ||
if (address) getAddress() | ||
}, [temperatureMin]) | ||
|
||
return ( | ||
<WeatherContext.Provider | ||
value={{ | ||
icon: weatherIcon, | ||
tempMax: temperatureMax, | ||
tempMin: temperatureMin, | ||
tempNow: temperature, | ||
}}> | ||
{children} | ||
</WeatherContext.Provider> | ||
) | ||
} |
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