-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetails.js
73 lines (65 loc) · 2.44 KB
/
details.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
import { fetchCountries } from "./api.js";
async function loadCountryDetails() {
const params = new URLSearchParams(window.location.search);
const countryName = params.get("country");
const countries = await fetchCountries();
const country = countries.find((c) => c.name.common === countryName);
if (country) {
displayCountryDetails(country);
} else {
document.getElementById("country-details-container").innerHTML =
"<p>Country not found.</p>";
}
}
function displayCountryDetails(country) {
const container = document.getElementById("country-details-container");
container.innerHTML = `
<div class="country-details-card">
<img src="${country.flags.svg}" alt="Flag of ${country.name.common}">
<div class="country-details-content">
<h2>${country.name.common}</h2>
<p><strong><i class="fas fa-city"></i> Capital:</strong> ${
country.capital
}</p>
<p><strong><i class="fas fa-globe"></i> Region:</strong> ${
country.region
}</p>
<p><strong><i class="fas fa-users"></i> Population:</strong> ${country.population.toLocaleString()}</p>
<p><strong><i class="fas fa-language"></i> Languages:</strong> ${Object.values(
country.languages
).join(", ")}</p>
<p><strong><i class="fas fa-money-bill-wave"></i> Currencies:</strong> ${Object.values(
country.currencies
)
.map((c) => c.name)
.join(", ")}</p>
<p><strong><i class="fas fa-map-marked-alt"></i> Subregion:</strong> ${
country.subregion
}</p>
<p><strong><i class="fas fa-clock"></i> Timezones:</strong> ${country.timezones.join(
", "
)}</p>
<p><strong><i class="fas fa-map"></i> Borders:</strong> ${
country.borders ? country.borders.join(", ") : "None"
}</p>
<p><strong><i class="fas fa-ruler-combined"></i> Area:</strong> ${country.area.toLocaleString()} km²</p>
</div>
</div>
`;
}
document.getElementById("back-button").addEventListener("click", () => {
window.location.href = "index.html";
});
document.getElementById("theme-toggle").addEventListener("click", () => {
const body = document.body;
const themeIcon = document.getElementById("theme-icon");
body.dataset.theme = body.dataset.theme === "dark" ? "light" : "dark";
if (body.dataset.theme === "dark") {
themeIcon.classList.remove("fa-sun");
themeIcon.classList.add("fa-moon");
} else {
themeIcon.classList.remove("fa-moon");
themeIcon.classList.add("fa-sun");
}
});
loadCountryDetails();