-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetails.js
More file actions
175 lines (153 loc) · 5.7 KB
/
details.js
File metadata and controls
175 lines (153 loc) · 5.7 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
window.onload= ()=> {
let countryName = getUrlParameter()
fetchCountry(countryName)
}
function getUrlParameter() {
let params = (new URL(document.location)).searchParams;
const countryName = params.get('name');
const divheader = document.getElementById("countryname-div")
const header = document.getElementById("countryname");
header.innerHTML = countryName;
divheader.appendChild (header);
return countryName
}
const fetchCountry = (countryName) => {
console.log('countryName :>> ', countryName);
fetch(`https://restcountries.com/v3.1/name/${countryName}?fullText=true`)
.then((res) => res.json())
.then((res) => {
const fetchedCountry = res[0];
armarTabla(fetchedCountry)
addEvents();
})
.catch((err) => console.log(err));
}
const armarTabla = (country) => {
const title = document.getElementById("title");
const divheader = document.getElementById("countryname-div")
const header = document.getElementById("countryname");
header.setAttribute("text-align", "center")
const imgdiv = document.getElementById("flag-div")
const flagImg = document.createElement("img");
const escdiv = document.getElementById("escudo-div")
const escImg = document.createElement("img");
const table = document.getElementById("details-table");
const row = document.createElement("tr");
row.id = "datarow"
title.innerHTML = (`Weather for ${country.name.common}`)
header.innerHTML = country.name.official;
flagImg.setAttribute("src", country.flags.png);
flagImg.setAttribute("alt", country.flags.alt);
escImg.setAttribute("src", country.coatOfArms.png);
escImg.setAttribute("alt", country.coatOfArms.alt);
const capital = document.createElement("td");
capital.innerHTML = country.capital;
const language = document.createElement("td");
language.innerHTML = country.demonyms.eng.m;
const population = document.createElement("td");
population.innerHTML = country.population;
const timezone = document.createElement("td");
timezone.innerHTML = country.timezones;
const region = document.createElement("td");
region.innerHTML = country.region;
const subregion = document.createElement("td");
subregion.innerHTML = country.subregion;
table.appendChild(row);
divheader.appendChild(header);
imgdiv.append(flagImg);
escdiv.append(escImg);
row.append(capital, language, population, timezone, region, subregion);
getWeatherByCity(capital);
}
const getWeatherByCity = (city) => {
// hide spinner
const spinner = document.getElementById("spinner");
spinner.classList.remove("invisible");
//show table
const containerData = document.getElementById("data-to-display");
containerData.classList.add("invisible");
setTimeout(() => {
const capital = city.innerText;
let urlForecast = `http://api.weatherapi.com/v1/forecast.json?key=${API_KEY}&q=${capital}&days=7&aqi=no&alerts=no`;
fetch(urlForecast).then((singleResponse) => {
return singleResponse.json();
}).then((result) => {
const weatherData = result;
displayData(weatherData);
});
}, 1000);
};
const displayData = (weatherData) => {
// hide spinner
const spinner = document.getElementById("spinner");
spinner.classList.add("invisible");
//show table
const containerData = document.getElementById("data-to-display");
containerData.classList.remove("invisible");
const city = document.getElementById("city");
const tbody = document.getElementById("weather-data");
const { forecast, location } = weatherData;
city.innerText = `Weather for ${location.name} in ${location.country}, for the next week...`;
createTable(tbody, forecast);
};
const createTable = (tbody, forecast) => {
forecast.forecastday.forEach((day) => {
const row = document.createElement("tr");
row.id = "weatherrow"
const td1 = document.createElement("td");
const td2 = document.createElement("td");
const td3 = document.createElement("td");
const td4 = document.createElement("td");
const td5 = document.createElement("td");
const td6 = document.createElement("td");
const td7 = document.createElement("td");
const td8 = document.createElement("td");
const td9 = document.createElement("td");
const td10 = document.createElement("img");
td1.innerText = day.date;
td2.innerText = `${day.day.mintemp_c} °C`;
td3.innerText = `${day.day.maxtemp_c} °C`;
td4.innerText = `${day.day.avghumidity} %`;
td5.innerText =`${day.day.maxwind_kph} kph`;
td6.innerText = day.astro.sunrise;
td7.innerText = day.astro.sunset;
td8.innerText = day.astro.moonrise;
td9.innerText = day.astro.moonset;
td10.setAttribute("src", day.day.condition.icon)
row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td3);
row.appendChild(td4);
row.appendChild(td5);
row.appendChild(td6);
row.appendChild(td7);
row.appendChild(td8);
row.appendChild(td9);
row.appendChild(td10);
tbody.appendChild(row);
});
};
const cleanDOM = () => {
document.getElementById("buscador").value = "";
document.querySelectorAll("img").innerHTML = "";
document.getElementById("datarow").innerText = "";
document.getElementById("flag-div").innerText = "";
document.getElementById("escudo-div").innerText = "";
document.getElementById("weather-data").innerText = "";
document.getElementById("city").innerText = "";
};
//BUSCAR A TRAPES DEL INPUT TEXT
const addEvents = () => {
let country = "";
const searchInput = document.getElementById("buscador");
searchInput.addEventListener("change", (event) => {
country = event.target.value;
});
searchInput.addEventListener("keydown", (event) => {
event.preventDefault()
if (event.key === "Enter") {
cleanDOM();
fetchCountry(country);
}
});
};