-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
94 lines (80 loc) · 3.15 KB
/
script.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
const container = document.querySelector(".container"),
subContainer = document.querySelector(".subContainer"),
inputField = document.querySelector("input"),
submit = document.querySelector("button"),
weatherInfo = document.querySelector(".weatherInfo"),
displayInfo = document.querySelector(".displayInfo"),
addError = document.querySelector(".addError"),
arrowBack = container.querySelector("h1 i"),
apikey = `b49ee8f728d6a66f93756323af0bb5e2`,
colorBtn = document.querySelectorAll(".colorBtn"),
colorPalette = document.querySelector(".color-palette"),
wIcon = weatherInfo.querySelector("img");
submit.addEventListener("click", () => {
if (inputField.value != "") {
getWeather(inputField.value);
} else {
addError.classList.remove("addError");
addError.innerText = "Please enter city name";
}
});
const getWeather = (city) => {
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apikey}&units=metric`).then(response => response.json()).then(response => {
if (response.cod != "404") {
colorPalette.classList.add("display");
displayInfo.style.display = "block";
subContainer.style.display = "none";
arrowBack.style.display = "inline";
console.log(response);
temp = response.main.temp
feels_like = response.main.feels_like;
humidity = response.main.humidity
names = response.name
country = response.sys.country
description = response.weather[0].description;
id = response.weather[0].id;
if (id == 800) {
wIcon.src = "icons/clear.svg";
} else if (id >= 200 && id <= 232) {
wIcon.src = "icons/storm.svg";
} else if (id >= 600 && id <= 622) {
wIcon.src = "icons/snow.svg";
} else if (id >= 701 && id <= 781) {
wIcon.src = "icons/haze.svg";
} else if (id >= 801 && id <= 804) {
wIcon.src = "icons/cloud.svg";
} else if ((id >= 500 && id <= 531) || (id >= 300 && id <= 321)) {
wIcon.src = "icons/rain.svg";
}
detail();
} else {
addError.classList.remove("addError");
addError.innerText = "City name is invalid";
}
})
.catch(err => console.error(err));
// -------------------------------------------------------------
}
const detail = () => {
weatherInfo.querySelector(".temp .num").innerText = Math.floor(temp);
weatherInfo.querySelector(".humidity span").innerText = `${humidity}%`;
weatherInfo.querySelector(".details .num2").innerText = Math.floor(feels_like);
weatherInfo.querySelector(".weather").innerText = `${description}`;
weatherInfo.querySelector(".area").innerText = `${names}, ${country}`;
}
// --------------------------------------------------
arrowBack.addEventListener("click", () => {
colorPalette.classList.remove("display");
displayInfo.style.display = "none";
subContainer.style.display = "block";
arrowBack.style.display = "none";
addError.classList.add("addError");
inputField.value = "";
});
// ---------------------------------------------------
colorBtn.forEach(colorBtn => {
let color = colorBtn.getAttribute("color");
colorBtn.addEventListener("click", () => {
document.querySelector(":root").style.setProperty("--color", color);
});
});