-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
156 lines (134 loc) · 5.47 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
//making object of weatherapi
const weatherApi = {
key: '9f23b56e8dcad8299bf4e5a2a3fc932b',
baseUrl: 'https://api.openweathermap.org/data/2.5/weather'
}
//anonymous function
//adding event listener key press of enter
let searchInputBox = document.getElementById('input-box');
searchInputBox.addEventListener('keypress', (event) => {
if (event.keyCode == 13) {
// console.log(searchInputBox.value);
getWeatherReport(searchInputBox.value);
}
})
//get waether report
function getWeatherReport(city) {
fetch(`${weatherApi.baseUrl}?q=${city}&appid=${weatherApi.key}&units=metric`) // fetch method fetching the data from base url ...metric is used for unit in celcius......here i am appending the base url to get data by city name .
.then(weather => { //weather is from api
return weather.json(); // return data from api in JSON
}).then(showWeaterReport); // calling showweatherreport function
}
//show weather report
function showWeaterReport(weather) {
let city_code=weather.cod;
if(city_code==='400'){
swal("Empty Input", "Please enter any city", "error");
reset();
}else if(city_code==='404'){
swal("Bad Input", "entered city didn't matched", "warning");
reset();
}
else{
// console.log(weather.cod);
// console.log(weather);
let op = document.getElementById('weather-body');
op.style.display = 'block';
let todayDate = new Date();
let parent=document.getElementById('parent');
let weather_body = document.getElementById('weather-body');
weather_body.innerHTML =
`
<div class="location-deatils">
<div class="city" id="city">${weather.name}, ${weather.sys.country}</div>
<div class="date" id="date"> ${dateManage(todayDate)}</div>
</div>
<div class="weather-status">
<div class="temp" id="temp">${Math.round(weather.main.temp)}°C </div>
<div class="weather" id="weather"> ${weather.weather[0].main} <i class="${getIconClass(weather.weather[0].main)}"></i> </div>
<div class="min-max" id="min-max">${Math.floor(weather.main.temp_min)}°C (min) / ${Math.ceil(weather.main.temp_max)}°C (max) </div>
<div id="updated_on">Updated as of ${getTime(todayDate)}</div>
</div>
<hr>
<div class="day-details">
<div class="basic">Feels like ${weather.main.feels_like}°C | Humidity ${weather.main.humidity}% <br> Pressure ${weather.main.pressure} mb | Wind ${weather.wind.speed} KMPH</div>
</div>
`;
parent.append(weather_body);
changeBg(weather.weather[0].main);
reset();
}
}
//making a function for the last update current time
function getTime(todayDate) {
let hour =addZero(todayDate.getHours());
let minute =addZero(todayDate.getMinutes());
return `${hour}:${minute}`;
}
//date manage for return current date
function dateManage(dateArg) {
let days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
let months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
let year = dateArg.getFullYear();
let month = months[dateArg.getMonth()];
let date = dateArg.getDate();
let day = days[dateArg.getDay()];
// console.log(year+" "+date+" "+day+" "+month);
return `${date} ${month} (${day}) , ${year}`
}
// function for the dynamic background change according to weather status
function changeBg(status) {
if (status === 'Clouds') {
document.body.style.backgroundImage = 'url(img/clouds.jpg)';
} else if (status === 'Rain') {
document.body.style.backgroundImage = 'url(img/rainy.jpg)';
} else if (status === 'Clear') {
document.body.style.backgroundImage = 'url(img/clear.jpg)';
}
else if (status === 'Snow') {
document.body.style.backgroundImage = 'url(img/snow.jpg)';
}
else if (status === 'Sunny') {
document.body.style.backgroundImage = 'url(img/sunny.jpg)';
} else if (status === 'Thunderstorm') {
document.body.style.backgroundImage = 'url(img/thunderstrom.jpg)';
} else if (status === 'Drizzle') {
document.body.style.backgroundImage = 'url(img/drizzle.jpg)';
} else if (status === 'Mist' || status === 'Haze' || status === 'Fog') {
document.body.style.backgroundImage = 'url(img/mist.jpg)';
}
else {
document.body.style.backgroundImage = 'url(img/bg.jpg)';
}
}
//making a function for the classname of icon
function getIconClass(classarg) {
if (classarg === 'Rain') {
return 'fas fa-cloud-showers-heavy';
} else if (classarg === 'Clouds') {
return 'fas fa-cloud';
} else if (classarg === 'Clear') {
return 'fas fa-cloud-sun';
} else if (classarg === 'Snow') {
return 'fas fa-snowman';
} else if (classarg === 'Sunny') {
return 'fas fa-sun';
} else if (classarg === 'Mist') {
return 'fas fa-smog';
} else if (classarg === 'Thunderstorm' || classarg === 'Drizzle') {
return 'fas fa-thunderstorm';
} else {
return 'fas fa-cloud-sun';
}
}
function reset() {
let input = document.getElementById('input-box');
input.value = "";
}
// funtion to add zero if hour and minute less than 10
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}