-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
34 lines (27 loc) · 1.25 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import * as ELEMENTS from 'elements.js';
import {Http} from 'http.js';
import {WeatherData, WEATHER_PROXY_HANDLER} from 'weather-data.js';
const APP_ID = '41cd3b12f9c75f5770acfd60c26b1559';
ELEMENTS.ELEMENT_SEARCH_BUTTON.addEventListener('click', searchWeather);
function searchWeather() {
const CITY_NAME = ELEMENTS.ELEMENT_SEARCHED_CITY.value.trim();
if (CITY_NAME.length === 0) {
return alert('Please enter a city name');
}
const URL = 'http://api.openweathermap.org/data/2.5/weather?q=' + CITY_NAME + '&units=metric&appid=' + APP_ID;
Http.fetchData(URL)
.then(responseData => {
const WEATHER_DATA = new WeatherData(CITY_NAME, responseData.weather[0].description.toUpperCase());
const WEATHER_PROXY = new Proxy(WEATHER_DATA, WEATHER_PROXY_HANDLER);
WEATHER_PROXY.temperature = responseData.main.temp;
updateWeather(WEATHER_PROXY);
})
.catch(error => alert(error));
}
function updateWeather(weatherData) {
console.log(weatherData);
ELEMENTS.ELEMENT_WEATHER_CITY.textContent = weatherData.cityName;
ELEMENTS.ELEMENT_WEATHER_DESCRIPTION.textContent = weatherData.description;
ELEMENTS.ELEMENT_WEATHER_TEMPERATURE.textContent = weatherData.temperature;
ELEMENTS.ELEMENT_WEATHER_BOX.style.display = 'block';
}