-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
179 lines (137 loc) · 6.45 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Salutation
let salutation = document.getElementById("displayGreeting");
if (new Date().getHours() < 12) {
salutation.innerHTML = "Good Morning 😊"
}
else if (new Date().getHours() < 16) {
salutation.innerHTML = "Good Afternoon 🌞"
}
else {
salutation.innerHTML = "Good Evening 🌙"
};
// Weather & API
const apiKey = "b2c5830f9322f0830a6ef1df22547ed0"; // Do not use my api key
const apiURL = "https://api.openweathermap.org/data/2.5/weather?";
let form = document.querySelector("form");
let inputLocation = document.getElementById("inputLocation");
let searchIcon = document.querySelector(".fa-search");
async function getWeatherInfo() {
try {
const searchWeather = await fetch(apiURL + `&appid=${apiKey}` + `&q=` + inputLocation.value + `&units=metric`); //Parameters for the api
const weatherData = await searchWeather.json();
document.getElementById("cityName").innerHTML = weatherData.name + ","
document.getElementById("country").innerHTML = weatherData.sys.country
document.getElementById("flag").src = "https://flagsapi.com/"+weatherData.sys.country+"/flat/32.png"
document.getElementById("temperature").innerHTML = weatherData.main.temp
document.getElementById("min-temp").innerHTML = weatherData.main.temp_min
document.getElementById("max-temp").innerHTML = weatherData.main.temp_max
document.querySelector(".description").innerHTML = weatherData.weather[0].description
document.querySelector(".feels-like").innerHTML = weatherData.main.feels_like
// Weather Date Format Start
let daysOfTheWeek; switch (new Date().getDay()) {
case 0:
daysOfTheWeek = "Sunday,";
break;
case 1:
daysOfTheWeek = "Monday,";
break;
case 2:
daysOfTheWeek = "Tuesday,";
break;
case 3:
daysOfTheWeek = "Wednesday,";
break;
case 4:
daysOfTheWeek = "Thursday,";
break;
case 5:
daysOfTheWeek = "Friday,";
break;
case 6:
daysOfTheWeek = "Saturday,";
break;
};
let month; switch(new Date().getMonth()) {
case 0:
month = "January"
break;
case 1:
month = "February"
break;
case 2:
month = "March"
break;
case 3:
month = "April"
break;
case 4:
month = "May"
break;
case 5:
month = "June"
break;
case 6:
month = "July"
break;
case 7:
month = "August"
break;
case 8:
month = "September"
break;
case 9:
month = "October"
break;
case 10:
month = "November"
break;
case 11:
month = "December"
break;
}
let date = new Date().getDate()
let year = new Date().getFullYear()
document.getElementById("date").innerHTML = daysOfTheWeek + " " + month + " " + date + "," + " " + year
//Weather Date Format End
document.getElementById("cloud-img").src = "https://openweathermap.org/img/wn/"+weatherData.weather[0].icon+"@4x.png"
document.getElementById("humidity").innerHTML = weatherData.main.humidity + "%"
document.getElementById("pressure").innerHTML = weatherData.main.pressure + "hPa"
document.getElementById("wind-speed").innerHTML = weatherData.wind.speed + "Km/hr"
document.getElementById("cloud").innerHTML = weatherData.clouds.all + "%"
inputLocation.value = ""; // Clears the input tag for another text
}
catch {
const htmlTag = document.querySelector("html")
const errorElement = document.createElement("div")
htmlTag.appendChild(errorElement)
htmlTag.style.backgroundColor="black"
errorElement.style.color="#87CEEB"
errorElement.classList.add("container-fluid")
errorElement.innerHTML = `
<div class="js-container-element container d-flex-column justify-content-center align-content-center">
<div class="d-flex justify-content-center mb-3">
<img src="Images/logo.png" alt="Img" class="img-fluid" style="width: 50px; height: 50px;">
</div>
<p class="text-center">Something went wrong</p>
<p class="text-center">Refresh the page</p>
</div>
`
document.querySelector("body").style.display="none" // removes the body tag in html
} //Error message to run incase of any issue
}
form.addEventListener("submit",
(event) => {
event.preventDefault()
getWeatherInfo()
}
); // Calling 1 via enter key
searchIcon.addEventListener("click",
() => {getWeatherInfo()}
); // Calling 2 via font-awesome search icon
// Default Weather Info
function defaultWeatherInfo() {
inputLocation.value = "London";
getWeatherInfo()
inputLocation.value = ""; // Clears the default text (london) from the input tag
}
defaultWeatherInfo()