commit 2ea1abec0254235dc403744ac998b5d870b505e6 Author: AndrewTrieu Date: Thu Jan 5 21:27:30 2023 +0200 Repush diff --git a/README.md b/README.md new file mode 100644 index 0000000..df51e38 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Simple Weather App + +This app allows users to search for the weather in a given location. It consists of an HTML file and a JavaScript file. Check it out [here](https://andrewtrieu.github.io/weather-deploy/). + +## Instructions + +To use the app, simply enter a location (city, state, and country) in the form and click the "Get Weather" button. The app will display the details of the current weather of that location. If no location is provided, the app will display the weather for the user's current location as determined by their IP address. + +However, due to the nature of GitHub Pages, the deployment will not work correctly. Please test it locally as the link above is for reference purpose only. Actual usage is demonstrated in the following video: + +[New video.webm](https://user-images.githubusercontent.com/68151686/210859583-0eaa11bd-1ba7-4f54-9de2-866a99fc65d8.webm) + +## API + +This app uses the OpenWeatherMap API to retrieve weather data and the Rest Countries API to retrieve country names. It also uses the ipapi API to determine the user's current location. + +## Notes + +This app was built using Bootstrap for styling and layout. It also utilizes fetch() and async/await to handle API calls. diff --git a/app.js b/app.js new file mode 100644 index 0000000..e7a5f78 --- /dev/null +++ b/app.js @@ -0,0 +1,130 @@ +const apiKey = "YOUR_API_KEY"; +const weatherForm = document.querySelector("#weather-form"); +const weatherInfo = document.querySelector("#weather-info"); + +fetchCurrent = async () => { + const current = {}; + await fetch("https://ipapi.co/json/") + .then((response) => response.json()) + .then((data) => { + current["city"] = data.city; + current["state"] = data.region; + current["country"] = data.country_name; + }) + .catch((error) => { + console.log(error); + weatherInfo.innerHTML = "

An error occurred

"; + }); + return current; +}; + +fetchCountry = async (country_code) => { + let country; + await fetch(`https://restcountries.com/v3.1/alpha/${country_code}`) + .then((response) => response.json()) + .then((data) => { + country = data[0]; + }) + .catch((error) => { + console.log(error); + weatherInfo.innerHTML = "

An error occurred

"; + }); + return country.name.common; +}; + +fetchLocation = async (city, region, country_name, apiKey) => { + let location; + await fetch( + `http://api.openweathermap.org/geo/1.0/direct?q=${city},${region},${country_name}&appid=${apiKey}` + ) + .then((response) => response.json()) + .then((data) => { + location = data[0]; + }) + .catch((error) => { + console.log(error); + weatherInfo.innerHTML = "

An error occurred

"; + }); + if (!location) { + location = { lat: 0, lon: 0 }; + } else { + location["country"] = await fetchCountry(location.country); + } + return location; +}; + +fetchWeather = async (city, region, country_name, apiKey) => { + const location = await fetchLocation(city, region, country_name, apiKey); + let weather; + await fetch( + `https://api.openweathermap.org/data/2.5/weather?lat=${location.lat}&lon=${location.lon}&units=metric&&appid=${apiKey}` + ) + .then((response) => response.json()) + .then((data) => { + weather = data; + }) + .catch((error) => { + console.log(error); + weatherInfo.innerHTML = "

An error occurred

"; + }); + if (location.state) + weather[ + "destination" + ] = `${location.name}, ${location.state}, ${location.country}`; + else weather["destination"] = `${location.name}, ${location.country}`; + return weather; +}; + +weatherForm.addEventListener("submit", async (event) => { + event.preventDefault(); + + let city = document.querySelector("#city").value; + let state = document.querySelector("#state").value; + let country = document.querySelector("#country").value; + + if (city === "") { + const current = await fetchCurrent(); + city = current.city; + state = current.state; + country = current.country; + } + const weather = await fetchWeather(city, state, country, apiKey); + + if (weather.coord.lat === 0 && weather.coord.lon === 0) { + weatherInfo.innerHTML = ` +
+

Location not found!

+
+
+
+
+ Not found +
+
+
+ `; + } else { + weatherInfo.innerHTML = ` +
+

Weather in ${weather.destination}

+
+
+
+
+ Weather icon +
+
+

Weather: ${weather.weather[0].main} (${weather.weather[0].description})

+

Temperature: ${weather.main.temp}°C

+

Feels like: ${weather.main.feels_like}°C

+

Pressure: ${weather.main.pressure}hPa

+

Humidity: ${weather.main.humidity}%

+

Visibility: ${weather.visibility}m

+

Wind speed: ${weather.wind.speed}m/s

+

Cloudiness: ${weather.clouds.all}%

+
+
+
+ `; + } +}); diff --git a/index.html b/index.html new file mode 100644 index 0000000..818f368 --- /dev/null +++ b/index.html @@ -0,0 +1,38 @@ + + + + + Weather App + + + + + + + + + + +
+
+ + + + + + + +
+
+
+ + + + \ No newline at end of file