-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
101 lines (84 loc) · 3.29 KB
/
app.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
//state
let currCity='London'
let units='metric'
//Selectors
let city=document.querySelector('.weather-city');
let datetime = document.querySelector(".weather-datetime");
let weather_forcast=document.querySelector('.weather-forcast');
let weather_temp = document.querySelector(".weather-tempreture");
let weather_icon=document.querySelector('.weather-icon');
let weather_minmax = document.querySelector(".weather-minmax");
let weather_realfeel = document.querySelector(".weather-realfeel");
let weather_humidity = document.querySelector(".weather-humidity");
let weather_wind = document.querySelector(".weather-wind");
let weather_pressure = document.querySelector(".weather-pressure");
//search
document.querySelector('.weather-search').addEventListener('submit',e=>{
let search=document.querySelector('.weather-searchform');
//prevent default actions
e.preventDefault();
currCity=search.value;
//get weather forecast
getWeather();
//clear form
search.value='';
})
//units
document.querySelector('.weather-unit-celcius').addEventListener('click',()=>{
if(units!=='metric'){
//change to metric
units = "metric";
//get the weather forcast
getWeather();
}
})
document
.querySelector(".weather-unit-farenheight")
.addEventListener("click", () => {
if (units !== "imperial") {
//change to imperial
units = "imperial";
//get the weather forcast
getWeather();
}
});
//convert country Code to name
const convertCountryCode=(country)=>{
let regionNames=new Intl.DisplayNames(['en'],{type:'region'});
return(regionNames.of(country))
}
//convert timezone and time stamp
const convertTimeStamp=(timeStamp,timeZone)=>{
const convertTimeZone=timeZone/3600;
const date=new Date(timeStamp*1000);
const options={
weekDay:'lang',
day:'numeric',
month:'long',
year:'numeric',
hour:'numeric',
minute:'numeric',
timeZone:`Etc/GMT${convertTimeZone>=0? "-":"+"}${Math.abs(convertTimeZone)}`,
hour12:true
}
return date.toLocaleString('en-US',options)
}
// let
const getWeather=()=>{
const API_KEY = "a9ea75c3911512734ecb522c590b65aa";
fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${currCity}&appid=${API_KEY}&units=${units}`).then
(res=>res.json()).then(data=>{
city.innerHTML=`${data.name},${convertCountryCode(data.sys.country)}`
datetime.innerHTML=convertTimeStamp(data.dt,data.timezone)
weather_forcast.innerHTML=`<p>${data.weather[0].main}`
weather_temp.innerHTML=`${data.main.temp.toFixed()}°`
weather_icon.innerHTML = `<img src="http://openweathermap.org/img/wn/${data.weather[0].icon}@4x.png" alt="" />`;
weather_minmax.innerHTML = `<p>Min: ${data.main.temp_min.toFixed()}°</p><p>Max: ${data.main.temp_max.toFixed()}°</p>`;
weather_realfeel.innerHTML = `${data.main.feels_like.toFixed()}°`;
weather_humidity.innerHTML = `${data.main.humidity}%`;
weather_wind.innerHTML = `${data.wind.speed}${units==='imperial'?'mph':'m/s'}`;
weather_pressure.innerHTML = `${data.main.pressure}hPa`;
})
}
document.addEventListener('load',getWeather())