-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
171 lines (149 loc) · 5.75 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
170
//Defining all the variables and DOM.
let cityName = document.getElementById("inputData");
let temp = document.getElementById("temp");
let min_max = document.querySelector("#min_max #temp_range");
let weather = document.querySelector("#min_max #weather");
let slider = document.getElementById("slider_button");
let airPressure = document.querySelector(".pressure");
let windSpeed = document.querySelector(".wind");
let visibility = document.querySelector(".visibility");
let rainfall = document.querySelector(".rain");
let forcast = document.querySelector(".forcast").firstElementChild;
let lastChild = document.querySelector(".forcast").lastElementChild;
let feels_like = document.querySelector(".feels_like");
let collectionOfElems = document.getElementsByClassName("hourlyForcast");
let hourlyData = Array.from(collectionOfElems);
let horizontalSlider = document.getElementById("horizontalSlide");
let buttons = document.getElementsByClassName("button");
let arrayOfButtons = Array.from(buttons);
const api_key = "970c671602970b7ec8f4c05a7ac46ea9";
//Here we are calling the function on change of the value of input element...
cityName.addEventListener("change", (event) => {
getData();
hourlyForcast();
});
//This function below makes first letter capital casing....
const letterCasing = () => {
let arrayOfNames = [cityName.value, weather.innerHTML];
arrayOfNames.forEach(function (elem) {
let correctName = elem.charAt(0).toUpperCase() + elem.slice(1);
if (elem == cityName.value) {
cityName.value = correctName;
} else {
weather.innerHTML = correctName;
}
});
};
//Fetching current weather data using async/await.
const getData = async () => {
try {
//Here we are fetching the weather related data from the weather API...
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${cityName.value}&appid=${api_key}`
);
const data = await response.json();
//We are extracting the temperature basis on the cityname...
if (data.main?.temp == undefined) {
return (temp.innerHTML = "Not found !!");
} else {
temp.innerHTML = Math.ceil(data.main.temp - 273.15) + "°C";
}
//We are extracting the waether description from the data we have got as a response...
let weather_description = data.weather[0].description;
weather.innerHTML = weather_description;
//Check about this function above and this is the correct place to call this func...
letterCasing();
// Here we will get the min and max temperature from the data...
let min_temp = Math.floor(data.main.temp_min - 273.15);
let max_temp = Math.ceil(data.main.temp_max - 273.15);
min_max.innerHTML = min_temp + " ~ " + max_temp + "°C";
//Air pressure
airPressure.innerHTML = data.main.pressure + " hPa";
//Wind speed
windSpeed.innerHTML = data.wind.speed + " m/s";
//Visibility
visibility.innerHTML = data.visibility / 1000 + " km";
//Chances of Rain
rainfall.innerHTML = data.clouds.all + " %";
//Getting city name and temp..
forcast.innerHTML = cityName.value;
lastChild.innerHTML = temp.innerHTML;
feels_like.innerHTML = Math.ceil(data.main.feels_like - 273.15) + " °C";
} catch (error) {
console.log(error);
console.log("link has been broken !!!");
}
};
getData();
//Fetching hourlyforcast weather data.
const hourlyForcast = async () => {
try {
let fetchingData = await fetch(
`https://api.openweathermap.org/data/2.5/forecast?q=${cityName.value}&appid=${api_key}`
);
let res = await fetchingData.json();
// Here we are modifying our html static values with 3 hour gap dynamic weather related information.
//Here we will get weather related information with a gap of each 3hours...
let dataList = res.list;
for (let i = 0; i <= 6; i++) {
let hourlyTime = dataList[i].dt_txt.slice(11).substr(0, 5);
let elementsForHourlyTime =
hourlyData[i].firstElementChild.nextElementSibling;
elementsForHourlyTime.innerHTML = hourlyTime;
let storeHourlytemp = dataList[i];
let storeElem = hourlyData[i].lastElementChild;
let finalHourlyTemp = Math.floor(storeHourlytemp.main.temp - 273.15);
storeElem.innerHTML = finalHourlyTemp + "°C";
}
} catch (error) {
console.log(error);
console.log("Link has been broken");
}
};
hourlyForcast();
//Sliding effect on a button click....
const [button1, button2] = arrayOfButtons;
arrayOfButtons.forEach((btn) => {
btn.addEventListener("click", (event) => {
// console.log(event.target);
if (button1.firstElementChild == event.target) {
console.log("First");
horizontalSlider.classList.remove("-translate-x-28");
horizontalSlider.classList.add("translate-x-1");
} else {
horizontalSlider.classList.remove("translate-x-1");
horizontalSlider.classList.add("-translate-x-28");
}
});
});
//This function below creates animation.......
let x = 0;
slider.addEventListener("click", () => {
let animate = document.querySelector(".animate");
let arrowAnimate = document.querySelector(".arrow");
console.log(arrowAnimate);
if (x == 0) {
animate.classList.remove("reverse");
animate.classList.add("slider_div");
arrowAnimate.style.transform = "rotate(180deg)";
button1.style.bottom = "26.7rem";
button2.style.bottom = "26.7rem";
x = 1;
} else {
animate.classList.remove("slider_div");
animate.classList.add("reverse");
arrowAnimate.style.transform = "rotate(0deg)";
button1.style.bottom = "3.7rem";
button2.style.bottom = "3.7rem";
x = 0;
}
});
//Preventing default regreshing of the page when enter key is pressed..
function mykey(event) {
let keyPress = event.key;
if (keyPress == "Enter") {
event.preventDefault();
getData();
hourlyForcast();
}
}