-
Notifications
You must be signed in to change notification settings - Fork 28
Fetching from JSON and Filtering #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"); | ||
|
|
||
| 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"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use |
||
| 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(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| 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(); | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
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