-
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
done working on countries #20
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,24 @@ | ||
function getCountry() { | ||
document.addEventListener("DOMContentLoaded", () => { | ||
const countryDetails = JSON.parse(localStorage.getItem("countryDetails")); | ||
|
||
if (countryDetails) { | ||
document.querySelector(".country-flag img").src = countryDetails.flag; | ||
document.querySelector(".country-flag img").alt = `${countryDetails.name} Flag`; | ||
document.querySelector(".country-title").textContent = countryDetails.name; | ||
|
||
const countryBrief = document.querySelector(".country-brief"); | ||
countryBrief.innerHTML = ` | ||
<li><strong>Population:</strong> ${countryDetails.population}</li> | ||
<li><strong>Region:</strong> ${countryDetails.region}</li> | ||
<li><strong>Capital:</strong> ${countryDetails.capital}</li> | ||
`; | ||
|
||
console.log("Country details loaded:", countryDetails); | ||
} else { | ||
console.error("No country details found in localStorage."); | ||
} | ||
}); | ||
} | ||
|
||
getCountry(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
let countriesData = []; | ||
const countriesGrid= document.querySelector('.countries-grid'); | ||
const searchInput= document.querySelector('.search-input'); | ||
const dropdownWrapper = document.querySelector('.dropdown-wrapper'); | ||
const dropdownHeader = document.querySelector('.dropdown-header'); | ||
|
||
|
||
fetch('./CountriesData.json') | ||
.then((response) => response.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. Hardcoded strings should be saved inside constant file, this will enable you to add or change things inside 1 place (Other places might need this string as well) |
||
.then((countries) => { | ||
countriesData = countries; | ||
renderCountreis(countries); | ||
}) | ||
.catch((error) => console.error('Error fetching countries:', error)); | ||
|
||
//----------------render the countries function---------------- | ||
|
||
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. You don`t need this comment because the name of the function makes it super clear |
||
function renderCountreis(countries){ | ||
|
||
countriesGrid.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. Same comment in this function about |
||
countries.forEach((country) => { | ||
const countryCard= document.createElement('div'); | ||
countryCard.classList.add('country'); | ||
|
||
countryCard.innerHTML = ` | ||
<a href ="details.html?country=${encodeURIComponent(country.name)}" | ||
onclick="setCountry('${country.flag}','${country.name}','${country.population}','${country.region}','${country.capital}')" | ||
class="country scale-effect countryDiv"> | ||
<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}</li> | ||
</ul> | ||
</div> | ||
</a> | ||
`; | ||
|
||
countriesGrid.appendChild(countryCard); | ||
}); | ||
|
||
} | ||
|
||
//----------------set country function- single country page (saves the data in local storage)------------- | ||
|
||
function setCountry(flag, name, population, region, capital){ | ||
const countryDetails = { flag, name, population, region, capital }; | ||
localStorage.setItem("countryDetails", JSON.stringify(countryDetails)); | ||
console.log("Country details saved:", countryDetails); | ||
} | ||
|
||
//----------------opens the filter by region---------------- | ||
|
||
dropdownHeader.onclick = () => { | ||
dropdownWrapper.classList.toggle('open'); | ||
}; | ||
|
||
//----------------filter the countries by the selected regions---------------- | ||
|
||
function filterCountries(){ | ||
const checkBoxes= document.querySelectorAll('.dropdown-body input[type="checkbox"]'); | ||
const selectedRegions=[]; | ||
|
||
checkBoxes.forEach((checkbox)=>{ | ||
if (checkbox.checked && checkbox.value!== 'all'){ | ||
selectedRegions.push(checkbox.value.toLowerCase()); | ||
} | ||
}) | ||
|
||
let filteredCountries= countriesData; | ||
|
||
if (selectedRegions.length>0){ | ||
filteredCountries=countriesData.filter((country)=> | ||
selectedRegions.includes(country.region.toLowerCase()) | ||
); | ||
} | ||
|
||
renderCountreis(filteredCountries); | ||
} | ||
|
||
//----------------find a country by name in search-line---------------- | ||
|
||
searchInput.addEventListener("input",()=>{ | ||
const searchValue = searchInput.value.trim().toLowerCase(); | ||
|
||
const filteredCountries = countriesData.filter(country => | ||
country.name.toLowerCase().startsWith(searchValue) | ||
); | ||
renderCountreis(filteredCountries); | ||
}) | ||
|
||
|
||
|
||
|
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.
Couple of things regarding this function :
document.querySelector(".country-flag img")
, save this inside a variable, and one more thing that you should do is to style your element via css classes and not jsinnerHTML
, it is a dangerous method