-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
115 lines (100 loc) · 3.56 KB
/
app.js
File metadata and controls
115 lines (100 loc) · 3.56 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
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
// JavaScript to handle the dropdown menu
const searchSection = document.querySelector('.search-section');
const cityDropdown = document.getElementById('cityDropdown');
cityDropdown.addEventListener('click', function() {
if (searchSection.classList.contains('open')) {
searchSection.classList.remove('open');
} else {
searchSection.classList.add('open');
}
});
// JavaScript to update the text input when a city is selected
document.getElementById("cityDropdown").addEventListener("change", function() {
const selectedCity = this.value;
document.getElementById("searchInput").value = selectedCity;
});
// JavaScript to handle the search button click
document.getElementById("searchButton").addEventListener("click", function() {
const selectedCity = document.getElementById("cityDropdown").value;
const searchText = document.getElementById("searchInput").value;
console.log("Selected City: " + selectedCity);
console.log("Search Text: " + searchText);
});
// JavaScript to handle the Weather searches
const apiKey = "28a7ee5707154ddb848fc79da1f6499b";
const main = document.getElementById('main');
fetch('https://extreme-ip-lookup.com/json/')
.then( res => res.json())
.then(response => {
console.log("Country: ", response.country);
})
.catch((data, status) => {
console.log('Request failed');
})
const form = document.getElementById('form');
const search = document.getElementById('searchInput');
const url = (city)=> `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`;
async function getWeatherByLocation(city) {
try
{
const resp = await fetch(url(city), {
origin: "cros"
});
if (resp.status === 200)
{
const respData = await resp.json();
addWeatherToPage(respData);
}
else
{
displayError("Weather data not available for this location. You might have done a spelling mistake.");
}
}
catch (error)
{
displayError("An error occurred while fetching weather data.");
}
}
function displayError(errorMessage) {
const errorElement = document.createElement('div');
errorElement.classList.add('error');
errorElement.textContent = errorMessage;
main.innerHTML = '';
main.appendChild(errorElement);
}
function addWeatherToPage(data)
{
const temp = Ktoc(data.main.temp);
const weather = document.createElement('div')
weather.classList.add('weather');
weather.innerHTML = `
<h2><img src="https://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png" /> ${temp}°C <img src="https://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png" /></h2>
<small>${data.weather[0].main}</small> `;
//cleanup
main.innerHTML= "";
main.appendChild(weather);
};
function Ktoc(K)
{
return Math.floor(K - 273.15);
}
form.addEventListener('submit',(e) =>{
e.preventDefault();
const city = search.value;
if(city)
{
getWeatherByLocation(city)
}
});
document.getElementById("searchButton").addEventListener("click", function() {
const selectedCity = document.getElementById("cityDropdown").value;
const searchText = document.getElementById("searchInput").value;
if (searchText) {
// Check if there's text in the input field
getWeatherByLocation(searchText);
}
else {
// Handle no city or text entered
console.log("Please enter a city or location.");
}
});