-
Notifications
You must be signed in to change notification settings - Fork 28
added html and js #7
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,44 @@ | ||
| document.addEventListener("DOMContentLoaded", () => { | ||
| const countryName = new URLSearchParams(window.location.search).get('country'); | ||
| if (countryName) { | ||
| fetchCountryData(countryName); | ||
| } | ||
| else { | ||
| console.error('Country name not found'); | ||
| } | ||
| }); | ||
|
|
||
| const fetchCountryData = (countryName) => { | ||
|
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. Display a loader before fetching country data |
||
| fetch('./CountriesData.json') | ||
|
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. Avoid calling fetch repeatedly for the same data (e.g., when filtering regions). Cache the data once: |
||
| .then(response => response.json()) | ||
| .then(countries => { | ||
| const country = countries.find((c) => c.name.toLowerCase() === countryName.toLowerCase()); | ||
| if (country) { | ||
| displayCountryDetails(country); | ||
| } | ||
| else { | ||
| console.error('Country not found'); | ||
| } | ||
| }) | ||
| .catch(error => { | ||
| console.error('Error fetching country data:', error); | ||
| }); | ||
| }; | ||
|
|
||
| const displayCountryDetails = (country) => { | ||
| const detailsContainer = document.querySelector('.country-details'); | ||
| const loader = document.querySelector('.loader'); | ||
| loader.style.display = 'none'; | ||
|
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. Hide loader after data is fetched |
||
|
|
||
| detailsContainer.innerHTML = ` | ||
|
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. Ensure the use of backticks (``) for the innerHTML content |
||
| <h2>${country.name}</h2> | ||
| <div class="country-flag"> | ||
| <img src="${country.flag}" alt="${country.name} Flag"> | ||
| </div> | ||
| <ul> | ||
| <li><strong>Capital:</strong> ${country.capital || 'N/A'}</li> | ||
| <li><strong>Population:</strong> ${country.population.toLocaleString()}</li> | ||
| <li><strong>Region:</strong> ${country.region}</li> | ||
| </ul> | ||
| `; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| fetch('./CountriesData.json') | ||
| .then((response) => { | ||
| if (!response.ok) { | ||
| throw new Error('Network response - not ok'); | ||
| } | ||
| return response.json(); | ||
| }) | ||
| .then((data) => { | ||
| displayCountries(data); | ||
| }) | ||
| .catch((error) => { | ||
| console.error('Error fetching countries data:', error); | ||
| }); | ||
|
|
||
| const displayCountries = (countries) => { | ||
| const countriesGrid = document.querySelector('.countries-grid'); | ||
| countriesGrid.innerHTML = ''; | ||
|
|
||
| // Loop through each country and generate its card | ||
| countries.forEach((country) => { | ||
| const countryCard = ` | ||
| <a href="details.html?country=${encodeURIComponent(country.name)}" class="country scale-effect"> | ||
| <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.toLocaleString()}</li> | ||
| <li><strong>Region:</strong> ${country.region}</li> | ||
| <li><strong>Capital:</strong> ${country.capital || 'N/A'}</li> | ||
| </ul> | ||
| </div> | ||
| </a> | ||
| `; | ||
| countriesGrid.innerHTML += countryCard; | ||
| }); | ||
| } | ||
| const showRegions = () => { | ||
| const dropdownWrapper = document.querySelector('.dropdown-wrapper'); | ||
|
|
||
| if (dropdownWrapper.classList.contains('open')) { | ||
| dropdownWrapper.classList.remove('open'); | ||
|
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. Close dropdown after selection |
||
| } | ||
| else { | ||
| dropdownWrapper.classList.add('open'); | ||
| } | ||
| } | ||
|
|
||
| const chosenRegion = (element) => { | ||
| const region = element.getAttribute('data-region'); | ||
| filterByRegion(region); | ||
| const dropdownWrapper = document.querySelector('.dropdown-wrapper'); | ||
| dropdownWrapper.classList.remove('open'); | ||
| } | ||
|
|
||
| const filterByRegion = (region) => { | ||
|
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. Consider displaying a loader while filtering data by region. This ensures the user is aware that a background process is ongoing. |
||
| let filteredCountries; | ||
| fetch('./CountriesData.json') | ||
| .then((response) => response.json()) | ||
| .then((countries) => { | ||
| if (region === 'all') { | ||
| filteredCountries = countries; | ||
| } | ||
| else { | ||
| filteredCountries = countries.filter((country) => country.region.toLowerCase() === region); | ||
| } | ||
| displayCountries(filteredCountries); | ||
| }) | ||
| .catch((error) => { | ||
| console.error('Error filtering countries:', error); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
|
|
||
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.
Provide feedback to user if country name is not found