-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
142 lines (127 loc) · 5.39 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
const inputBox = document.getElementById("search");
const weatherImage = document.querySelector(".weather-icon");
const currTemp = document.querySelector(".curr-temp");
const weatherStatus = document.querySelector(".weather-status");
const cityName = document.querySelector(".city");
const countryName = document.querySelector(".country");
const uvIndex = document.querySelector(".uv-data");
const windSpeed = document.querySelector(".wind-data");
const sunriseTime = document.querySelector(".sunrise span");
const sunsetTime = document.querySelector(".sunset span");
const humidity = document.querySelector(".humidity-data");
const visibility = document.querySelector(".visibility-data");
const feelsLike = document.querySelector(".feels-like-data");
const forecastCardDays = document.querySelectorAll(".days");
const forecastCardTemp = document.querySelectorAll(".forecast-temp-data");
const forecastRainData = document.querySelectorAll(".forecast-rain-data");
const forecastHumidityData = document.querySelectorAll(
".forecast-humidity-data"
);
const forecastCardImage = document.querySelectorAll(".forecast-image-data");
const forecastDate = document.querySelectorAll(".forecast-date");
const forecastCondition = document.querySelectorAll(
".forecast-weather-condition"
);
const onPageLoad = async () => {
const defaultApiCall = await fetch(
`https://api.weatherapi.com/v1/forecast.json?key=e9f03c0935864a1ba58105924231102&q=kolkata&days=7`
);
const defaultWeatherData = await defaultApiCall.json();
currTemp.innerText = defaultWeatherData.current.temp_c;
// weatherImage.src = defaultWeatherData.current.condition.icon;
weatherStatus.innerText = defaultWeatherData.current.condition.text;
cityName.innerText = defaultWeatherData.location.name;
countryName.innerText = defaultWeatherData.location.country;
uvIndex.innerText = defaultWeatherData.current.uv;
windSpeed.innerText = `${defaultWeatherData.current.wind_kph} km/h`;
sunriseTime.innerText =
defaultWeatherData.forecast.forecastday[0].astro.sunrise;
sunsetTime.innerText =
defaultWeatherData.forecast.forecastday[0].astro.sunset;
humidity.innerText = defaultWeatherData.current.humidity;
visibility.innerText = defaultWeatherData.current.vis_km;
feelsLike.innerText = defaultWeatherData.current.feelslike_c;
for (let i = 0; i <= 2; i++) {
const dateData = defaultWeatherData.forecast.forecastday[i].date;
const dateString = dateData.split("-").join(",");
const date = new Date(dateString);
const daysOfWeek = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
forecastDate[i].innerText = defaultWeatherData.forecast.forecastday[i].date;
forecastCardDays[i].innerText = daysOfWeek[date.getDay()];
forecastCardDays[0].innerText = "Today";
forecastCardDays[1].innerText = "Tomorrow";
forecastCondition[i].innerText =
defaultWeatherData.forecast.forecastday[i].day.condition.text;
forecastCardTemp[i].innerText =
defaultWeatherData.forecast.forecastday[i].day.avgtemp_c;
forecastRainData[i].innerText =
defaultWeatherData.forecast.forecastday[i].day.daily_chance_of_rain;
forecastHumidityData[i].innerText =
defaultWeatherData.forecast.forecastday[i].day.avghumidity;
forecastCardImage[i].src =
defaultWeatherData.forecast.forecastday[i].day.condition.icon;
}
};
onPageLoad();
let data;
const getData = async (event) => {
event.preventDefault();
if (!inputBox.value) {
alert("Please Enter City Name");
return;
}
const inputValue = inputBox.value;
const fetchData = await fetch(
`https://api.weatherapi.com/v1/forecast.json?key=e9f03c0935864a1ba58105924231102&q=${inputValue}&days=7`
);
const weatherData = await fetchData.json();
data = weatherData;
currTemp.innerText = data.current.temp_c;
weatherImage.src = data.current.condition.icon;
weatherStatus.innerText = data.current.condition.text;
cityName.innerText = data.location.name;
countryName.innerText = data.location.country;
uvIndex.innerText = data.current.uv;
windSpeed.innerText = `${data.current.wind_kph} km/h`;
sunriseTime.innerText = data.forecast.forecastday[0].astro.sunrise;
sunsetTime.innerText = data.forecast.forecastday[0].astro.sunset;
humidity.innerText = data.current.humidity;
visibility.innerText = data.current.vis_km;
feelsLike.innerText = data.current.feelslike_c;
for (let i = 0; i <= 2; i++) {
const dateData = data.forecast.forecastday[i].date;
const dateString = dateData.split("-").join(",");
const date = new Date(dateString);
const daysOfWeek = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
forecastDate[i].innerText = data.forecast.forecastday[i].date
.split("-")
.join("/");
forecastCardDays[i].innerText = daysOfWeek[date.getDay()];
forecastCardDays[0].innerText = "Today";
forecastCardDays[1].innerText = "Tomorrow";
forecastCondition[i].innerText =
data.forecast.forecastday[i].day.condition.text;
forecastCardTemp[i].innerText = data.forecast.forecastday[i].day.avgtemp_c;
forecastRainData[i].innerText =
data.forecast.forecastday[i].day.daily_chance_of_rain;
forecastHumidityData[i].innerText =
data.forecast.forecastday[i].day.avghumidity;
forecastCardImage[i].src = data.forecast.forecastday[i].day.condition.icon;
}
};