-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
144 lines (104 loc) · 4.14 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
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
const apiKey = 'REPLACE_WITH_API_KEY';
const apiUrl = "https://api.openweathermap.org/data/2.5/weather?units=metric";
const searchBox = document.querySelector(".search input");
const searchBtn = document.querySelector(".search button");
const weatherIcon = document.querySelector(".weather-icon");
const selectBtn = document.querySelector(".select-btn");
const cityList = document.getElementById('cityList');
const searchInput = document.getElementById('searchInput');
// const dropDownList = document.querySelector(".contemt");
let cities = [];
fetch('current.city.list.json')
.then(response => response.json())
.then(data => {
// Store all city names in the array
const citySet = new Set(data.map(city => city.name));
cities = [...citySet]; // Convert Set back to array
// console.log(cities);
})
.catch(error => {
console.error('Error loading cities:', error);
reject(error); // Reject the promise if there's an error
});
document.addEventListener('DOMContentLoaded', function() {
// Event listener for input changes (keyup event)
searchInput.addEventListener('keyup', function() {
const searchTerm = searchInput.value.toLowerCase();
const filteredCities = cities.filter(city => city.toLowerCase().includes(searchTerm));
// Clear previous list items
cityList.innerHTML = '';
// Create and append new list items for filtered cities
filteredCities.forEach(city => {
const li = document.createElement('li');
li.textContent = city;
li.setAttribute('onclick', 'selectItem(this)');
cityList.appendChild(li);
});
});
});
function loadCities(cities){
cities.forEach(city => {
const cityName = city;
// console.log(cityName);
const li = document.createElement('li');
li.textContent = cityName;
li.setAttribute('onclick', 'selectItem(this)');
cityList.appendChild(li);
});
}
async function checkWeather(city){
// make a fetch and wait for the response
const response = await fetch(apiUrl + `&q=` + city + `&appid=${apiKey}` );
// wait for the response to get parsed as json
var data = await response.json();
// updating data
document.querySelector(".city").innerHTML = city;
document.querySelector(".temp").innerHTML = Math.round(data.main.temp) + "°C";
document.querySelector(".humidity").innerHTML = data.main.humidity + "%";
document.querySelector(".wind").innerHTML = data.wind.speed + " Km/h";
// update weather condns....
if(data.weather[0].main == "Clouds"){
weatherIcon.src = "images/clouds.png"
}
else if(data.weather[0].main == "Clear"){
weatherIcon.src = "images/clear.png"
}
else if(data.weather[0].main == "Rain"){
weatherIcon.src = "images/rain.png"
}
else if(data.weather[0].main == "Drizzle"){
weatherIcon.src = "images/drizzle.png"
}
else if(data.weather[0].main == "Mist"){
weatherIcon.src = "images/mist.png"
}
else if(data.weather[0].main == "Snow"){
weatherIcon.src = "images/snow.png"
}
//
document.querySelector(".weather").style.display = "block";
console.log(data);
}
const content = document.querySelector(".content");
function selectItem(item) {
// Update selected item display
// select-btn
// document.getElementsByClassName("select-btn").textContent = item.textContent;
document.getElementById('selectedText').textContent = item.textContent;
// Hide dropdown
content.style.display = "none";
checkWeather(item.textContent);
}
// searchBtn.addEventListener("click",()=>{
// checkWeather(searchBox.value);
// })
selectBtn.addEventListener("click", () => {
if (content.style.display === "none" || content.style.display === "") {
content.style.display = "block";
document.getElementById('searchInput').value = '';
loadCities(cities);
} else {
content.style.display = "none";
}
});
// checkWeather("Jodhpur");