-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
88 lines (64 loc) · 2.91 KB
/
Copy pathscript.js
File metadata and controls
88 lines (64 loc) · 2.91 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
const form = document.querySelector("form");
const cidadeInput = document.getElementById("input-city");
const city = document.getElementById("city");
const weather = document.getElementById("weather");
const weatherToday = document.getElementById("weather-today");
const monday = document.getElementById("weather-monday");
const tuesday = document.getElementById("weather-tuesday");
const wednesday = document.getElementById("weather-wednesday");
const thursday = document.getElementById("weather-thursday");
const friday = document.getElementById("weather-friday");
const forecastSection = document.getElementById("forecast-section");
form.addEventListener("submit", async function (event) {
event.preventDefault();
const nomeCidade = cidadeInput.value.trim();
if (!nomeCidade) {
city.innerText = "Digite uma cidade";
forecastSection.classList.add("hidden");
return;
}
try {
// Esconde enquanto carrega
forecastSection.classList.add("hidden");
weather.innerText = "Carregando...";
// 1️⃣ Buscar latitude e longitude
const geoUrl = `https://geocoding-api.open-meteo.com/v1/search?name=${nomeCidade}&count=1&language=pt&format=json`;
const geoResponse = await fetch(geoUrl);
if (!geoResponse.ok) {
throw new Error("Erro ao buscar localização");
}
const geoData = await geoResponse.json();
if (!geoData.results || geoData.results.length === 0) {
city.innerText = "Cidade não encontrada";
weather.innerText = "";
return;
}
const latitude = geoData.results[0].latitude;
const longitude = geoData.results[0].longitude;
city.innerText = geoData.results[0].name;
// 2️⃣ Buscar previsão diária
const climaUrl =
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&daily=temperature_2m_max&timezone=auto`;
const climaResponse = await fetch(climaUrl);
if (!climaResponse.ok) {
throw new Error("Erro ao buscar clima");
}
const climaData = await climaResponse.json();
const temperaturas = climaData.daily.temperature_2m_max;
// 3️⃣ Preencher dados
weatherToday.innerText = `${temperaturas[0]}°C`;
monday.innerText = `${temperaturas[1]}°C`;
tuesday.innerText = `${temperaturas[2]}°C`;
wednesday.innerText = `${temperaturas[3]}°C`;
thursday.innerText = `${temperaturas[4]}°C`;
friday.innerText = `${temperaturas[5]}°C`;
weather.innerText = "Previsão máxima para os próximos dias";
// 🔥 MOSTRA A SECTION AGORA
forecastSection.classList.remove("hidden");
} catch (error) {
console.error(error);
city.innerText = "Erro ao buscar dados";
weather.innerText = "";
forecastSection.classList.add("hidden");
}
});