-
Notifications
You must be signed in to change notification settings - Fork 0
/
java.js
148 lines (137 loc) · 4.46 KB
/
java.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
142
143
144
145
146
147
148
let h3 = document.querySelector("h3");
let now = new Date();
let days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
let months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
let currentYear = now.getFullYear();
let currentDay = days[now.getDay()];
let currentMonth = months[now.getMonth()];
let currentDate = now.getDate();
let hours = now.getHours();
if (hours < 10) {
hours = `0${hours}`;
}
let minutes = now.getMinutes();
if (minutes < 10) {
minutes = `0${minutes}`;
}
h3.innerHTML = `Last updated:${currentDay}, ${currentMonth} ${currentDate}, ${currentYear}, ${hours}:${minutes}`;
function formatDay(timestamp) {
let date = new Date(timestamp * 1000);
let day = date.getDay();
let days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
return days[day];
}
function displayForecast(response) {
let forecast = response.data.daily;
let forecastElement = document.querySelector("#dailyforecast");
let forecastHTML = `<div class="row">`;
forecast.forEach(function (forecastDay, index) {
if (index < 6) {
forecastHTML =
forecastHTML +
`
<div class="col-2">
<div class="weather-forecast-date">${formatDay(forecastDay.dt)}</div>
<img
src="http://openweathermap.org/img/wn/${
forecastDay.weather[0].icon
}@2x.png"
alt=""
width="42"
/>
<div class="weather-forecast-temperatures">
<strong> <span class="weather-forecast-temperature-max"> ${Math.round(
forecastDay.temp.max
)}° </span> </strong>
<span class="weather-forecast-temperature-min"> ${Math.round(
forecastDay.temp.min
)}° </span>
</div>
</div>
`;
}
});
forecastHTML = forecastHTML + `</div>`;
forecastElement.innerHTML = forecastHTML;
}
function getForecast(coordinates) {
console.log(coordinates);
let apiKey = "be3ab8e09c2856ab67e7aa09558d9610";
let apiUrl = `https://api.openweathermap.org/data/2.5/onecall?lat=${coordinates.lat}&lon=${coordinates.lon}&appid=${apiKey}&units=metric`;
axios.get(apiUrl).then(displayForecast);
}
//
function search(city) {
let apiKey = "be3ab8e09c2856ab67e7aa09558d9610";
let apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
axios.get(apiUrl).then(showTemperature);
}
function handleSubmit(event) {
event.preventDefault();
let cityInputElement = document.querySelector("#cityinput");
search(cityInputElement.value);
}
let form = document.querySelector("#change-city");
form.addEventListener("submit", handleSubmit);
function showTemperature(response) {
let temperature = document.querySelector("#temperature");
let description = document.querySelector("#description");
let humidity = document.querySelector("#humidity");
let wind = document.querySelector("#wind");
let icon = document.querySelector("#icon");
let h1 = document.querySelector("h1");
celsiusTemperature = response.data.main.temp;
h1.innerHTML = response.data.name;
temperature.innerHTML = Math.round(celsiusTemperature);
description.innerHTML = response.data.weather[0].description;
humidity.innerHTML = response.data.main.humidity;
wind.innerHTML = Math.round(response.data.wind.speed);
icon.setAttribute(
"src",
`http://openweathermap.org/img/wn/${response.data.weather[0].icon}@2x.png`
);
icon.setAttribute("alt", response.data.weather[0].description);
getForecast(response.data.coord);
}
function displayFahrenheitTemperature(event) {
event.preventDefault();
let temperature = document.querySelector("#temperature");
celsius.classList.remove("active");
fahrenheit.classList.add("active");
let fahrenheitTemperature = (celsiusTemperature * 9) / 5 + 32;
temperature.innerHTML = Math.round(fahrenheitTemperature);
}
function displayCelsiusTemperature(event) {
event.preventDefault();
celsiusLink.classList.add("active");
fahrenheitLink.classList.remove("active");
let temperature = document.querySelector("#temperature");
temperature.innerHTML = Math.round(celsiusTemperature);
}
let celsiusTemperature = null;
let fahrenheitLink = document.querySelector("#fahrenheit");
fahrenheitLink.addEventListener("click", displayFahrenheitTemperature);
let celsiusLink = document.querySelector("#celsius");
celsiusLink.addEventListener("click", displayCelsiusTemperature);
search("Lviv");