-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (129 loc) · 4.97 KB
/
script.js
File metadata and controls
147 lines (129 loc) · 4.97 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
const userWeather = document.querySelector("[userWeather]");
const searchWeather = document.querySelector("[searchWeather]");
const grantLocation = document.querySelector(".grant-location");
const grantAccess = document.querySelector("[grant-access]");
const formContainer = document.querySelector("[location-search-form]");
const locationSearchInput = document.querySelector("[location-search-input]");
const weatherParentContainer = document.querySelector(
".weather-parent-container"
);
const loadingScreen = document.querySelector(".loading-screen");
const userWeatherInfo = document.querySelector(".user-weather-info");
const errorClass = document.querySelector(".error-class");
let currentTab = userWeather;
const API_KEY = "688c5fb47c4dab7e6ad3ae470839fea0";
currentTab.classList.add("current-tab");
getLocationFromStorage();
function switchingTabs(clickedTab) {
if (clickedTab != currentTab) {
currentTab.classList.remove("current-tab");
currentTab = clickedTab;
currentTab.classList.add("current-tab");
if (!formContainer.contains(document.querySelector(".activate"))) {
userWeatherInfo.classList.remove("activate");
grantLocation.classList.remove("activate");
formContainer.classList.add("activate");
} else {
formContainer.classList.remove("activate");
userWeatherInfo.classList.remove("activate");
getLocationFromStorage();
}
}
}
userWeather.addEventListener("click", () => {
switchingTabs(userWeather);
});
searchWeather.addEventListener("click", () => {
switchingTabs(searchWeather);
});
function getLocationFromStorage() {
const localCoordinates = sessionStorage.getItem("user-coordinates");
if (!localCoordinates) {
grantLocation.classList.add("activate");
} else {
const coordinates = JSON.parse(localCoordinates);
fetchUserWeather(coordinates);
}
}
async function fetchUserWeather(coordinates) {
const { lat, lon } = coordinates;
grantLocation.classList.remove("activate");
loadingScreen.classList.add("activate");
errorClass.classList.remove("activate");
try {
const fetchedInfo = await fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`
);
const weatherData = await fetchedInfo.json();
if (!weatherData.sys) {
throw weatherData;
}
loadingScreen.classList.remove("activate");
userWeatherInfo.classList.add("activate");
renderWeatherInfo(weatherData);
} catch (error) {
loadingScreen.classList.remove("activate");
errorClass.classList.add("activate");
}
}
function renderWeatherInfo(weatherData) {
const cityName = document.querySelector("[city-name]");
const countryFlag = document.querySelector("[country-flag]");
const weatherDescription = document.querySelector("[weather-description]");
const weatherIcon = document.querySelector("[weather-icon]");
const cityTemperature = document.querySelector("[city-temperature]");
const windspeed = document.querySelector("[windspeed]");
const humidity = document.querySelector("[humidity]");
const cloudiness = document.querySelector("[cloudiness]");
cityName.innerText = weatherData?.name;
countryFlag.src = `https://flagcdn.com/144x108/${weatherData?.sys?.country.toLowerCase()}.png`;
weatherDescription.innerText = weatherData?.weather?.[0]?.description;
weatherIcon.src = `https://openweathermap.org/img/w/${weatherData?.weather?.[0]?.icon}.png`;
cityTemperature.innerText = `${weatherData?.main?.temp}°C`;
windspeed.innerText = `${weatherData?.wind?.speed}m/s`;
humidity.innerText = weatherData?.main?.humidity + "%";
cloudiness.innerText = weatherData?.clouds?.all + "%";
}
function showPosition(position) {
const userCoordinates = {
lat: position.coords.latitude,
lon: position.coords.longitude,
};
sessionStorage.setItem("user-coordinates", JSON.stringify(userCoordinates));
fetchUserWeather(userCoordinates);
}
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
errorClass.classList.add("activate");
}
}
grantAccess.addEventListener("click", getLocation);
formContainer.addEventListener("submit", (e) => {
e.preventDefault();
let cityName = locationSearchInput.value;
if (cityName === "") return;
else fetchSearchWeather(cityName);
formContainer.reset();
});
async function fetchSearchWeather(city) {
loadingScreen.classList.add("activate");
userWeatherInfo.classList.remove("activate");
grantLocation.classList.remove("activate");
try {
const fetchedInfo = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${API_KEY}&units=metric`
);
const weatherData = await fetchedInfo.json();
if (!weatherData.sys) {
throw weatherData;
}
loadingScreen.classList.remove("activate");
userWeatherInfo.classList.add("activate");
renderWeatherInfo(weatherData);
} catch (error) {
loadingScreen.classList.remove("activate");
errorClass.classList.add("activate");
}
}