forked from aditya-ai00/weather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
173 lines (140 loc) · 4.65 KB
/
Copy pathscript.js
File metadata and controls
173 lines (140 loc) · 4.65 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const apiKey = "f0133e94263d448c963164120261904";
let currentUnit = "C"; // default
let currentData = null; // store latest weather
// 🔹 GET WEATHER FUNCTION
function getWeather() {
const city = document.getElementById("city").value.trim();
// fix: added default country handling
if (!city) {
alert("Please enter a city name");
return;
}
let query;
// ✅ Fix: default country handling
if (city.includes(",")) {
query = city;
} else {
query = city + ",IN";
}
const url = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${encodeURIComponent(query)}`;
const forecastUrl = `https://api.weatherapi.com/v1/forecast.json?key=${apiKey}&q=${encodeURIComponent(query)}&days=5`;
fetch(url)
.then(res => res.json())
.then(data => {
if (data.error) {
alert(data.error.message);
return;
}
updateWeather(data);
})
.catch(() => {
alert("Failed to fetch weather data");
});
fetch(forecastUrl)
.then(res => res.json())
.then(data => {
if (!data.error) {
updateForecast(data);
}
});
}
// 🔹 UPDATE UI FUNCTION
function updateWeather(data) {
currentData = data;
document.getElementById("name").innerText =
data.location.name + ", " + data.location.country;
let temp;
if (currentUnit === "C") {
temp = Math.round(data.current.temp_c) + " °C";
} else {
temp = Math.round(data.current.temp_f) + " °F";
}
document.getElementById("temp").innerText = temp;
document.getElementById("condition").innerText =
data.current.condition.text;
document.getElementById("icon").src =
"https:" + data.current.condition.icon;
}
// 🔹 FORECAST FUNCTION
function updateForecast(data) {
const forecastCards = document.getElementById("forecast-cards");
const forecastTitle = document.getElementById("forecast-title");
forecastTitle.innerText = "3-Day Forecast";
forecastCards.innerHTML = "";
data.forecast.forecastday.forEach(day => {
let high, low;
if (currentUnit === "C") {
high = Math.round(day.day.maxtemp_c) + "°C";
low = Math.round(day.day.mintemp_c) + "°C";
} else {
high = Math.round(day.day.maxtemp_f) + "°F";
low = Math.round(day.day.mintemp_f) + "°F";
}
const date = new Date(day.date);
const dayName = date.toLocaleDateString("en-US", { weekday: "short", month: "short", day: "numeric" });
const card = document.createElement("div");
card.classList.add("forecast-card");
card.innerHTML = `
<p class="forecast-day">${dayName}</p>
<img src="https:${day.day.condition.icon}" alt="${day.day.condition.text}" />
<p class="forecast-condition">${day.day.condition.text}</p>
<p class="forecast-temp">⬆ ${high} ⬇ ${low}</p>
`;
forecastCards.appendChild(card);
});
}
// 🔹 ENTER KEY SEARCH
document.getElementById("city").addEventListener("keypress", function (e) {
if (e.key === "Enter") {
getWeather();
}
});
// 🔹 GEOLOCATION
navigator.geolocation.getCurrentPosition(showPosition);
function showPosition(position) {
const lat = position.coords.latitude;
const lon = position.coords.longitude;
const url = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${lat},${lon}`;
const forecastUrl = `https://api.weatherapi.com/v1/forecast.json?key=${apiKey}&q=${lat},${lon}&days=5`;
fetch(url)
.then(res => res.json())
.then(data => {
updateWeather(data);
});
}
// 🔹 UNIT TOGGLE
document.getElementById("unit-toggle").addEventListener("click", function () {
if (!currentData) return;
if (currentUnit === "C") {
currentUnit = "F";
this.innerText = "Switch to °C";
} else {
currentUnit = "C";
this.innerText = "Switch to °F";
}
updateWeather(currentData);
const city = document.getElementById("city").value.trim() ||
`${currentData.location.lat},${currentData.location.lon}`;
const forecastUrl = `https://api.weatherapi.com/v1/forecast.json?key=${apiKey}&q=${encodeURIComponent(city)}&days=5`;
fetch(forecastUrl)
.then(res => res.json())
.then(data => {
if (!data.error) updateForecast(data);
});
});
// 🌙 DARK MODE
const themeBtn = document.getElementById("theme-toggle");
if (localStorage.getItem("theme") === "dark") {
document.body.classList.add("dark-mode");
themeBtn.innerText = "☀️ Light Mode";
}
themeBtn.addEventListener("click", () => {
document.body.classList.toggle("dark-mode");
if (document.body.classList.contains("dark-mode")) {
themeBtn.innerText = "☀️ Light Mode";
localStorage.setItem("theme", "dark");
} else {
themeBtn.innerText = "🌙 Dark Mode";
localStorage.setItem("theme", "light");
}
});