-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (114 loc) · 3.99 KB
/
script.js
File metadata and controls
133 lines (114 loc) · 3.99 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
126
127
128
129
130
131
132
133
const api = {
key: "",
base: "https://api.openweathermap.org/data/2.5/",
lang: "pt_br",
units: "metric"
}
const city = document.querySelector('.city');
const date = document.querySelector('#date');
const container_temp = document.querySelector('.container-temp');
const temperature = document.querySelector('.container-temp div');
const temp_unit = document.querySelector('.container-temp span');
const climate = document.querySelector('.temp');
const low_high = document.querySelector('#low-high');
const container_img = document.querySelector('.container-img');
const input = document.querySelector('.input');
const btn = document.querySelector('.btn');
window.addEventListener('load', () => {
//if ("geolocalização no navegador")
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(setPosition, showError);
}
else {
alert('navegador não suporta geolozalicação');
}
function setPosition(position) {
console.log(position)
let lat = position.coords.latitude;
let long = position.coords.longitude;
coordResults(lat, long);
}
function showError(error) {
alert(`erro: ${error.message}`);
}
})
function coordResults(lat, long) {
fetch(`${api.base}weather?lat=${lat}&lon=${long}&lang=${api.lang}&units=${api.units}&APPID=${api.key}`)
.then(response => {
if (!response.ok) {
throw new Error(`http error: status ${response.status}`)
}
return response.json();
})
.catch(error => {
alert(error.message)
})
.then(response => {
displayResults(response)
});
}
btn.addEventListener('click', function() {
searchResults(input.value)
})
input.addEventListener('keypress', enter)
function enter(event) {
key = event.keyCode
if (key === 13) {
searchResults(input.value)
}
}
function searchResults(city) {
fetch(`${api.base}weather?q=${city}&lang=${api.lang}&units=${api.units}&APPID=${api.key}`)
.then(response => {
if (!response.ok) {
throw new Error(`http error: status ${response.status}`)
}
return response.json();
})
.catch(error => {
alert(error.message)
})
.then(response => {
displayResults(response)
});
}
function displayResults(weather) {
console.log(weather)
city.innerText = `${weather.name}, ${weather.sys.country}`;
let now = new Date();
date.innerText = dateBuilder(now);
let iconName = weather.weather[0].icon;
container_img.innerHTML = `<img src="./icons/${iconName}.png">`;
let temperatureC = `${Math.round(weather.main.temp)}`;
temperature.innerHTML = temperatureC;
temp_unit.innerHTML = `°C`;
weather_tempo = weather.weather[0].description;
climate.innerText = capitalizeFirstLetter(weather_tempo);
low_high.innerText = `${Math.round(weather.main.temp_min)}°C / ${Math.round(weather.main.temp_max)}°C`;
}
function dateBuilder(d) {
let days = ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"];
let months = ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julio", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"];
let day = days[d.getDay()]; //getDay: 0-6
let date = d.getDate();
let month = months[d.getMonth()];
let year = d.getFullYear();
return `${day}, ${date} ${month} ${year}`;
}
container_temp.addEventListener('click', changeTemp)
function changeTemp() {
temp_number_now = temperature.innerHTML
if (temp_unit.innerHTML === "°C") {
let f = (temp_number_now * 1.8) + 32
temp_unit.innerHTML = "°F"
temperature.innerHTML = Math.round(f)
}
else {
let c = (temp_number_now - 32) / 1.8
temp_unit.innerHTML = "°C"
temperature.innerHTML = Math.round(c)
}
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}