diff --git a/index.html b/index.html index f4afbf5..c263563 100644 --- a/index.html +++ b/index.html @@ -67,18 +67,18 @@

Where in the world?

/> + diff --git a/js/common.js b/js/common.js new file mode 100644 index 0000000..e69de29 diff --git a/js/details.js b/js/details.js new file mode 100644 index 0000000..fd8f2f1 --- /dev/null +++ b/js/details.js @@ -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) => { + fetch('./CountriesData.json') + .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'; + + detailsContainer.innerHTML = ` +

${country.name}

+
+ ${country.name} Flag +
+ + `; +}; diff --git a/js/index.js b/js/index.js new file mode 100644 index 0000000..7cde8bf --- /dev/null +++ b/js/index.js @@ -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 = ` + +
+ ${country.name} Flag +
+
+

${country.name}

+ +
+
+ `; + countriesGrid.innerHTML += countryCard; + }); +} +const showRegions = () => { + const dropdownWrapper = document.querySelector('.dropdown-wrapper'); + + if (dropdownWrapper.classList.contains('open')) { + dropdownWrapper.classList.remove('open'); + } + 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) => { + 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); + }); +} + + +