-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
157 lines (124 loc) · 5.09 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
const yourLocTab = document.querySelector(".current-tab");
const searchLocTab = document.querySelector(".search-weather");
const whetherReportContainer = document.querySelector(".weather-report-container");
const searchBoxContainer = document.querySelector(".search-box-contaienr");
const searchBoxInput = document.getElementById("search-box");
const searchBoxSubmitBtn=document.getElementById("search-box-submit-btn");
const grantLoactionContainer = document.getElementById("grantLocationCont");
const loadingContainer = document.querySelector(".loading-container");
const API_KEY = "d1845658f92b31c64bd94f06f7188c9c";
const grantAccessBtn = document.getElementById("grant-access-btn");
const cityNotFoundContainer=document.getElementsByClassName("city-not-found");
defaultScreen();
searchLocTab.addEventListener("click", () => {
yourLocTab.classList.remove("tab-highlight");
loadingContainer.classList.remove("active");
cityNotFoundContainer[0].classList.remove("active");
searchLocTab.classList.add("tab-highlight");
whetherReportContainer.classList.remove("active");
searchBoxContainer.classList.add("searc-box-active");
grantLoactionContainer.classList.remove("active");
searchBoxInput.value = "";
});
yourLocTab.addEventListener("click", defaultScreen);
function defaultScreen() {
searchBoxContainer.classList.remove("searc-box-active");
searchLocTab.classList.remove("tab-highlight");
yourLocTab.classList.add("tab-highlight");
getfromSessionStorage();
}
function getfromSessionStorage() {
const localCoordinates = sessionStorage.getItem("user-coordinates");
if (!localCoordinates) {
grantLoactionContainer.classList.add("active");
} else {
const cordinates = JSON.parse(localCoordinates);
fetchUserWeatherInfo(cordinates);
}
}
async function fetchUserWeatherInfo(cordinates) {
const { lat, lon } = cordinates;
grantLoactionContainer.classList.remove("active");
loadingContainer.classList.add("active");
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();
renderWeatherInfo(data);
loadingContainer.classList.remove("active");
whetherReportContainer.classList.add("active");
console.log(data);
}
catch (err) {
loadingContainer.classList.remove("active");
// hw
}
}
let countryFlags=document.getElementById("country-flag");
let whetherTypeIcon=document.getElementById("weather-type-image");
function renderWeatherInfo(data) {
const cityName = document.getElementById("city-name");
const whetherType = document.getElementById("weather-type");
const temp = document.getElementById("temprature");
const windSpeed = document.getElementById("wind-speed");
const humidity = document.getElementById("humidity");
const clouds = document.getElementById("clouds");
countryFlags.src=`https://flagcdn.com/16x12/${data?.sys?.country.toLowerCase()}.png`;
whetherTypeIcon.src=`http://openweathermap.org/img/w/${data?.weather?.[0]?.icon}.png`;
temp.innerText = `${data?.main?.temp} °C`;
cityName.innerText = data?.name;
whetherType.innerText = data?.weather?.[0]?.description;
windSpeed.innerText = `${data?.wind?.speed} m/s`;
humidity.innerText = `${data?.main?.humidity}%`;
clouds.innerText = `${data?.clouds?.all}%`;
}
grantAccessBtn.addEventListener("click", getLocation)
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
// hw show alert to ...
alert("your browser does not support geo-location");
}
}
function showPosition(position) {
const userCoordinates = {
lat: position.coords.latitude,
lon: position.coords.longitude,
};
sessionStorage.setItem("user-coordinates", JSON.stringify(userCoordinates));
fetchUserWeatherInfo(userCoordinates);
}
searchBoxContainer.addEventListener("submit",(e)=>{
e.preventDefault();
if(searchBoxInput.value==="")
return;
else
fetchSearchWeatherInfo();
});
async function fetchSearchWeatherInfo(){
loadingContainer.classList.add("active");
searchBoxContainer.classList.remove("searc-box-active");
// grantAccessContainer.classList.remove("active");
try {
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${searchBoxInput.value}&appid=${API_KEY}&units=metric`
);
const data = await response.json();
console.log("data" ,data);
if(data?.message=="city not found")
{
// alert("city Not found");
loadingContainer.classList.remove("active");
cityNotFoundContainer[0].classList.add("active");
return;
}
renderWeatherInfo(data);
loadingContainer.classList.remove("active");
whetherReportContainer.classList.add("active");
}
catch(err) {
alert("search box not working ");
}
}