From ee44b998c29054083fd83f9d285b7ca7cfd1b1fb Mon Sep 17 00:00:00 2001 From: emilyluskin Date: Sat, 14 Dec 2024 20:27:22 +0200 Subject: [PATCH 1/2] added html and js --- index.html | 1 + index.js | 0 2 files changed, 1 insertion(+) create mode 100644 index.js diff --git a/index.html b/index.html index f4afbf5..8611333 100644 --- a/index.html +++ b/index.html @@ -154,5 +154,6 @@

Belize

+ diff --git a/index.js b/index.js new file mode 100644 index 0000000..e69de29 From 7c640a7e207d2f008de23279942548be1177246a Mon Sep 17 00:00:00 2001 From: emilyluskin Date: Sat, 14 Dec 2024 23:41:20 +0200 Subject: [PATCH 2/2] added js files with Functionality --- index.html | 20 +++++------ index.js => js/common.js | 0 js/details.js | 44 +++++++++++++++++++++++ js/index.js | 76 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 10 deletions(-) rename index.js => js/common.js (100%) create mode 100644 js/details.js create mode 100644 js/index.js diff --git a/index.html b/index.html index 8611333..c263563 100644 --- a/index.html +++ b/index.html @@ -67,18 +67,18 @@

Where in the world?

/> - + diff --git a/index.js b/js/common.js similarity index 100% rename from index.js rename to js/common.js 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}

+
    +
  • Population: ${country.population.toLocaleString()}
  • +
  • Region: ${country.region}
  • +
  • Capital: ${country.capital || 'N/A'}
  • +
+
+
+ `; + 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); + }); +} + + +