Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 3 additions & 66 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,73 +86,10 @@ <h1>Where in the world?</h1>
</section>
<!-- Countries -->
<main class="main">
<div class="container">
<section class="countries-grid">
<a
href="#"
class="country scale-effect"
data-country-name="Afghanistan"
>
<div class="country-flag">
<img
src="https://upload.wikimedia.org/wikipedia/commons/5/5c/Flag_of_the_Taliban.svg"
alt="Afghanistan FLag"
/>
</div>
<div class="country-info">
<h2 class="country-title">Afghanistan</h2>
<ul class="country-brief">
<li><strong>population: </strong>40218234</li>
<li><strong>Region: </strong>Asia</li>
<li><strong>capital: </strong>Kabul</li>
</ul>
</div>
</a>
<a
href="#"
class="country scale-effect"
data-country-name="Åland Islands"
>
<div class="country-flag">
<img src="https://flagcdn.com/ax.svg" alt="Åland Islands FLag" />
</div>
<div class="country-info">
<h2 class="country-title">Åland Islands</h2>
<ul class="country-brief">
<li><strong>population: </strong>28875</li>
<li><strong>Region: </strong>Europe</li>
<li><strong>capital: </strong>Mariehamn</li>
</ul>
</div>
</a>
<a href="#" class="country scale-effect" data-country-name="Aruba">
<div class="country-flag">
<img src="https://flagcdn.com/aw.svg" alt="Aruba FLag" />
</div>
<div class="country-info">
<h2 class="country-title">Aruba</h2>
<ul class="country-brief">
<li><strong>population: </strong>106766</li>
<li><strong>Region: </strong>Americas</li>
<li><strong>capital: </strong>Oranjestad</li>
</ul>
</div>
</a>
<a href="#" class="country scale-effect" data-country-name="Belize">
<div class="country-flag">
<img src="https://flagcdn.com/bz.svg" alt="Belize FLag" />
</div>
<div class="country-info">
<h2 class="country-title">Belize</h2>
<ul class="country-brief">
<li><strong>population: </strong>397621</li>
<li><strong>Region: </strong>Americas</li>
<li><strong>capital: </strong>Belmopan</li>
</ul>
</div>
</a>
</section>
<div class="container" id="container">
<section class="countries-grid" id="countries-grid"></section>
</div>
</main>
<script src="./scripts.js"></script>
</body>
</html>
81 changes: 81 additions & 0 deletions scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Load countries from JSON file, once completed use displayCountries to render them
const loadCountries = async () => {
const grid = document.getElementById("countries-grid");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please save your hardcoded strings into constants file

try {
const response = await fetch("./CountriesData.json");
const countries = await response.json();
displayCountries(countries);
} catch (error) {
console.error("Error loading countries:", error);
}
};

const displayCountries = (countries) => {
const grid = document.getElementById("countries-grid");
grid.innerHTML = ""; // Reset the grid

countries.forEach((country) => {
const countryElement = document.createElement("a");
countryElement.href = "#";
countryElement.className = "country scale-effect";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use innerHtmL, its a dangerous method.

countryElement.setAttribute("data-country-name", country.name);

countryElement.innerHTML = `
<div class="country-flag">
<img
src="${country.flag}"
alt="${country.name} Flag"
/>
</div>
<div class="country-info">
<h2 class="country-title">${country.name}</h2>
<ul class="country-brief">
<li><strong>Population: </strong>${country.population}</li>
<li><strong>Region: </strong>${country.region}</li>
<li><strong>Capital: </strong>${country.capital}</li>
</ul>
</div>
`;

// Append the country element to the grid
grid.appendChild(countryElement);
});
};

const filterCountries = async (region) => {
try {
const response = await fetch("./CountriesData.json");
const countries = await response.json();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should only do 1 thing and this is to filter the countries.
Extract the get data logic into separate function

const filteredCountries =
region === "all"
? countries
: countries.filter(
(country) => country.region.toLowerCase() === region
);
displayCountries(filteredCountries);
} catch (error) {
console.error("Error filtering countries:", error);
}
};

// Load countries when the DOM is fully loaded
document.addEventListener("DOMContentLoaded", () => {
loadCountries();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good practice to call dat fetching functions when the app first loads

const dropdownWrapper = document.querySelector(".dropdown-wrapper");
const dropdownHeader = document.querySelector(".dropdown-header");
const dropdownItems = document.querySelectorAll(".dropdown-body li");

dropdownHeader.addEventListener("click", () => {
dropdownWrapper.classList.toggle("open");
});

dropdownItems.forEach((item) => {
item.addEventListener("click", (event) => {
const region = event.target.getAttribute("data-region");
filterCountries(region);
dropdownWrapper.classList.remove("open"); // close the dropdown
});
});
});