Skip to content

Commit

Permalink
Add Weather API
Browse files Browse the repository at this point in the history
  • Loading branch information
sehee0207 committed Feb 17, 2024
1 parent 74b7ec4 commit aa8d8fe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
44 changes: 41 additions & 3 deletions src/components/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { styled } from "styled-components";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faCircleInfo, faClose } from "@fortawesome/free-solid-svg-icons";

Check failure on line 3 in src/components/Modal.tsx

View workflow job for this annotation

GitHub Actions / build

'faCircleInfo' is declared but its value is never read.

Check failure on line 3 in src/components/Modal.tsx

View workflow job for this annotation

GitHub Actions / build

'faCircleInfo' is declared but its value is never read.
import { useEffect, useState } from "react";

interface ModalProps{
title: string | null | undefined;
Expand Down Expand Up @@ -33,7 +34,7 @@ const ModalContainer = styled.div`
display: flex;
flex-direction: column;
border-radius: 5px;
padding: 30px 0px;
padding: 30px 30px;
border: 1px solid white;
background-color: white;
position: relative;
Expand All @@ -44,6 +45,7 @@ const ModalContainer = styled.div`

const CloseButtonStyle = styled.div`
display: flex;
justify-content: flex-end;
:hover{
cursor: pointer;
Expand All @@ -54,8 +56,35 @@ const InfoContainerStyle = styled.div`
`

const WeatherContainerStyle = styled.div`
`

const WeatherDivStyle = styled.div`
padding: 15px 0px 0px 0px;
`


const Modal = ({ title, latlng, onClick }: ModalProps) =>{
const [weatherData, setWeatherData] = useState<any>(null);

useEffect(() => {
if(latlng){
//onecall?lat=${latlng.lat()}&lon=${latlng.lng()}&exclude=current&appid=${import.meta.env.VITE_WEATHER_API_KEY}`
fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${latlng.lat()}&lon=${latlng.lng()}&appid=${import.meta.env.VITE_WEATHER_API_KEY}`)
.then(response => response.json())
.then(data => setWeatherData(data))
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
}
}, [latlng]);

const getWindDirection = (deg: number) => {
const directions = ['북', '북동', '동', '남동', '남', '남서', '서', '북서'];
const value = Math.floor((deg + 22.5) / 45);
return directions[value % 8];
};

return (
<ModalWrap>
<ModalBackGround onClick={onClick} />
Expand All @@ -65,9 +94,18 @@ const Modal = ({ title, latlng, onClick }: ModalProps) =>{
</CloseButtonStyle>

<InfoContainerStyle>
여기는 {title && (<h4>{title}</h4>)}
{title && (<h5>{title}</h5>)}
{latlng && (
<p>위도: {latlng.lat()}, 경도: {latlng.lng()}</p>
<h5>위도: {latlng.lat()}, 경도: {latlng.lng()}</h5>
)}

{weatherData && (
<WeatherContainerStyle>
<WeatherDivStyle>날씨 정보 : {weatherData.weather[0].main}</WeatherDivStyle>
<WeatherDivStyle>습도 : {weatherData.main.humidity} %</WeatherDivStyle>
<WeatherDivStyle>풍향 : {getWindDirection(weatherData.wind.deg)}</WeatherDivStyle>
<WeatherDivStyle>풍속 : {weatherData.wind.speed} m/s</WeatherDivStyle>
</WeatherContainerStyle>
)}
</InfoContainerStyle>
</ModalContainer>
Expand Down
4 changes: 1 addition & 3 deletions src/pages/Signuppage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ const Signup = () => {
const idCheck = () => {
axios.get(`http://localhost:8080/api/check/id?userId=${id}`)
.then(response => {
if(!response.data){
setIdConfirm(false);
}
setIdConfirm(response.data);
})
.catch(error => {
console.error('Error:', error);
Expand Down
2 changes: 1 addition & 1 deletion src/styles/GlobalStyle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const GlobalStyle = createGlobalStyle`
margin-top: 0px;
}
h6{
h5, h6{
margin: 0px;
}
Expand Down

0 comments on commit aa8d8fe

Please sign in to comment.