diff --git a/app.js b/app.js
index b331d64..f722c1b 100644
--- a/app.js
+++ b/app.js
@@ -1,48 +1,38 @@
-const appid = '';
-let weatherData;
+const appid = 'eb98981901058b162b2802e1ddd392b9';
+//You gonna need this for caching.
+// let globalWeatherData;
-$.getJSON("./il.cities.json", function (data) {
- let items = [];
- $.each(data, function (key, val) {
- items.push("");
- });
-
- $("", {
- "class": "my-city-list",
- html: items.join("")
- }).appendTo("#forecast");
-});
+getInitData();
$('#forecast').on('change', () => {
- let selectedCity = $('.my-city-list option').filter(':selected').text();
- let url = 'https://api.openweathermap.org/data/2.5/weather?'
- let data = `q=${selectedCity}&appid=${appid}&units=metric`
- console.log(selectedCity);
- $.getJSON(url, data,
- function (data, textStatus, jqXHR) {
- if (textStatus == 'success') {
- weatherData = data;
- $('[data-city]').html(weatherData.name+", "+weatherData.sys.country);
- $('[data-feel]').html('feels like\n '+weatherData.main.feels_like + "°c");
- $('[data-humidity]').html('Humidity\n '+weatherData.main.humidity + '%');
- $('[data-temp]').html('Temp\n '+weatherData.main.temp + "°c");
- $('[data-maxTemp]').html('max\n '+weatherData.main.temp_max + "°c");
- $('[data-minTemp]').html('min\n '+weatherData.main.temp_min + "°c");
- $('[data-weather]').html('Info: '+weatherData.weather[0].description);
- let deg = weatherData.wind.deg;
- let degDir = setDirection(deg);
- $('[data-wind]').html(`Wind: ${degDir}(${deg}°)
at speed:
- ${weatherData.wind.speed} [Km/h]`);
- }
- else{
- alert('city not found or not hava an info!');
- }
- }
- );
+ $.getJSON('https://api.openweathermap.org/data/2.5/weather?', `q=${$('.my-city-list option').filter(':selected').text()}&appid=${appid}&units=metric`, function (mWeatherData, textStatus) {
+ (textStatus !== 'success') ? alert('Can\'t get data') : populateWeatherData(mWeatherData)
+ });
})
-function setDirection(deg){
- const dirCode = ["E","ENE","NNE","N","N","NNW","WNW","W","W","WWS","WSS","S","S","SSE","ESE","E"];
- let i = Math.floor(deg/22.5);
- return dirCode[i]
+function populateWeatherData(weatherData) {
+ // globalWeatherData = weatherData
+ $('[data-city]').text(weatherData.name + ", " + weatherData.sys.country);
+ $('[data-feel]').text('feels like\n ' + weatherData.main.feels_like + "°c");
+ $('[data-humidity]').text('Humidity\n ' + weatherData.main.humidity + '%');
+ $('[data-temp]').text('Temp\n ' + weatherData.main.temp + "°c");
+ $('[data-maxTemp]').text('max\n ' + weatherData.main.temp_max + "°c");
+ $('[data-minTemp]').text('min\n ' + weatherData.main.temp_min + "°c");
+ $('[data-weather]').text('Info: ' + weatherData.weather[0].description);
+ $('[data-wind]').html(`Wind: ${setDirection(weatherData.wind.deg)}(${weatherData.wind.deg}&weatherData.wind.deg)
at speed:
+ ${weatherData.wind.speed} [Km/h]`);
+}
+
+function setDirection(deg) {
+ const dirCode = ["E", "ENE", "NNE", "N", "N", "NNW", "WNW", "W", "W", "WWS", "WSS", "S", "S", "SSE", "ESE", "E"];
+ return dirCode[Math.floor(deg / 22.5)]
+}
+
+function getInitData() {
+ $.getJSON("./il.cities.json", function (data) {
+ $("#forecast").append($("", {
+ "class": "my-city-list",
+ html: data.map((item) => "").join("")
+ }));
+ });
}