-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
125 lines (115 loc) · 4.55 KB
/
Copy pathmain.js
File metadata and controls
125 lines (115 loc) · 4.55 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
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
let weatherApp = new Vue({
el: "#app",
data: {
name: "",
currentTemp: "",
minTemp: "",
maxTemp: "",
sunrise: "",
sunset: "",
pressure: "",
humidity: "",
wind: "",
overcast: "",
locationFound: "",
errorMsg: "ERROR! NO GEOLOCATION IN YOUR BROWSER!",
cityMsg: "Сменить город",
},
methods: {
getWeather() {
const API_KEY = "699be447d91ec1be2f59446a75e073ff";
const img = document.querySelector(".weather-overcast__image");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
lon = position.coords.longitude;
lat = position.coords.latitude;
fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&units=metric&lang=ru&appid=${API_KEY}`,
)
.then((res) => {
return res.json();
})
.then((res) => {
this.name = res.name;
this.currentTemp = `${res.main.temp} °C`;
this.minTemp = `Min ${res.main.temp_min} °C`;
this.maxTemp = `Max ${res.main.temp_max} °C`;
this.pressure = `${res.main.pressure} мм рт. ст.`;
this.humidity = `${res.main.humidity} %`;
this.wind = `${res.wind.speed} м/с`;
this.overcast = res.weather[0].description;
this.sunrise = new Date(res.sys.sunrise * 1000).toLocaleTimeString("en-GB").slice(0, 5);
this.sunset = new Date(res.sys.sunset * 1000).toLocaleTimeString("en-GB").slice(0, 5);
img.src = `http://openweathermap.org/img/wn/${res.weather[0].icon}.png`;
const appContainer = document.querySelector(".app__container");
appContainer.style.display = "flex";
});
}),
(error) => {
this.locationFound = `ERROR(${error.code}): ${error.message}`;
console.warn(`ERROR(${error.code}): ${error.message}`);
};
} else {
alert(this.errorMsg);
console.error(new Error(this.errorMsg));
}
},
},
mounted() {
const getWeatherByCity = async (e) => {
try {
e.preventDefault();
const API_KEY = "699be447d91ec1be2f59446a75e073ff";
const city = document.querySelector(".form__city").value;
const img = document.querySelector(".weather-overcast__image");
if (city !== "") {
const weatherUrl = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${city}&lang=ru&appid=${API_KEY}&units=metric`,
);
const errorModal = document.querySelector("#modal");
if (weatherUrl.status === 404) {
errorModal.classList.remove("hidden");
} else {
errorModal.classList.add("hidden");
}
const data = await weatherUrl.json();
if (data.cod !== "404") {
this.name = data.name;
this.currentTemp = `${data.main.temp} °C`;
this.minTemp = `Min ${data.main.temp_min} °C`;
this.maxTemp = `Max ${data.main.temp_max} °C`;
this.pressure = `${data.main.pressure} мм рт. ст.`;
this.humidity = `${data.main.humidity} %`;
this.wind = `${data.wind.speed} м/с`;
this.overcast = data.weather[0].description;
this.sunrise = new Date(data.sys.sunrise * 1000).toLocaleTimeString("en-GB").slice(0, 5);
this.sunset = new Date(data.sys.sunset * 1000).toLocaleTimeString("en-GB").slice(0, 5);
img.src = `http://openweathermap.org/img/wn/${data.weather[0].icon}.png`;
}
}
} catch (err) {
console.log("error: ", err);
}
};
const btn = document.querySelector(".form__btn");
btn.addEventListener("click", getWeatherByCity);
const cityInput = document.querySelector(".form__city");
const form = document.querySelector(".form");
cityInput.addEventListener("input", function () {
if (cityInput.value == "") {
btn.classList.add("hidden");
cityInput.classList.add("input-error");
form.insertAdjacentHTML("beforebegin", '<div class="locationError">Введите название города</div>');
} else {
btn.classList.remove("hidden");
cityInput.classList.remove("input-error");
cityInput.classList.add("input-ok");
const errorInputMsg = document.querySelector(".locationError");
if (errorInputMsg) {
errorInputMsg.remove();
}
}
});
this.getWeather();
},
});