-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
142 lines (124 loc) · 5.81 KB
/
Copy pathscript.js
File metadata and controls
142 lines (124 loc) · 5.81 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
const city = document.getElementById('city');
const searchButton = document.getElementById('search');
const apiKey = "1e1e9e8caeae4074a9d51316242110";
// Displaying the location and date, image, textSatus
const locationElement = document.getElementById('location');
const dateDayElement = document.getElementById('dateDay');
const imageElement = document.getElementById('currentWeatherIcon');
const textStatusElement = document.getElementById('textStatus');
// Displaying the forecast data
const tempElement = document.getElementById('temp');
const feelsLikeElement = document.getElementById('feelsLike');
const windElement = document.getElementById('wind');
const humidityElement = document.getElementById('humidity');
const precipitationElement = document.getElementById('precipitation');
const cloudCoverElement = document.getElementById('cloudCover');
const visibilityElement = document.getElementById('visibility');
const uvIndexElement = document.getElementById('uvIndex');
const windChillElement = document.getElementById('windChill');
const heatIndexElement = document.getElementById('heatIndex');
// for alert
const alertElement = document.getElementById('alertWindow');
// for home Location
const homeLocation = document.getElementById('homeLocation');
const setHomeLocation = document.getElementById('homeIcon');
async function getCurrentData(cityName) {
try {
const response = await fetch(`https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${cityName}&aqi=no`);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
const locationData = `${data.location.name}, ${data.location.region}, ${data.location.country}`;
const timeDateData = `${data.location.localtime.slice(11, 16)} ${data.location.localtime.slice(0, 10)}`;
locationElement.innerHTML = locationData;
dateDayElement.innerHTML = timeDateData;
imageElement.src = data.current.condition.icon;
textStatusElement.innerHTML = `Current Temp. ${data.current.temp_c}°C, Feels like. ${data.current.feelslike_c}°C, ${data.current.condition.text}`;
tempElement.innerHTML = `Temp. : ${data.current.temp_c}°C`;
feelsLikeElement.innerHTML = `Feels Like : ${data.current.feelslike_c}°C`;
windElement.innerHTML = `Wind : ${data.current.wind_kph} km/h || Wind Direction : ${data.current.wind_dir}`;
humidityElement.innerHTML = `Humidity : ${data.current.humidity}%`;
precipitationElement.innerHTML = `Precipitation : ${data.current.precip_mm} mm`;
cloudCoverElement.innerHTML = `Cloud Cover : ${data.current.cloud}%`;
visibilityElement.innerHTML = `Visibility : ${data.current.vis_km} km`;
uvIndexElement.innerHTML = `UV Index : ${data.current.uv}`;
windChillElement.innerHTML = `Wind Chill : ${data.current.windchill_c}°C`;
heatIndexElement.innerHTML = `Heat Index : ${data.current.heatindex_c}°C`;
const alertDataResponse = await fetch(`https://api.weatherapi.com/v1/alerts.json?key=${apiKey}&q=${cityName}`);
const alertData = alertDataResponse.json();
if (alertData.alerts.length > 0) {
alertWindow.classList.remove('visible');
alertElement.innerHTML = alertData.alerts[0].headline;
} else {
alertElement.innerHTML = '';
}
} catch (error) {
console.error('There has been a problem with your fetch operation:', error);
}
}
async function getForecastData(cityName) {
try {
const response = await fetch(`https://api.weatherapi.com/v1/forecast.json?key=${apiKey}&q=${cityName}&days=7&aqi=no&alerts=no`);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
const forecastData = data.forecast.forecastday;
document.getElementById('forecastPanel').innerHTML = '';
forecastData.forEach(element => {
const newDiv = document.createElement('div');
newDiv.className = 'forecastDay';
newDiv.innerHTML = `
<h3>${element.date}</h3>
<img src="${element.day.condition.icon}" alt="weather icon">
<p>${element.day.mintemp_c}°C/${element.day.maxtemp_c}°C</p>
<p>${element.day.totalprecip_mm} mm</p>
<p>${element.day.daily_chance_of_rain}%</p>
<p>${element.day.avgvis_km} km</p>
`;
document.getElementById('forecastPanel').appendChild(newDiv);
});
} catch (error) {
console.error('There has been a problem with your fetch operation:', error);
}
}
searchButton.addEventListener('click', () => {
const cityName = city.value;
if (cityName) {
getCurrentData(cityName);
getForecastData(cityName);
} else {
alert('Please enter a city name');
}
});
homeLocation.addEventListener('click', () => {
const homeCity = localStorage.getItem('homeLocation');
if(homeCity){
getCurrentData(homeCity);
getForecastData(homeCity);
} else {
getCurrentData('London');
getForecastData('London');
}
});
document.addEventListener('DOMContentLoaded', () => {
const defaultLocation = localStorage.getItem('homeLocation');
if (defaultLocation) {
homeLocation.innerHTML = "";
homeLocation.innerHTML = `${defaultLocation}`;
getCurrentData(defaultLocation);
getForecastData(defaultLocation);
} else {
getCurrentData('London');
getForecastData('London');
}
});
setHomeLocation.addEventListener('click', () => {
const homeCity = city.value;
if(homeCity) {
localStorage.setItem('homeLocation', homeCity);
homeLocation.innerHTML = "";
homeLocation.innerHTML = `${homeCity}`;
}
});