Skip to content

Commit e1bd500

Browse files
committed
add background image
1 parent 2303095 commit e1bd500

File tree

1 file changed

+80
-22
lines changed

1 file changed

+80
-22
lines changed

Source-Code/WeatherApp/script.js

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ const btn = document.getElementById("btn");
33
const apiKey = "e3a46268fdc2475cb63214712240202";
44
const cityName = document.getElementById("city-name");
55
const dateTime = document.getElementById("date-time");
6-
const condition = document.getElementById("condition");
76
const condition2 = document.getElementById("condition2");
87
const temp = document.getElementById("temp");
98
const humidity = document.getElementById("humidity");
109
const country = document.getElementById("country");
1110
const locat = document.getElementById("getlocation");
1211
const cities = document.getElementsByClassName("city");
1312
const icon = document.getElementById("icon");
14-
13+
const body = document.querySelector(".weather-app");
1514
const fetchData = async (url) => {
1615
try {
1716
const data = await fetch(url);
@@ -24,21 +23,69 @@ const fetchData = async (url) => {
2423
throw error;
2524
}
2625
};
26+
2727
const updateWeatherInfo = (result) => {
28-
cityName.innerText = `${result.location.name}`;
29-
country.innerText = `${result.location.country}`;
30-
dateTime.innerText = `${result.location.localtime}`;
31-
temp.innerText = `${result.current.temp_c} °C`;
32-
humidity.innerText = `${result.current.humidity} %`;
33-
condition.innerText = `${result.current.condition.text}`;
34-
condition2.innerText = `${result.current.condition.text}`;
35-
icon.src = `${result.current.condition.icon}`; // Set the src attribute of the img tag with id "icon"
36-
};
37-
const getData = async (cityName) =>
38-
fetchData(
39-
`http://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${cityName}&aqi=no`
40-
);
28+
const { error, location, current } = result;
29+
if (error) {
30+
cityName.innerText = "Error: " + error.message;
31+
[country, dateTime, temp, humidity, condition2, icon].forEach(
32+
(elem) => (elem.innerText = "")
33+
);
34+
icon.src = "";
35+
} else {
36+
const { name, country, localtime } = location;
37+
cityName.innerText = name;
38+
country.innerText = country;
39+
dateTime.innerText = localtime;
40+
temp.innerText = `${current.temp_c} °C`;
41+
humidity.innerText = `${current.humidity} %`;
42+
condition2.innerText = current.condition.text;
43+
icon.src = current.condition.icon;
4144

45+
const isDay = current.is_day == 1 ? "day" : "night";
46+
const codes = [
47+
[1000, 10000, 10001, 1100, 11001, 11000, 51190, 60030], // clear
48+
[
49+
1101, 11011, 11010, 11021, 11020, 1102, 1001, 10010, 10011, 1003, 1150,
50+
1153, 1168, 1147, 1135, 1087, 1003, 1006, 1207, 1009,
51+
], // cloudy
52+
[
53+
1261, 1273, 1276, 1274, 1246, 1243, 1240, 1201, 1204, 1198, 1192, 1195,
54+
1189, 1186, 1183, 1180, 1186,
55+
], // rainy
56+
[
57+
1030, 1066, 1168, 1171, 1210, 1213, 1216, 1219, 1222, 1225, 1237, 1255,
58+
1258, 1279, 1282,
59+
], // snowy
60+
];
61+
const imageUrls = [
62+
`./images/${isDay}/clear.jpg`,
63+
`./images/${isDay}/cloudy.jpg`,
64+
`./images/${isDay}/rainy.jpg`,
65+
`./images/${isDay}/snowy.jpg`,
66+
];
67+
68+
for (let i = 0; i < codes.length; i++) {
69+
if (codes[i].includes(current.condition.code)) {
70+
body.style.backgroundImage = `url('${imageUrls[i]}')`;
71+
console.log(imageUrls[i]);
72+
break;
73+
}
74+
}
75+
}
76+
};
77+
const getData = async (cityName) => {
78+
try {
79+
const result = await fetchData(
80+
`http://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${cityName}&aqi=no`
81+
);
82+
return result;
83+
} catch (error) {
84+
return {
85+
error: { message: "Failed to fetch data. Please try again later." },
86+
};
87+
}
88+
};
4289
const getlocation = async (lat, long) =>
4390
fetchData(
4491
`http://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${lat},${long}&aqi=no`
@@ -50,22 +97,29 @@ const gotlocation = async (position) => {
5097
position.coords.latitude,
5198
position.coords.longitude
5299
);
100+
console.log(result);
53101
updateWeatherInfo(result);
54102
} catch (error) {
55103
cityName.innerText = "Error fetching weather based on location";
56104
}
57105
};
58106
const failedlocation = () => console.log("failed to locate location");
59107

60-
btn.addEventListener("click", async () => {
108+
btn.addEventListener("click", async (e) => {
61109
try {
62-
const { value } = input;
63-
const result = await getData(value);
64-
updateWeatherInfo(result);
65-
console.log(result);
110+
if (input.value.length === 0) {
111+
alert("Please type a city name");
112+
} else {
113+
const { value } = input;
114+
const result = await getData(value);
115+
console.log(result);
116+
updateWeatherInfo(result);
117+
console.log(result);
118+
}
66119
} catch (error) {
67120
cityName.innerText = "Error to fetch weather";
68121
}
122+
e.preventDefault();
69123
});
70124

71125
locat.addEventListener("click", () =>
@@ -74,8 +128,12 @@ locat.addEventListener("click", () =>
74128
const citiesArray = [...cities];
75129
citiesArray.forEach((element) => {
76130
element.addEventListener("click", async () => {
77-
const cityName = element.innerText; // Extract city name from the clicked element
78-
const result = await getData(cityName); // Pass the city name to getData
131+
const cityName = element.innerText;
132+
const result = await getData(cityName);
79133
updateWeatherInfo(result);
80134
});
81135
});
136+
137+
window.addEventListener("load", async () => {
138+
navigator.geolocation.getCurrentPosition(gotlocation, failedlocation);
139+
});

0 commit comments

Comments
 (0)