-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchweather.js
127 lines (98 loc) · 4.09 KB
/
fetchweather.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
import { closeAllLists } from './autocomplete.js'
const weatherApi = {
key: "5898d1f46544d841eac1352fba609ade",
baseUrl: "https://api.openweathermap.org/data/2.5/"
}
const userInput = document.getElementById("input-box");
let modalText = document.getElementById("message");
const modalPopup = document.getElementById("modal");
modalPopup.querySelector(".ok-btn").addEventListener("click", closePopup);
document.getElementById("search-btn").addEventListener("click", clickedSearch);
userInput.addEventListener("keypress", event => {
if (event.key === "Enter") {
fetchWeather(userInput.value);
userInput.value = "";
closeAllLists(document.getElementById("autocomplete-results"));
}
});
export function clickedSearch() {
fetchWeather(userInput.value);
userInput.value = "";
}
function fetchWeather(cityName) {
if (cityName === undefined || cityName.length === 0) {
modalText.innerText = "Please enter a city name.";
openPopup();
} else {
let url = `${weatherApi.baseUrl}weather?q=${cityName}&units=metric&appid=${weatherApi.key}`;
// userInput.value = "";
fetch(url)
.then((response => {
if (response.ok) {
return response.json();
} else {
throw Error(response.statusText);
}
}))
.then(weatherData)
.catch((error) => {
//If city name is NOT valid
console.log("Error:", error);
modalText.innerText = `City not found.`;
openPopup();
});
}
}
function weatherData(data) {
// console.log('Weather Data:', data);
if (data) {
document.getElementById("weather").style.visibility = "";
}
const city = document.querySelector(".location .city");
city.innerHTML = `${data.name}`;
const country = document.querySelector(".location .country");
country.innerHTML = `${data.sys.country}`;
const sunrise = document.querySelector(".sunrise p");
sunrise.innerHTML = convertTimestamp(data.sys.sunrise, data.timezone);
const sunset = document.querySelector(".sunset p");
sunset.innerHTML = convertTimestamp(data.sys.sunset, data.timezone);
const todaysDate = document.querySelector(".date");
todaysDate.innerHTML = dateCalc();
const weatherDescription = document.querySelector(".weather-description p");
const text = data.weather[0].description;
const upperCaseText = text[0].toUpperCase() + text.slice(1).toLowerCase();
weatherDescription.innerHTML = `${upperCaseText}`;
const tempElement = document.querySelector(".temp");
const temp = Math.floor(data.main.temp);
tempElement.innerHTML = `${temp}℃`;
document.getElementById("weatherIcon").src = `http://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png`;
const tempMin = document.querySelector(".min-temp");
const tempLowest = Math.floor(data.main.temp_min);
tempMin.innerHTML = `${tempLowest}℃`;
const tempMax = document.querySelector(".max-temp");
const tempHighest = Math.floor(data.main.temp_max);
tempMax.innerHTML = `${tempHighest}℃`;
const feelsLike = document.querySelector(".feels-like p");
const floorTemp = Math.floor(data.main.feels_like);
feelsLike.innerHTML = `${floorTemp}℃`;
}
function dateCalc() {
const today = new Date();
// console.log(today);
const weekday = today.toLocaleDateString("default", { weekday: "long" });;
const date = today.getDate();
const month = today.toLocaleString("default", { month: "long" });
const year = today.getFullYear();
return `${weekday}, ${date} ${month} ${year} `;
}
function convertTimestamp(unixTimestamp, timezone) {
const sunSetRise = new Date((unixTimestamp * 1000 + timezone * 1000));
const formattedTime = sunSetRise.toLocaleTimeString("en-GB", { timeZone: "UTC", hour: "2-digit", minute: "2-digit" });
return formattedTime;
}
function openPopup() {
modalPopup.classList.add("open-popup");
}
function closePopup() {
modalPopup.classList.remove("open-popup");
}