-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
37 lines (29 loc) · 1.45 KB
/
app.js
File metadata and controls
37 lines (29 loc) · 1.45 KB
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
35
36
37
import {Http} from "./http.js";
import {WeatherData, WEATHER_PROXY_HANDLER} from "./weather-data.js";
import * as ELEMENTS from './elements.js';
const APP_ID = 'c9204738afe16537dde4cccc5db59ce0';
ELEMENTS.ELEMENT_SEARCH_BUTTON.addEventListener('click', searchWeather);
function searchWeather() {
const CITY_NAME = ELEMENTS.ELEMENT_SEARCH_CITY.value.trim();
if (CITY_NAME.length === 0) {
return alert("You did not enter a city name");
}
ELEMENTS.ELEMENT_LOADING_TEXT.style.display = 'block';
ELEMENTS.ELEMENT_WEATHER_BOX.style.display = 'none';
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) {
ELEMENTS.ELEMENT_WEATHER_CITY.textContent = weatherData.cityName;
ELEMENTS.ELEMENT_WEATHER_DESCRIPTION.textContent = weatherData.description;
ELEMENTS.ELEMENT_WEATHER_TEMPERATURE.textContent = weatherData.temperature;
ELEMENTS.ELEMENT_LOADING_TEXT.style.display = 'none';
ELEMENTS.ELEMENT_WEATHER_BOX.style.display = 'block';
}