diff --git a/src/App.jsx b/src/App.jsx index 90355b9..119e64c 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,15 +1,125 @@ -import logo from "/logo.png"; import "./App.css"; +import axios from "axios"; +import { useState } from "react"; + +const apiKey = "04c67599a65508e4e65f4773b3e5a761"; function App() { + const [cityInput, setCityInput] = useState(""); + const [cityName, setCityName] = useState(""); + const [weather, setWeather] = useState(""); + const [weatherTemp, setWeatherTemp] = useState(""); + const [weatherIcon, setWeatherIcon] = useState(""); + const [weatherDesc, setWeatherDesc] = useState(""); + const [weatherForecast, setWeatherForecast] = useState(null); + + const handleSubmit = (event) => { + event.preventDefault(); + // uses GET request to a specified URL + axios + .get( + `http://api.openweathermap.org/geo/1.0/direct?q=${cityInput}&limit=1&appid=${apiKey}` + ) + .then((response) => response.data[0]) + .then((cityGeoData) => + axios.get( + `https://api.openweathermap.org/data/2.5/weather?lat=${cityGeoData.lat}&lon=${cityGeoData.lon}&appid=${apiKey}&units=metric` + ) + ) + .then((response) => { + const { data: weatherData } = response; + console.log(weatherData); + + setCityInput(""); + setCityName(weatherData.name); + setWeather(weatherData.weather[0].main); + setWeatherTemp(weatherData.main.temp); + setWeatherDesc(weatherData.weather[0].description); + setWeatherIcon(weatherData.weather[0].icon); + }); + + axios + .get( + `http://api.openweathermap.org/geo/1.0/direct?q=${cityInput}&limit=1&appid=${apiKey}` + ) + .then((response) => response.data[0]) + .then((cityGeoData) => + axios + .get( + `https://api.openweathermap.org/data/2.5/forecast?lat=${cityGeoData.lat}&lon=${cityGeoData.lon}&appid=${apiKey}&units=metric` + ) + .then((response) => { + const { data: weatherForecastData } = response; + setWeatherForecast(weatherForecastData); + console.log(weatherForecastData); + }) + ); + }; + + const weatherDisplay = cityName ? ( +
Current City: {cityName}
+Current Temperature: {weatherTemp}°C
+Current Weather: {weatherDesc}
+Key in a city name to see the weather
+ ); + + const WeatherForecastData = ({ weatherForecast }) => { + if (!weatherForecast) return null; + return ( +Date | +Temperature | +Weather | +Icon | +
---|---|---|---|
{new Date(item.dt * 1000).toLocaleString()} | +{item.main.temp}°C | +{item.weather[0].description} | ++ + | +