-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (147 loc) · 5.46 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
const userTab = document.querySelector("#your-weather");
const searchTab = document.querySelector("#search-weather");
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 grantAccessButton = document.querySelector("#grant-access");
const searchInput = document.querySelector("#data-searchInput");
var display = 0;
let currTab = userTab;
const API_KEY = "2ab605e0cd10cb65ce801299f8641a3c";
currTab.classList.add("current-tab");
grantAccessContainer.classList.add("active");
function switchTab(clickedTab)
{
if (clickedTab != currTab)
{
//Tab Switching done
currTab.classList.remove("current-tab");
currTab = clickedTab;
currTab.classList.add("current-tab");
// check search form was active ("searchTab") or not("userTab")
if(!searchForm.classList.contains("active"))
{
userInfoContainer.classList.remove("active");
grantAccessContainer.classList.remove("active");
searchForm.classList.add("active");
}
else if(display !== 0){
searchForm.classList.remove("active");
userInfoContainer.classList.remove("active");
// Now in userTab
getfromSessionStorage();
}
else{
searchForm.classList.remove("active");
userInfoContainer.classList.remove("active");
grantAccessContainer.classList.add("active");
}
}
}
function getfromSessionStorage()
{
const localCoordinates = sessionStorage.getItem("user-coordinates");
if(!localCoordinates){
grantAccessContainer.classList.add("active");
}
else{
const coordinates = JSON.parse(localCoordinates);
fetchUserWeatherInfo(coordinates);
}
}
async function fetchUserWeatherInfo(coordinates)
{
const {lat,long} = coordinates;
// grant access invisible
grantAccessContainer.classList.remove("active");
// loader visible
loadingScreen.classList.add("active");
//API CALL
try{
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=${API_KEY}&units=metric`);
const data = await response.json();
loadingScreen.classList.remove("active");
userInfoContainer.classList.add("active");
renderWeatherInfo(data);
}
catch(err)
{
alert('Unable to fetch details');
}
}
function renderWeatherInfo(weatherInfo)
{
const cityName = document.querySelector("#data-cityName");
const countryIcon = document.querySelector("#data-countryIcon");
const desc = document.querySelector("#data-weatherDescription");
const weatherIcon = document.querySelector("#data-weatherIcon");
const temp = document.querySelector("#data-temperature");
const windSpeed = document.querySelector("#data-windSpeed");
const humidity = document.querySelector("#data-humidity");
const cloudiness = document.querySelector("#data-cloudiness")
cityName.innerText = `${weatherInfo?.name}`;
countryIcon.src = `https://flagcdn.com/16x12/${weatherInfo?.sys?.country.toLowerCase()}.png`;
desc.innerText = weatherInfo?.weather?.[0]?.description;
weatherIcon.src = `https://openweathermap.org/img/w/${weatherInfo?.weather?.[0]?.icon}.png`;
temp.innerText = `${weatherInfo?.main?.temp} °C`;
windSpeed.innerText = `${weatherInfo?.wind?.speed}m/s`;
humidity.innerText = `${weatherInfo?.main?.humidity}%`;
cloudiness.innerText = `${weatherInfo?.clouds?.all}%`;
}
userTab.addEventListener("click",()=>{
switchTab(userTab);
});
searchTab.addEventListener("click",()=>{
switchTab(searchTab);
})
function getlocation()
{
if(navigator.geolocation)
{
display++;
navigator.geolocation.getCurrentPosition(showPosition);
}
else{
alert('Location Support Unavailabe');
}
}
function showPosition(position)
{
const userCoordinates = {
lat : position.coords.latitude,
long : position.coords.longitude,
}
sessionStorage.setItem("user-coordinates" , JSON.stringify(userCoordinates));
fetchUserWeatherInfo(userCoordinates);
}
grantAccessButton.addEventListener("click",getlocation);
searchForm.addEventListener("submit",(e)=>{
e.preventDefault();
let cityName = searchInput.value;
if(cityName === "") return;
fetchSearchweatherInfo(cityName);
})
async function fetchSearchweatherInfo(cityName){
loadingScreen.classList.add("active");
userInfoContainer.classList.remove("active");
grantAccessContainer.classList.remove("active");
try{
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${API_KEY}&units=metric`);
const data = await response.json();
if(data?.name)
{
loadingScreen.classList.remove("active");
userInfoContainer.classList.add("active");
renderWeatherInfo(data);
}
else{
loadingScreen.classList.remove("active");
}
}
catch(err)
{
alert('Unable to fetch details');
}
}