-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (132 loc) · 5.51 KB
/
index.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
const container = document.querySelector('.container');
const search = document.querySelector('.search-box button');
const weatherBox = document.querySelector('.weather-box');
const weatherDetails = document.querySelector('.weather-details');
const cityCountryElement = document.querySelector('.container .city-country');
const error404 = document.querySelector('.not-found');
const temperature = document.querySelector('.weather-box .temperature');
let isFahrenheit = false;
async function getCityCountryInfo(city) {
const APIKey = '2437a4c75fbf487f697570c80773f8c5';
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${APIKey}`);
const data = await response.json();
if (data.cod === '404') {
return null;
}
const cityCountryInfo = {
city: data.name,
country: data.sys.country,
};
return cityCountryInfo;
}
async function searchWeather() {
const APIKey = '2437a4c75fbf487f697570c80773f8c5';
const cityInput = document.querySelector('.search-box input');
const city = cityInput.value;
if (city === '') {
return;
}
try {
const json = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${APIKey}`)
.then(response => response.json());
const cityCountryInfo = await getCityCountryInfo(city);
if (json.cod === '404') {
container.style.height = '400px';
weatherBox.style.display = 'none';
weatherDetails.style.display = 'none';
cityCountryElement.style.display = 'none'
error404.style.display = 'block';
error404.classList.add('fadeIn');
return;
}
error404.style.display = 'none';
error404.classList.remove('fadeIn');
const image = document.querySelector('.weather-box img');
const description = document.querySelector('.weather-box .description');
const humidity = document.querySelector('.weather-details .humidity span');
const wind = document.querySelector('.weather-details .wind span');
switch (json.weather[0].main) {
case 'Clear':
image.src = 'images/clear.png';
break;
case 'Rain':
image.src = 'images/rain.png';
break;
case 'Snow':
image.src = 'images/snow.png';
break;
case 'Clouds':
image.src = 'images/cloud.png';
break;
case 'Haze':
image.src = 'images/mist.png';
break;
default:
image.src = '';
}
const cityTimezone = json.timezone / 3600; // Time zone received
const currentUTCHour = new Date().getUTCHours();
const currentLocalHour = currentUTCHour + cityTimezone;
temperature.innerHTML = `${parseInt(json.main.temp)}<span>°C</span>`;
description.innerHTML = `${json.weather[0].description}`;
humidity.innerHTML = `${json.main.humidity}%`;
wind.innerHTML = `${parseInt(json.wind.speed)}Km/h`;
// Show time of city and country
const options = {
timeZone: `Etc/GMT${cityTimezone >= 0 ? '-' : '+'}${Math.abs(cityTimezone)}`,
hour: 'numeric',
minute: 'numeric',
hour12: false,
};
const cityTime = new Intl.DateTimeFormat('en-US', options).format(new Date());
// Add city and country
if (cityCountryInfo) {
cityCountryElement.innerHTML = `${cityCountryInfo.city}, ${cityCountryInfo.country}`;
cityCountryElement.innerHTML += ` - ${cityTime}`;
} else {
cityCountryElement.innerHTML = '';
}
// Change background color according to city time
const isNight = currentLocalHour < 6 || currentLocalHour > 18;
if (isNight) {
document.body.style.background = '#08162A'; // Night background color
} else {
document.body.style.background = '#06283D'; // Day background color
}
weatherBox.style.display = '';
weatherDetails.style.display = '';
cityCountryElement.style.display = '';
weatherBox.classList.add('fadeIn');
weatherDetails.classList.add('fadeIn');
cityCountryElement.classList.add('fadeIn');
container.style.height = '590px';
// Her arama sonrasında sıcaklık birimini Celsius'a çevir
isFahrenheit = false;
toggleTemperature();
} catch (error) {
console.error('An error occurred:', error);
}
}
const toggleButton = document.getElementById('toggleButton');
function toggleTemperature() {
const temperatureValue = parseInt(temperature.innerText);
if (isFahrenheit) {
// Convert Fahrenheit to Celsius
const celsiusValue = ((temperatureValue - 32) * 5) / 9;
temperature.innerHTML = `${Math.round(celsiusValue)}<span>°C</span>`;
toggleButton.innerHTML = '°F';
} else {
// Convert Celsius to Fahrenheit
const fahrenheitValue = (temperatureValue * 9) / 5 + 32;
temperature.innerHTML = `${Math.round(fahrenheitValue)}<span>°F</span>`;
toggleButton.innerHTML = '°C';
}
isFahrenheit = !isFahrenheit;
}
toggleButton.addEventListener('click', toggleTemperature);
search.addEventListener('click' , searchWeather);
document.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
searchWeather();
}
});