-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
184 lines (145 loc) · 6.03 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
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 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');
let displayNotFound = document.querySelector('.not-found');
let currentTab = userTab;
window.addEventListener("offline", function() {
console.log("Disconnected...so sad!!!")
})
grantAccessContainer.classList.add('active');
let api_key = "57679222a09d40a0b6b164232241706";
currentTab.classList.add('current-tab');
userTab.addEventListener('click',() => {
switchTab(userTab);
});
searchTab.addEventListener('click', () =>{
switchTab(searchTab);
})
function switchTab(current){
if(current === currentTab){
return;
}else {
currentTab.classList.remove('current-tab');
currentTab = current;
currentTab.classList.add('current-tab');
displayNotFound.classList.remove('active');
if(!searchForm.classList.contains('active')){
userInfoContainer.classList.remove('active');
grantAccessContainer.classList.remove('active');
searchForm.classList.add('active');
}else{
userInfoContainer.classList.remove('active');
searchForm.classList.remove('active');
getFromSessionStroage();
}
}
}
// check if cordinates are already present
function getFromSessionStroage(){
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, lon} = coordinates;
grantAccessContainer.classList.remove('active');
loadingScreen.classList.add('active');
try{
const response = await fetch(`https://api.weatherapi.com/v1/current.json?key=${api_key}&q=${lat},${lon}`) ;
const data = await response.json();
// console.log(data);
loadingScreen.classList.remove('active');
userInfoContainer.classList.add('active');
renderWeatherInfo(data);
}catch(err){
loadingScreen.classList.remove('active');
//hw
console.log('error found ' + err);
}
}
async function renderWeatherInfo(weatherInfo){
const city = document.querySelector('[data-cityName]');
const countryIcon = document.querySelector('[data-countryIcon]');
const desc = 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]');
city.innerText = weatherInfo?.location?.name;
let countryName = weatherInfo.location.country.toLowerCase();
const countryInfo = await fetch(`https://restcountries.com/v3.1/name/${countryName}?fullText=true`);
let countryData = await countryInfo.json();
let countryId = await countryData[0].cca2;
// console.log(countryId);
countryIcon.src = `https://flagcdn.com/144x108/${countryId.toLowerCase()}.png`;
desc.innerText = weatherInfo?.current?.condition?.text;
let weatherIconSrc = weatherInfo?.current?.condition?.icon;
// console.log(weatherIconSrc);
let source = String(weatherIconSrc.substring(2));
// console.log(source);
source = "https://" + source;
weatherIcon.src = source;
temp.innerText = weatherInfo?.current?.temp_c + ' °c';
windspeed.innerText = weatherInfo?.current?.wind_kph + ' km/h';
humidity.innerText = weatherInfo?.current?.humidity + '%';
cloudiness.innerText = weatherInfo?.current?.cloud + '%';
}
function getLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}else{
//hw show an alert for no geoloaction support available.
alert('geo laoction is not supported in you system');
}
}
function showPosition(position){
const userCoordinates = {
lat: position.coords.latitude,
lon: position.coords.longitude,
}
sessionStorage.setItem('user-coordinates',JSON.stringify(userCoordinates));
fetchUserWeatherInfo(userCoordinates);
}
const grantAccessButton = document.querySelector('[data-grantAccess]');
grantAccessButton.addEventListener('click',getLocation);
const searchInput = document.querySelector('[data-searchInput]');
searchForm.addEventListener('submit',(e)=>{
e.preventDefault();
let cityName = searchInput.value;
if(cityName === ""){
return ;
}else{
fetchSearchWeatherInfo(cityName);
}
})
async function fetchSearchWeatherInfo(city){
loadingScreen.classList.add('active');
userInfoContainer.classList.remove('active');
grantAccessContainer.classList.remove('active');
try{
const response = await fetch(`https://api.weatherapi.com/v1/current.json?key=${api_key}&q=${city}`) ;
const data = await response.json();
// console.log(data);
if(data?.error?.code === 1006){
displayNotFound.classList.add('active');
userInfoContainer.classList.remove('active');
loadingScreen.classList.remove('active');
return;
}
loadingScreen.classList.remove('active');
userInfoContainer.classList.add('active');
renderWeatherInfo(data);
}catch(e){
//hw
// console.log(e);
}
}