-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
69 lines (66 loc) · 2.15 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function handleSubmit(event) {
event.preventDefault();
let cityUser = document.querySelector("#city-input");
cityUser = cityUser.value;
search(cityUser);
}
function search(cityUser) {
let apiKey = "2ae2f19a65e443eebd09dd05cfe0af8a";
let urlApi = `https://api.openweathermap.org/data/2.5/weather?q=${cityUser}&appid=${apiKey}&units=metric`;
axios.get(urlApi).then(showTemp);
}
function showCurrent() {
navigator.geolocation.getCurrentPosition(handlePosition);
}
function handlePosition(position) {
let lat = position.coords.latitude;
let lon = position.coords.longitude;
let apiKey = "2ae2f19a65e443eebd09dd05cfe0af8a";
let urlApi = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}&units=metric`;
axios.get(urlApi).then(showTemp);
}
function showTemp(response) {
let tempRound = Math.round(response.data.main.temp);
let temperature = document.querySelector("#temperature");
let weather = document.querySelector("#weather");
let city = document.querySelector("#city");
temperature.innerHTML = `${tempRound}`;
weather.innerHTML = response.data.weather[0].main;
city.innerHTML = response.data.name;
}
function formatData() {
let now = new Date();
now.getMinutes(); // 0,1,2, 12
now.getHours(); //1, 2, 3, 4
now.getDate(); //1, 2, 3, 4
now.getDay(); // 0, 1, 2
now.getMonth(); // 0, 1, 2
now.getFullYear(); // 2021
let days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
let day = document.querySelector("#dayInWeek");
day.innerHTML = days[now.getDay()];
let hour = document.querySelector("#hour");
hour.innerHTML = `${now.getHours()}`;
if (now.getHours() < 10) {
hour.innerHTML = `0${now.getHours()}`;
}
let minute = document.querySelector("#minute");
minute.innerHTML = `${now.getMinutes()}`;
if (now.getMinutes() < 10) {
minute.innerHTML = `0${now.getMinutes()}`;
}
}
formatData();
search("Zaporizhzhia");
let form = document.querySelector("#city-form");
form.addEventListener("submit", handleSubmit);
let currentButton = document.querySelector("#current");
currentButton.addEventListener("click", showCurrent);