forked from Technigo/project-weather-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
172 lines (149 loc) · 6.41 KB
/
app.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
// API key for OpenWeatherMap
const API_KEY = 'fb560f0e3d208f655263c202ebe8452d';
// URLs for current weather and 5-day forecast
const CURRENT_WEATHER_URL = 'https://api.openweathermap.org/data/2.5/weather';
const FORECAST_URL = 'https://api.openweathermap.org/data/2.5/forecast';
// Default city
let city = 'Stockholm';
// DOM selector for current weather and forecast
const currentWeatherElement = document.getElementById('currentWeather');
const forecastElement = document.getElementById('forecast');
// Array of weekday names
const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
// search icon and input field
const searchIcon = document.getElementById('searchIcon');
const cityInput = document.getElementById('cityInput');
const searchContainer = document.querySelector('.search-container');
searchIcon.addEventListener('click', function () {
searchContainer.classList.toggle('active');
if (searchContainer.classList.contains('active')) {
cityInput.focus();
} else {
cityInput.blur();
}
});
// Trigger the search
cityInput.addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
const cityInputValue = cityInput.value.trim();
if (cityInputValue) {
city = cityInputValue;
fetchWeatherData(city);
fetchForecastData(city);
cityInput.value = '';
} else {
alert('Please enter a city name.');
}
}
});
// Function to convert time format
function convertUnixToTime(unixTime) {
const date = new Date(unixTime * 1000);
const hours = date.getHours();
const minutes = date.getMinutes();
return `${hours}:${minutes < 10 ? '0' : ''}${minutes}`;
}
// Function to change background image based on the weather
function setBackground(weatherDescription, isDaytime) {
if (isDaytime) {
if (weatherDescription.includes('clear')) {
currentWeatherElement.style.backgroundImage = "url('daytime-clear.jpg')";
} else if (weatherDescription.includes('clouds')) {
currentWeatherElement.style.backgroundImage = "url('daytime-cloudy.jpg')";
} else {
currentWeatherElement.style.backgroundImage = "url('daytime.jpg')";
}
} else {
if (weatherDescription.includes('clear')) {
currentWeatherElement.style.backgroundImage = "url('night-clear.jpg')";
} else if (weatherDescription.includes('clouds')) {
currentWeatherElement.style.backgroundImage = "url('night-cloudy.jpg')";
} else {
currentWeatherElement.style.backgroundImage = "url('night.jpg')";
}
}
currentWeatherElement.style.backgroundSize = "cover";
}
// Function to fetch current weather
function fetchWeatherData(city) {
fetch(`${CURRENT_WEATHER_URL}?q=${city}&units=metric&APPID=${API_KEY}`)
.then(response => response.json())
.then(data => {
console.log('Current Weather:', data);
// Data from the response
const location = data.name;
const temperature = Math.round(data.main.temp);
const description = data.weather[0].description;
const sunrise = convertUnixToTime(data.sys.sunrise);
const sunset = convertUnixToTime(data.sys.sunset);
const currentTime = new Date().getTime() / 1000;
const isDaytime = currentTime >= data.sys.sunrise && currentTime < data.sys.sunset;
// Set background image based on weather description and time of day
setBackground(description.toLowerCase(), isDaytime);
// Data for current weather
currentWeatherElement.innerHTML = `
<div id="currentWeather">
<p class="temperature">${temperature}°C</p>
<p class="location">${location}</p>
<p class="time">Time: ${convertUnixToTime(data.dt)}</p>
<p class="description">${description}</p>
<div class="sun-info">
<p>sunrise <span>${sunrise}</span></p>
<p>sunset <span>${sunset}</span></p>
</div>
</div>
`;
})
.catch(error => {
console.error('Error fetching current weather:', error);
alert('Failed to retrieve current weather data. Please try again later.');
});
}
// Fetch forecast data
function fetchForecastData(city) {
fetch(`${FORECAST_URL}?q=${city}&units=metric&APPID=${API_KEY}`)
.then(response => response.json())
.then(data => {
console.log('Forecast Data:', data);
const forecastList = data.list;
const forecastByDay = {};
forecastList.forEach(entry => {
const dateTime = entry.dt_txt;
const time = dateTime.split(' ')[1];
if (time === '12:00:00') {
const date = dateTime.split(' ')[0];
if (!forecastByDay[date]) {
forecastByDay[date] = entry;
}
}
});
// Data for the next 4 days
const forecastDays = Object.keys(forecastByDay).slice(0, 4);
if (forecastDays.length === 0) {
forecastElement.innerHTML = '<p>No forecast data available at 12:00 PM.</p>';
} else {
forecastElement.innerHTML = '';
forecastDays.forEach(date => {
const forecast = forecastByDay[date];
const temperature = Math.round(forecast.main.temp);
const windSpeed = forecast.wind.speed;
const dayOfWeek = new Date(date).getDay();
const dayName = weekdays[dayOfWeek];
forecastElement.innerHTML += `
<div class="forecast-item">
<p class="day"><strong>${dayName}</strong></p>
<p class="temp">${temperature}°C</p>
<p class="wind">${windSpeed} m/s</p>
</div>
`;
});
}
})
.catch(error => {
console.error('Error fetching forecast:', error);
alert('Failed to retrieve forecast data. Please try again later.');
});
}
// Invoking the functions
fetchWeatherData(city);
fetchForecastData(city);