-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathscript.js
201 lines (185 loc) · 7.06 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//API URL
const defaultURL = "https://api.openweathermap.org/data/2.5/forecast?lat=59.334591&lon=18.063240&appid=15a1790288c26c9ab80c3b6f2209e071"; //Default Stockholm
const parURL = "https://api.openweathermap.org/data/2.5/forecast?lat=48.85341&lon=2.3488&appid=15a1790288c26c9ab80c3b6f2209e071"; // Paris
const barURL = "https://api.openweathermap.org/data/2.5/forecast?lat=41.38879&lon=2.15899&appid=15a1790288c26c9ab80c3b6f2209e071"; // Barcelona
//DOM Elements
const cityName = document.getElementById("cityName");
const weatherDesc = document.getElementById("weatherDesc");
const sunrise = document.getElementById("sunrise");
const mainTemp = document.getElementById("mainTemp");
const weatherIcon = document.getElementById("weatherIcon");
const searchButton = document.getElementById("inputBtn");
const weatherMain = document.getElementById("weatherMain");
const nextCity = document.getElementById("nextCity");
let fetchedData = [];
//Fetch API data
const fetchData = async (url) => {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Status ${response.status}`);
}
const data = await response.json();
fetchedData = [data]; // Wrap the response in an array
updateCity();
fetchForecastData(url); // Fetch forecast data after fetching main weather data
}
catch (error) {
alert("There was an error, please try again later: " + error);
console.error("Error fetching data:", error);
}
};
//Fetch Forecast data
const fetchForecastData = async (url) => {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTPP errror! Status: ${response.status}`);
}
const data = await response.json();
if (!data.list || data.list.length === 0) {
throw new Error("Forecast data is empty or undefined");
}
//filterr out for clsoest to 12:00
fetchedData.forecast = data.list
.filter(item => {
const date = new Date(item.dt * 1000);
const hours = date.getUTCHours();
return hours === 12;
})
.slice(0, 4) //for 4 days only
.map(item => {
return {
date: new Date(item.dt * 1000).toLocaleDateString('en-US', { month: 'short', day: 'numeric' }),
temp: Math.round(item.main.temp - 273.15), //from Kelevin to Celsius
description: item.weather[0].description,
icon: item.weather[0].icon
};
});
updateForecast();
} catch (error) {
console.error("Errrror fetching forecast data:", error);
alert("There was an error, please try again later: " + error.message);
}
};
const updateForecast = () => {
const forecastContainer = document.getElementById("weekForecast");
forecastContainer.innerHTML = ``;
fetchedData.forecast.forEach((day) => {
const listItem = document.createElement("li");
const iconCode = day.icon;
const iconUrl = `https://openweathermap.org/img/wn/${iconCode}.png`;
listItem.innerHTML = `<span>${day.date}</span> <span>${day.temp}°C</span> <img src="${iconUrl}" alt="${day.description} icon"> <span>${day.description}</span>`;
forecastContainer.appendChild(listItem);
});
};
//Time conversion
const timeConversion = () => {
const timestamps = {
sunrise: fetchedData[0].city.sunrise,
sunset: fetchedData[0].city.sunset,
};
const timezone = fetchedData[0].city.timezone;
Object.entries(timestamps).forEach(([key, value]) => {
const date = new Date((value + timezone) * 1000);
const hours = date.getUTCHours().toString().padStart(2, '0');
const minutes = date.getUTCMinutes().toString().padStart(2, '0');
// Update the respective HTML elements
const element = document.getElementById(key);
if (element) {
element.innerHTML = `${hours}:${minutes}`;
}
else {
console.warn(`Element with id '${key}' not found in the DOM.`);
alert("There was an error, please try again later");
}
});
};
//Update City
const updateCity = () => {
if (fetchedData[0].city && fetchedData[0].city.name) {
cityName.innerHTML = fetchedData[0].city.name;
weatherDesc.innerHTML = fetchedData[0].list[0].weather[0].description;
updateWeather();
timeConversion();
updateMainTemp();
}
else {
console.error("City data is missing in fetchedData", fetchedData);
}
};
//Update Weather
const updateWeather = () => {
document.body.className = '';
if (fetchedData[0].list[0].weather[0].main === "Clear") {
document.body.classList.add("clear");
weatherMain.innerHTML = `Get your sunnies on. ${fetchedData[0].city.name} is looking great today.`;
weatherIcon.innerHTML = `<img
src="./img/clear.svg"
alt="Sunglasses for clear weather"
class="weatherIcon weatherIconClear"
>`;
}
else if (fetchedData[0].list[0].weather[0].main === "Clouds") {
document.body.classList.add("cloud");
weatherMain.innerHTML = `It's sweater weather in ${fetchedData[0].city.name} today. Get cosy!`;
weatherIcon.innerHTML = `<i class="fa-solid fa-cloud weatherIcon weatherIconCloud"></i>`;
} else if (fetchedData[0].list[0].weather[0].main === "Snow") {
document.body.classList.add("snow")
weatherMain.innerHTML = `It's snowing in ${fetchedData[0].city.name} today. Grab your sled!`
weatherIcon.innerHTML = `<i class="fa-solid fa-snowflake weatherIcon weatherIconSnow"></i>`
} else if (fetchedData[0].list[0].weather[0].main === "Rain") {
document.body.classList.add("rain");
weatherMain.innerHTML = `Don't forget your umbrella. It's wet in ${fetchedData[0].city.name} today.`;
weatherIcon.innerHTML = `<img
src="./img/rain.svg"
alt="An umbrella for rainy weather"
class="weatherIcon weatherIconRain"
>`;
} else {
document.body.classList.add("");
}
};
//Update Main Temperature
const updateMainTemp = () => {
if (fetchedData[0].list[0].main.temp) {
const tempInCelsius = fetchedData[0].list[0].main.temp - 273.15;
mainTemp.innerHTML = tempInCelsius.toFixed(0) + "°C";
}
else {
console.error("Main temperature is missing in fetchedData", fetchedData);
}
};
//Search City
const searchCity = () => {
const inputElement = document.getElementById("searchInput");
if (!inputElement) {
alert("Search input element not found in the DOM");
return;
}
const input = inputElement.value;
if (input.trim() === "") {
alert("Please enter a city name");
return;
}
let searchURL = `https://api.openweathermap.org/data/2.5/forecast?q=${input}&appid=15a1790288c26c9ab80c3b6f2209e071`;
fetchData(searchURL);
// This clears the input field after the search
document.getElementById("searchInput").value = ""
};
//Event Listeners
searchButton.addEventListener("click", searchCity);
const nextCityClick = () => {
if (fetchedData[0].city.name === "Stockholm") {
fetchData(parURL);
}
else if (fetchedData[0].city.name === "Paris") {
fetchData(barURL);
}
else {
fetchData(defaultURL);
}
};
nextCity.addEventListener("click", nextCityClick);
//Initial fetch
fetchData(defaultURL);