-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
98 lines (79 loc) · 2.97 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
import getWeatherData from "./utils/httpReq.js";
import { removeModal, showModal } from "./utils/modal.js";
import { getWeekDay } from "./utils/customeDate.js";
const searchInput = document.querySelector("input");
const searchButton = document.querySelector("button");
const weatherContainer = document.getElementById("weather");
const forecastContainer = document.getElementById("forecast");
const locationIcon = document.getElementById("location-icon");
const modalButton = document.getElementById("modal-button");
const renderCurrentWeather = (data) => {
if (!data) return;
const weatherJSX = `
<h1>${data.name}, ${data.sys.country}</h1>
<div id="main">
<img alt="weather icon" src="https://openweathermap.org/img/w/${data.weather[0].icon}.png" />
<span>${data.weather[0].description}</span>
<p>${Math.round(data.main.temp)}°C</p>
</div>
<div id="info">
<p>Humidity: <span>${data.main.humidity} %</span></p>
<p>WindSpeed: <span>${data.wind.speed} m/s</span></p>
</div>
`;
weatherContainer.innerHTML = weatherJSX;
};
const renderForecastWeather = (data) => {
if (!data) return;
forecastContainer.innerHTML = "";
data = data.list.filter((obj) => obj.dt_txt.endsWith("12:00:00"));
data.forEach((i) => {
const forecastJSX = `
<div>
<img alt="weather icon" src="https://openweathermap.org/img/w/${i.weather[0].icon}.png" />
<h3>${getWeekDay(i.dt)}</h3>
<p>${Math.round(i.main.temp)}°C</p>
<span>${i.weather[0].main}</span>
</div>
`;
forecastContainer.innerHTML += forecastJSX;
});
};
const searchHandler = async () => {
const cityName = searchInput.value;
if (!cityName) {
showModal("Please enter city name");
return;
}
const currentData = await getWeatherData("current", cityName);
renderCurrentWeather(currentData);
const forecastData = await getWeatherData("forecast", cityName);
renderForecastWeather(forecastData);
searchInput.value = "";
};
const positionCallback = async (position) => {
const currentData = await getWeatherData("current", position.coords);
renderCurrentWeather(currentData);
const forecastData = await getWeatherData("forecast", position.coords);
renderForecastWeather(forecastData);
};
const errorCallback = (error) => {
showModal(error.message);
};
const locationHandler = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(positionCallback, errorCallback);
} else {
showModal("Geolocation is not supported by this browser.");
}
};
const initHandler = async () => {
const currentData = await getWeatherData("current", "qorveh");
renderCurrentWeather(currentData);
const forecastData = await getWeatherData("forecast", "qorveh");
renderForecastWeather(forecastData);
};
searchButton.addEventListener("click", searchHandler);
locationIcon.addEventListener("click", locationHandler);
modalButton.addEventListener("click", removeModal);
document.addEventListener("DOMContentLoaded", initHandler);