diff --git a/README.md b/README.md index 4638c655..fac7b3b1 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,30 @@ This project has been created by a student at Parsity, an online software engineering course. The work in this repository is wholly of the student based on a sample starter project that can be accessed by looking at the repository that this project forks. If you have any questions about this project or the program in general, visit [parsity.io](https://parsity.io/) or email hello@parsity.io. + +# What is it? + +A weather app that fetches weather data using the [open weather api](https://openweathermap.org/). + +## Features + +- Search weather of a city using a text input (current and 5 day forecast) +  +- Search weather of current location using JS's [geolocation api](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition) +- Set a default location that +  +- Responsive components +  + +## Files/Folder overview + +- index.html: main html file +- main.js: houses all classes and js functions +- style.css: houses custom css +- static: houses screenshots of project + +## Technology used + +- Plain old JS +- Bootstrap v5 +- Jquery diff --git a/index.html b/index.html new file mode 100644 index 00000000..64e0855a --- /dev/null +++ b/index.html @@ -0,0 +1,116 @@ + + +
+ + + + + + + +${body}
`); + $("#project-modal").modal("toggle"); +} + +const initSetDefaultBtn = function() { + $("#set-default-btn").on("click", (event) => { + event.preventDefault(); + console.log("setDefaultBtn clicked"); + console.log(weatherComponent.name); + localStorage.setItem("WEATHERPROJECT", JSON.stringify({"name": weatherComponent.name})); + $("#default-city-text").text(`Default city: ${weatherComponent.name}`); + toggleModal("Success!", "New Default location has been set!"); + }); + + return; +}; + +const handleGetCurrentLocation = async function () { + const geolocation = navigator.geolocation + if (!geolocation) return alert("Geolocation is not supported. Sorry!"); + + const onSuccess = async function (pos) { + const {longitude, latitude} = pos.coords + const {name} = await getCurrentWeatherData(longitude, latitude); + handleSubmit(name, {"lon": longitude, "lat": latitude, "name": name}); + } + + const onError = function () { + alert("Oh No! Something went wrong finding your geolocation!"); + } + + geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy:true}); +}; + +const handleSubmit = async function (searchValue, locationData = null) { + const {lon, lat, name} = locationData ? locationData : await getLatLonData(searchValue); + + if (!name) return alert("Error: Location not found"); + + weatherComponent = new WeatherComponent(lon, lat, name); + + await updateWeatherPanel(weatherComponent); + initSetDefaultBtn(); +}; + +/** + * My Class + */ +class WeatherComponent { + constructor(lon, lat, name) { + this.lat = lat; + this.lon = lon; + this.name = name; + this.id = new Date().getTime(); + this.curTemp = null; + this.curWeather = null; + this.iconCode = null; + this.fiveDayData = null; + }; + + async fetchCurData() { + const {temp, weather, iconCode} = await getCurrentWeatherData(this.lon, this.lat); + this.curTemp = temp; + this.curWeather = weather; + this.iconCode = iconCode; + return 1; + }; + + async fetchFiveDayData() { + const data = await getFiveDayWeatherData(this.lon, this.lat); + this.fiveDayData = data; + }; + + getFiveDayWeatherPanelChildren() { + let template = "" + this.fiveDayData.forEach((day) => { + const box = ` +