-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
184 lines (149 loc) 路 5.65 KB
/
Copy pathscript.js
File metadata and controls
184 lines (149 loc) 路 5.65 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
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
182
183
184
const userTab=document.querySelector("[data-userWeather]");
const searchTab=document.querySelector("[data-searchWeather]")
const userCont=document.querySelector(".weather-container");
const grantAccess=document.querySelector(".grant-location-container");
const grantAccessButton=document.querySelector(".btn");
const userSearchForm=document.querySelector("[data-searchForm]");
const userInp=document.querySelector("[data-userInput]");
const loadingContainer=document.querySelector(".loading-container");
const userWeather=document.querySelector(".user-info-container");
const cityName=document.querySelector("[data-cityName]");
const countryIcon=document.querySelector("[data-countryIcon]");
const weatherDesc=document.querySelector("[data-weatherDesc]");
const weatherIcon=document.querySelector("[data-weatherIcon]");
const temp=document.querySelector("[data-temp]");
const windspeed=document.querySelector("[data-windspped]");
const humidity=document.querySelector("[data-humidity]");
const clouds=document.querySelector("[data-clouds]");
//initially variables need
const API_KEY="12f58fd1166be5fc552fb2ac2665269c";
let currentTab = userTab;
currentTab.classList.add("current-tab");
getfromSessionStorage();
//switching
function switchTab(clickedTab){
if(currentTab!=clickedTab){
currentTab.classList.remove("current-tab");
//usertab ko invisible, clickedtab ko visible
currentTab=clickedTab;
currentTab.classList.add("current-tab");
if(!userSearchForm.classList.contains("active")){
userWeather.classList.remove("active");
grantAccess.classList.remove("active");
userSearchForm.classList.add("active");
}
else{
//visible weather tab
userSearchForm.classList.remove("active");
userWeather.classList.remove("active");
//apni location ki weather display
// for Coordinates, if we have saved there
getfromSessionStorage();
}
}
}
userTab.addEventListener("click", ()=>{
//pass the input parameter
switchTab(userTab);
});
searchTab.addEventListener("click", ()=>{
switchTab(searchTab);
});
//check if coordinates are already present in session storage(local)
function getfromSessionStorage(){
const localCoordinates=sessionStorage.getItem("user-coordinates");
if(!localCoordinates){
//display grant access container
grantAccess.classList.add("active");
}
else{
const coordinates=JSON.parse(localCoordinates);
fetchUserWeatherInfo(coordinates);
}
}
//fetch according to latitude and longitude
async function fetchUserWeatherInfo(coordinates){
const {lat, lon} = coordinates;
//make grantaccess invisible
grantAccess.classList.remove("active");
//loader visible
loadingContainer.classList.add("active");
// API call
try{
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`);
const data = await response.json();
//loader remove
loadingContainer.classList.remove("active");
userWeather.classList.add("active");
renderWeatherInfo(data);
}
catch(err){
loadingContainer.classList.remove("active");
//missing
}
}
function renderWeatherInfo(weatherInfo){
// fetch the element
// already fetched above
// fetch the weatherInfo object values and display in UI
// use Optional chaining operator(?) if there is no value then it doesnot
// throw an error, it will say undefined
cityName.innerText = weatherInfo?.name;
countryIcon.src = `https://flagcdn.com/144x108/${weatherInfo?.sys?.country.toLowerCase()}.png`;
weatherDesc.innerText = weatherInfo?.weather?.[0]?.description;
weatherIcon.src = `https://api.openweathermap.org/img/w/${weatherInfo?.weather?.[0]?.icon}.png`;
temp.innerText = `${weatherInfo?.main?.temp} 掳C`;
windspeed.innerText = weatherInfo?.wind?.speed;
humidity.innerText = weatherInfo?.main?.humidity;
clouds.innerText = weatherInfo?.clouds?.all;
}
//use geoLocation API
function getLocation(){
//support available
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}
else{
//show an alert for no geolocation support available
const x = document.createElement("p");
x.innerText="Geolocation is not supported by this browser.";
//DO ANY ALERT POPUP
}
}
function showPosition(position){
const userCoordinates = {
lat: position.coords.latitude ,
lon: position.coords.longitude,
}
//store in session storage
sessionStorage.setItem("user-coordinates", JSON.stringify(userCoordinates));
fetchUserWeatherInfo(userCoordinates);
}
grantAccessButton.addEventListener("click", getLocation);
//search weather
userSearchForm.addEventListener("submit", (e)=>{
e.preventDefault();
//cityName
if(userInp.value==="")
return;
fetchSearchWeatherInfo(userInp.value);
});
//fetch according to city name
async function fetchSearchWeatherInfo(city){
// loading visible
loadingContainer.classList.add("active");
userWeather.classList.remove("active");
grantAccess.classList.remove("active");
try{
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${API_KEY}`);
const data = await response.json();
//loading invisible
loadingContainer.classList.remove("active");
userWeather.classList.add("active");
renderWeatherInfo(data);
}
catch(err){
loadingContainer.classList.remove("active");
// missing
}
}