-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
63 lines (58 loc) · 2.09 KB
/
script.js
File metadata and controls
63 lines (58 loc) · 2.09 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
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
const searchBar = document.querySelector(".search-bar");
const stateName = document.querySelector(".State");
const dateElement = document.querySelector(".date");
const cond = document.querySelector(".cond");
const temp = document.querySelector(".temp");
const iconw = document.querySelector(".iconw");
const sunset = document.querySelector(".sunset");
const sunrise = document.querySelector(".sunrise");
fetch("https://ipinfo.io/json")
.then((r) => r.json())
.then((data) => fetchAndUpdate(data.city));
const API_KEY = "1561b2c4b7ada334819f833f3abb18eb";
searchBar.addEventListener("keydown", (e) => {
if (e.key === "Enter") fetchAndUpdate(searchBar.value);
});
document
.querySelector(".search-icon")
.addEventListener("click", () => fetchAndUpdate(searchBar.value));
async function fetchAndUpdate(state) {
searchBar.value = "";
console.log(state);
await fetch(
"https://api.openweathermap.org/data/2.5/weather?q=" +
state +
"&appid=" +
API_KEY
)
.then((response) => {
if (response.status === 200) return response.json();
else alert("Enter valid state ");
})
.then((data) => {
console.log(data);
console.log(data.main.feels_like - 273);
temp.textContent = Math.floor(data.main.temp - 273) + " °C";
cond.textContent = data.weather[0].description;
stateName.textContent = data.name;
var tt = new Date();
// console.log(tt.slice(0, 21));
// dateElement.textContent = tt.slice(0, 21);
dateElement.textContent = "As of " + tt.toLocaleTimeString();
updateIconAndBkg(data.weather[0].id, data.weather[0].icon);
sunrise.textContent = new Date(
1000 * data.sys.sunrise
).toLocaleTimeString();
sunset.textContent = new Date(
1000 * data.sys.sunset
).toLocaleTimeString();
temp.click();
temp.focus();
});
}
function updateIconAndBkg(id, iconid) {
console.log(id, iconid);
if (iconid === "50d" || iconid === "50n")
iconw.setAttribute("src", "images/weather-icons/" + id + ".png");
else iconw.setAttribute("src", "images/weather-icons/" + iconid + ".png");
}