-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
121 lines (92 loc) · 3.41 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
let input=document.querySelector('.city-input');
let weatherData = document.querySelector('.weather-data');
let btn=document.querySelector('.btn-search');
btn.addEventListener('click', ()=>{
displayWeather();
})
document.addEventListener('keypress',(event)=>{
if(event.keyCode===13){
displayWeather();
}
})
//spinner
const renderLoader = () => {
let loader = `
<div class="d-flex justify-content-center m-2">
<div class="spinner-border spinner " role="status">
<span class="sr-only">Loading...</span>
</div>
</div>
`;
weatherData.insertAdjacentHTML('beforebegin', loader);
}
const clearLoader = () => {
let spinner = document.querySelector('.spinner');
if (spinner) {
spinner.parentElement.removeChild(spinner);
}
}
function displayWeather(){
let place = input.value;
console.log(place);
let api="https://api.openweathermap.org/data/2.5/find?q="+ place +"&units=metric&appid=850cac4c51c8a61b959dfe0593e8cb07";
console.log(api);
renderLoader();
fetch(api)
.then(response=>{
data=response.json();
return data;
})
.then(data=>{
console.log(data);
clearLoader();
//access data from the fetched object
let temp=data.list[0].main.feels_like;
let weather=data.list[0].weather[0].description;
let windSpeed=data.list[0].wind.speed;
let maxTemp=data.list[0].main.temp_max;
let minTemp=data.list[0].main.temp_min;
let humidity=data.list[0].main.humidity;
let pressure=data.list[0].main.pressure;
//display weather
document.querySelector('.temp').textContent=temp;
document.querySelector('.weather').textContent=weather;
document.querySelector('.wind-speed').textContent=windSpeed + " kph";
document.querySelector('.max-temp').textContent=maxTemp;
document.querySelector('.min-temp').textContent=minTemp ;
document.querySelector('.humidity').textContent=humidity + " %";
document.querySelector('.pressure').textContent=pressure + " Pa";
document.querySelector('.place').textContent=place;
//change icon for different weathers
if( weather=='moderate rain')
weatherIcon.setAttribute('src','icons/moderate.png');
else if(weather=='haze')
weatherIcon.setAttribute('src','icons/haze.png');
else if(weather=='scattered clouds')
weatherIcon.setAttribute('src','icons/scattered.png');
else if(weather=='light rain')
weatherIcon.setAttribute('src','icons/light.png');
else if(weather=='overcast clouds')
weatherIcon.setAttribute('src','icons/overcast.png');
})
.catch(error=>{
console.log(error);
alert('please enter a valid place.');
})
//daily forecast
let forecastapi="https://cors-anywhere.herokuapp.com/https://api.openweathermap.org/data/2.5/forecast/daily/find?q="+ place +"&units=metric&appid=4b2ab8f040bb8dd2f8d80e58c3e29cb3";
fetch(forecastapi)
.then(response=>{
forecastdata=response.json();
return forecastdata;
})
.then(forecastdata=>{
console.log(forecastdata);})
}
let weatherIcon=document.querySelector('.weather-icon');
//add today's date
let date=document.querySelector('.date');
let today=new Date();
const options={year: 'numeric', month: 'long', day: 'numeric' }
date.textContent=today.toLocaleDateString('en-US',options);
//daily forcast