-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
141 lines (133 loc) · 4.95 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
function handleSubmit(event) {
event.preventDefault();
let cityUser = document.querySelector("#city-input");
cityUser = cityUser.value;
search(cityUser);
}
function search(cityUser) {
let urlApi = `https://api.shecodes.io/weather/v1/current?query=${cityUser}&key=4e3d43265f7f3448fot5bf7a6b40260b&units=metric`;
axios.get(urlApi).then(showTemp);
console.log(urlApi);
}
function showCurrent() {
navigator.geolocation.getCurrentPosition(handlePosition);
}
function handlePosition(position) {
let lat = position.coords.latitude;
let lon = position.coords.longitude;
let urlApi = `https://api.shecodes.io/weather/v1/current?lat=${lat}&lon=${lon}&key=4e3d43265f7f3448fot5bf7a6b40260b&units=metric`;
console.log(urlApi);
axios.get(urlApi).then(showTemp);
}
function showTemp(response) {
let tempRound = Math.round(response.data.temperature.current);
let temperature = document.querySelector("#temperature");
let weather = document.querySelector("#weather");
let city = document.querySelector("#city");
let humidity = document.querySelector("#humidity");
let wind = document.querySelector("#wind");
let country = document.querySelector("#country");
let iconElement = document.querySelector("#icon");
temperature.innerHTML = tempRound;
weather.innerHTML = response.data.condition.description;
city.innerHTML = response.data.city;
humidity.innerHTML = response.data.temperature.humidity;
wind.innerHTML = Math.round(response.data.wind.speed);
country.innerHTML = response.data.country;
celsiusTemperature = tempRound;
iconElement.setAttribute(
"src",
`https://shecodes-assets.s3.amazonaws.com/api/weather/icons/${response.data.condition.icon}.png`
);
iconElement.setAttribute("alt", response.data.condition.description);
if (
response.data.condition.icon === "shower-rain-day" ||
response.data.condition.icon === "shower-rain-night" ||
response.data.condition.icon === "rain-day" ||
response.data.condition.icon === "rain-night" ||
response.data.condition.icon === "thunderstorm-day" ||
response.data.condition.icon === "thunderstorm-night"
) {
document.body.style.backgroundImage = "url('rain drops.webp')";
} else {
if (
response.data.condition.icon === "clear-sky-day" ||
response.data.condition.icon === "clear-sky-night"
) {
document.body.style.backgroundImage = "url('sunny.jpg')";
} else if (
response.data.condition.icon === "snow-day" ||
response.data.condition.icon === "snow-night"
) {
document.body.style.backgroundImage = "url('snow_image.webp')";
} else {
if (
response.data.condition.icon === "few-clouds-day" ||
response.data.condition.icon === "few-clouds-night" ||
response.data.condition.icon === "scattered-clouds-day" ||
response.data.condition.icon === "scattered-clouds-night" ||
response.data.condition.icon === "broken-clouds-day" ||
response.data.condition.icon === "broken-clouds-night"
) {
document.body.style.backgroundImage = "url('clouds.webp')";
}
}
}
console.log(response.data);
}
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()}`;
}
}
function displayFahrenheitTemp(event) {
event.preventDefault();
let temteratureElement = document.querySelector("#temperature");
let fahrenheitTemp = (celsiusTemperature * 9) / 5 + 32;
temteratureElement.innerHTML = Math.round(fahrenheitTemp);
fahrenheitLink.classList.add("active");
celsiusLink.classList.remove("active");
}
function displayCelsiusTemp(event) {
event.preventDefault();
let temteratureElement = document.querySelector("#temperature");
temteratureElement.innerHTML = celsiusTemperature;
fahrenheitLink.classList.remove("active");
celsiusLink.classList.add("active");
}
let form = document.querySelector("#city-form");
form.addEventListener("submit", handleSubmit);
let currentButton = document.querySelector("#current");
currentButton.addEventListener("click", showCurrent);
let celsiusTemperature = null;
let fahrenheitLink = document.querySelector("#fahrenheit-link");
fahrenheitLink.addEventListener("click", displayFahrenheitTemp);
let celsiusLink = document.querySelector("#celsius-link");
celsiusLink.addEventListener("click", displayCelsiusTemp);
formatData();
search("Zaporizhzhia");