-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.js
More file actions
81 lines (66 loc) · 3.48 KB
/
weather.js
File metadata and controls
81 lines (66 loc) · 3.48 KB
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
require('dotenv').config();
document.getElementById("getWeatherBtn").addEventListener('click',() => {
//getting the location value from cityInput
const location = document.getElementById("cityInput").value;
//if location is empty, we return from here and ask to enter the city name
if(location === "") {
const empty = document.getElementById("weatherResult");
empty.innerHTML = `
<h4 style="color:red;">Please enter the city name</h4>
`;
return;
}
//checking the response
//if any other response code other than 200 we return error
const checkResponse = ((response) => {
const resp_code = response.status;
return response.json().then(body => {
if(resp_code === 200) {
return body;
} else {
//throw new Error(`Error ${body.error.code}: ${body.error.message}`);
throw new Error(`Error: ${body.error.message}`);
}
});
});
//if the response seems to be valid, we fetch the weather details
const getWeatherInfo = ((data) => {
const condition_icon_url = "https:"+data.current.condition.icon;
const condition_icon_text = data.current.condition.text;
const result = document.getElementById("weatherResult");
// result.innerText = Math.round(data.current.temp_c) + "° C" + "\n" +
// Math.round(data.current.temp_f) + "° F" + "\n" +
// "Condition: " + data.current.condition.text + "\n" +
// "Name: " + data.location.name + "\n" +
// "Region: " + data.location.region + "\n" +
// "Country: " + data.location.country + "\n" +
// "LocalTime: " + data.location.localtime ;
result.innerHTML = `
<h3> ${data.location.name} </h3>
<h4> ${Math.round(data.current.temp_c)}° C </h4>
<h4> ${Math.round(data.current.temp_f)}° F </h4>
<h4> ${data.current.condition.text} </h4>
<img src="${condition_icon_url}" alt="${condition_icon_text}" style="width: 90px; height: 90px; vertical-align: middle; margin-top: 5px"><br>
Region: ${data.location.region}<br>
Country: ${data.location.country}<br>
LocalTime: ${data.location.localtime}
`;
});
//if any error is thrown from the above methods, it will be caught here
const getError = ((error) => {
console.error('Error:', error.message);
const show_error = document.getElementById("weatherResult");
//show_error.innerText = error.message;
show_error.innerHTML = `<h4 style="color:red;">${error.message}</h4><br>
<h4 style="color:red;">Please enter a valid name of the city.</h4>
`;
})
const API_KEY = process.env.WEATHER_API_KEY ;
//fetching the data and using input location in the api request
const obj = fetch(`https://api.weatherapi.com/v1/current.json?key=${API_KEY}&q=${location}&aqi=yes`);
//using the object with promises
obj
.then(response=>checkResponse(response))
.then(data=>getWeatherInfo(data))
.catch(error=>getError(error));
})