-
Notifications
You must be signed in to change notification settings - Fork 30
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
countries- project by chen kaduri #22
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,79 @@ | ||
document.addEventListener('DOMContentLoaded', function () { | ||
const urlParams = new URLSearchParams(window.location.search); | ||
const countryName = urlParams.get('country'); | ||
|
||
if (!countryName) { | ||
document.getElementById('country-name').innerHTML = '<p>No country selected!</p>'; | ||
return; | ||
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. Do not use |
||
} | ||
|
||
fetch('CountriesData.json') | ||
.then(response => { | ||
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. Hardcoded strings should be saved into constants file |
||
if (!response.ok) { | ||
throw new Error('Failed to load JSON data'); | ||
} | ||
return response.json(); | ||
}) | ||
.then(countries => { | ||
const country = countries.find(c => c.name.toLowerCase() === countryName.toLowerCase()); | ||
|
||
//If the country is found, display its details | ||
if (country) { | ||
// Create the main container | ||
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. Please avoid explaining the code with comments, is a bad practice. |
||
const countryContainer = document.createElement('div'); | ||
countryContainer.className = 'country-details'; | ||
|
||
// Create the flag section | ||
const flagContainer = document.createElement('div'); | ||
flagContainer.className = 'country-flag'; | ||
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. If you need to explain this with comment you can extract this into function named 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 comment apply to everywhere in your code |
||
const flagImage = document.createElement('img'); | ||
flagImage.src = country.flag; | ||
flagImage.alt = `${country.name} Flag`; | ||
flagContainer.appendChild(flagImage); | ||
|
||
// Create the info section | ||
const infoContainer = document.createElement('div'); | ||
infoContainer.className = 'country-info'; | ||
|
||
// Add country name | ||
const countryTitle = document.createElement('h1'); | ||
countryTitle.textContent = country.name; | ||
infoContainer.appendChild(countryTitle); | ||
|
||
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 good instead of using |
||
// Add details | ||
const details = [ | ||
{ label: 'Population', value: country.population.toLocaleString() }, | ||
{ label: 'Region', value: country.region }, | ||
{ label: 'Capital', value: country.capital || 'N/A' } | ||
]; | ||
|
||
details.forEach(detail => { | ||
const detailElement = document.createElement('p'); | ||
detailElement.innerHTML = `<strong>${detail.label}:</strong> ${detail.value}`; | ||
infoContainer.appendChild(detailElement); | ||
}); | ||
|
||
// Nest flag and info into the main container | ||
countryContainer.appendChild(flagContainer); | ||
countryContainer.appendChild(infoContainer); | ||
|
||
// Add the container to the body | ||
document.body.appendChild(countryContainer); | ||
|
||
// Remove the loader if it exists | ||
const loader = document.getElementById('loader'); | ||
if (loader) { | ||
loader.style.display = 'none'; | ||
} | ||
} else { | ||
// If country is not found, display a message | ||
const errorMessage = document.createElement('p'); | ||
errorMessage.textContent = 'Country not found!'; | ||
document.body.appendChild(errorMessage); | ||
} | ||
}) | ||
.catch(error => { | ||
console.error('Error loading the JSON data:', error); | ||
}); | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
fetch('CountriesData.json') | ||
.then((response) => response.json()) | ||
.then((countries) => { | ||
const countriesContainer = document.getElementById('countries-g'); | ||
if (!countriesContainer) { | ||
console.error('countries-g container not found in the DOM!'); | ||
return; | ||
} | ||
|
||
// global veraible | ||
let allCountries = countries; | ||
|
||
// view the countries | ||
function displayCountries(countriesToDisplay) { | ||
countriesContainer.innerHTML = ''; // Cleaning all the current HTML in the div | ||
countriesToDisplay.forEach((country) => { | ||
const countryDiv = document.createElement('div'); | ||
countryDiv.className = 'country'; | ||
|
||
const countryLink = document.createElement('a'); | ||
countryLink.href = `details.html?country=${country.name}`; | ||
countryDiv.appendChild(countryLink); | ||
|
||
const countryflagDiv = document.createElement('div'); | ||
countryflagDiv.className = 'country-flag'; | ||
|
||
const imgFlag = document.createElement('img'); | ||
imgFlag.src = `${country.flag}`; | ||
imgFlag.alt = `${country.name}`; | ||
countryflagDiv.appendChild(imgFlag); | ||
countryLink.appendChild(countryflagDiv); | ||
|
||
const countryInfo = document.createElement('div'); | ||
countryInfo.className = 'country-info'; | ||
|
||
const countryH = document.createElement('h2'); | ||
countryH.textContent = `${country.name}`; | ||
countryInfo.appendChild(countryH); | ||
|
||
const countryPopulation = document.createElement('p'); | ||
const strongP = document.createElement('strong'); | ||
strongP.textContent = 'Population: '; | ||
countryPopulation.appendChild(strongP); | ||
countryPopulation.appendChild(document.createTextNode(country.population)); | ||
countryInfo.appendChild(countryPopulation); | ||
|
||
const countryRegion = document.createElement('p'); | ||
const strongR = document.createElement('strong'); | ||
strongR.textContent = 'Region: '; | ||
countryRegion.appendChild(strongR); | ||
countryRegion.appendChild(document.createTextNode(country.region)); | ||
countryInfo.appendChild(countryRegion); | ||
|
||
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. In this page you have a lot of similar code again an again, can you think about a way to avoid it? |
||
const countryCapital = document.createElement('p'); | ||
const strongC = document.createElement('strong'); | ||
strongC.textContent = 'Capital: '; | ||
countryCapital.appendChild(strongC); | ||
countryCapital.appendChild(document.createTextNode(country.capital)); | ||
countryInfo.appendChild(countryCapital); | ||
|
||
countryDiv.appendChild(countryInfo); | ||
countriesContainer.appendChild(countryDiv); | ||
}); | ||
} | ||
|
||
|
||
displayCountries(allCountries); | ||
//filter by write | ||
function FilterByWrite(){ | ||
const filterText=document.getElementById("filter-by-write").value.toLowerCase(); | ||
//filter during writing | ||
const filterCountries= allCountries.filter(country => Object.values(country).some | ||
(value =>value.toString().toLowerCase().includes(filterText) ) ) | ||
displayCountries(filterCountries); | ||
} | ||
document.getElementById("filter-by-write").addEventListener(`input`,FilterByWrite ); | ||
|
||
// filter by region | ||
const filterItems = document.querySelectorAll('#dropdown-wrapper ul li'); | ||
filterItems.forEach((item) => { | ||
item.addEventListener('click', () => { | ||
const region = item.getAttribute('data-region'); | ||
|
||
let filteredCountries; | ||
if (region === 'all') { | ||
filteredCountries = allCountries; // all countries | ||
} else { | ||
filteredCountries = allCountries.filter((country) => { | ||
return country.region.toLowerCase() === region; | ||
}); | ||
} | ||
|
||
// the output | ||
if (filteredCountries.length === 0) { | ||
countriesContainer.innerHTML = '<p>No countries found.</p>'; | ||
} else { | ||
displayCountries(filteredCountries); | ||
} | ||
|
||
// close the menu after the choice | ||
document.getElementById('dropdown-wrapper').classList.remove('open'); | ||
}); | ||
}); | ||
}) | ||
.catch((error) => { | ||
console.error('Error loading the JSON data:', error); | ||
}); | ||
|
||
// open the menu of the filter | ||
document.addEventListener('DOMContentLoaded', function () { | ||
const dropdownHeader = document.getElementById('dropdown-header'); | ||
const dropdownWrapper = document.getElementById('dropdown-wrapper'); | ||
|
||
dropdownHeader.addEventListener('click', function () { | ||
dropdownWrapper.classList.toggle('open'); | ||
}); | ||
}); |
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.
Why do you need both class and id ?