-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
181 lines (149 loc) · 6.66 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
171
172
173
174
175
176
177
178
179
180
181
// API LINK : https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid="+API_KEY BY CITY
// API LINK : "https://api.openweathermap.org/data/2.5/weather?lat="+latitude+"&lon="+longitude+"&appid="+API_KEY+"&units=metric" BY Coordinates
const userTab = document.querySelector('[data-userWeather]');
const searchTab = document.querySelector('[data-searchWeather]');
const userContainer = document.querySelector(".weather-container");
const grantAccessContainer = document.querySelector(".grant-location-container");
const searchForm = document.querySelector("[data-searchForm]");
const loadingScreen = document.querySelector(".loading-container");
const userInfoContainer = document.querySelector(".user-info-container");
const errorContainer = document.querySelector(".error-container");
// INITIAL VARIABLES
let currentTab = userTab;
const API_KEY = "182ae2910d93f81d4a72e1292a67307e";
currentTab.classList.add("current-tab");
errorContainer.classList.remove("active");
getFromSessionStorage();
// SWITCH FUNCTION
function switchTab(clickedTab){
errorContainer.classList.remove("active");
if(clickedTab != currentTab){
currentTab.classList.remove("current-tab");
currentTab = clickedTab;
currentTab.classList.add("current-tab")
if(!searchForm.classList.contains("active")){
userInfoContainer.classList.remove("active");
grantAccessContainer.classList.remove("active");
searchForm.classList.add("active");
} else{
searchForm.classList.remove("active");
userInfoContainer.classList.remove("active");
getFromSessionStorage();
}
}
}
// // get coordinates from CURRENT SESSION
function getFromSessionStorage(){
// console.log('Session Storage');
const localCoordinates = sessionStorage.getItem("user-coordinates");
// console.log(localCoordinates);
if(!localCoordinates){
grantAccessContainer.classList.add("active");
} else{
const coordinates = JSON.parse(localCoordinates);
// console.log(typeof(coordinates));
// console.log('Entering');
fetchUserWeatherInfo(coordinates);
}
}
// FUNCTION THAT FETCH THE WEATHER INFORMATION USING THE API
async function fetchUserWeatherInfo(coordinates){
errorContainer.classList.remove("active");
// alert('Inside Fetch Function');
// console.log(coordinates.lat);
// console.log(coordinates.longi);
grantAccessContainer.classList.remove("active");
// LOADING SCREEN VISIBLE
loadingScreen.classList.add("active");
// API CALL
try{
const response = await fetch("https://api.openweathermap.org/data/2.5/weather?lat="+coordinates.lat+"&lon="+coordinates.longi+"&appid=182ae2910d93f81d4a72e1292a67307e&units=metric");
const data = await response.json();
loadingScreen.classList.remove("active");
userInfoContainer.classList.add("active");
renderWeatherInfo(data);
} catch(error){
loadingScreen.classList.add("active");
}
}
// SWITCHING BETWEEN TABS
userTab.addEventListener("click", () => {
switchTab(userTab);
})
searchTab.addEventListener("click", () => {
switchTab(searchTab);
})
// TO RENDER THE INFORMATION ON THE WEBSITE
function renderWeatherInfo(weatherInfo){
const cityName = document.querySelector("[data-cityName]");
const countryIcon = document.querySelector("[data-countryIcon]");
const Description = document.querySelector("[data-weatherDesc]");
const weatherIcon = document.querySelector("[data-weatherIcon]");
const Temp = document.querySelector("[data-temp]");
const windSpeed = document.querySelector("[data-windSpeed]");
const humidity = document.querySelector("[data-humidity]");
const cloudiness = document.querySelector("[data-cloudiness]");
// FETCHING THE CONTENT FROM THE API
cityName.innerText = weatherInfo?.name; //NAME OF THE CITY
countryIcon.src = "https://flagcdn.com/144x108/"+weatherInfo?.sys?.country.toLowerCase()+".png"; //FLAG OF COUNTRY
Description.innerText = weatherInfo?.weather?.[0]?.description; //DESCRIPTION OF THE WEATHER
weatherIcon.src = "http://openweathermap.org/img/w/"+weatherInfo?.weather?.[0]?.icon+".png"; //ICON OF WEATHER
Temp.innerText = weatherInfo?.main?.temp+" ℃";
windSpeed.innerText = weatherInfo?.wind?.speed+" m/s";
humidity.innerText = weatherInfo?.main?.humidity+" %";
cloudiness.innerText = weatherInfo?.clouds?.all+" %";
}
// FOR GETTING THE LOCATION OF THE USER
function getLocation(){
if(navigator.geolocation){
// console.log('Location supported');
navigator.geolocation.getCurrentPosition(showPosition);
} else{
console.log('Please allow access to location!!');
}
}
function showPosition(position){
// console.log('fetching the Location');
const userCoordinates = {
lat: position.coords.latitude.toFixed(3),
longi: position.coords.longitude.toFixed(3)
};
// console.log(userCoordinates.lat);
// console.log(userCoordinates.longi);
// console.log(userCoordinates);
sessionStorage.setItem("user-coordinates", JSON.stringify(userCoordinates));
// console.log(userCoordinates);
// console.log('Before Entering Function');
fetchUserWeatherInfo(userCoordinates);
}
const grantAccessButton = document.querySelector("[data-grantAccess]");
grantAccessButton.addEventListener("click", getLocation());
// FOR THE SEARCH OF CITY
const searchInput = document.querySelector("[data-searchInput]");
searchForm.addEventListener("submit", (e) => {
e.preventDefault();
let cityName = searchInput.value;
// console.log('City name ' + cityName);
if(cityName === ""){
return;
} else {
fetchUserWeatherInfoCity(cityName);
}
});
async function fetchUserWeatherInfoCity(city){
errorContainer.classList.remove("active");
loadingScreen.classList.add("active");
userInfoContainer.classList.remove("active");
grantAccessContainer.classList.remove("active");
let response = await fetch("https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid=182ae2910d93f81d4a72e1292a67307e&units=metric");
let data = await response.json();
if(data?.cod == "200"){
loadingScreen.classList.remove("active");
userInfoContainer.classList.add("active");
renderWeatherInfo(data);
} else {
loadingScreen.classList.remove("active");
errorContainer.classList.add("active");
alert('No Data Found');
}
};