Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion css/details.css
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ body {

.country-flag img {
display: block;
max-width: 100%;
width: 100%;
height:100%
object-fit: cover;
}

Expand All @@ -86,6 +87,8 @@ body {
.country-info li {
text-transform: capitalize;
color: #2b3945;


}

.country-info li button {
Expand Down
21 changes: 12 additions & 9 deletions details.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@
<title>Details Page</title>
</head>
<body>
<!-- Loader -->
<div class="loader">
<div class="spinner">
<i class="fa-regular fa-circle-notch fa-spin icon"></i>
</div>
</div>

<!-- Scroll Top -->
<button
type="button"
Expand Down Expand Up @@ -71,10 +66,18 @@ <h1>Where in the world?</h1>
</div>
<main class="main">
<div class="container">
<section class="country-details"></section>
<section class="country-details">
<div class="country-flag">
<img src="" alt="Country Flag" />
</div>
<div class="country-info">
<h2 class="country-title"></h2>
<ul class="country-brief"></ul>
</div>
</section>
</div>
</main>
<script src="./js/common.js"></script>
<script src="./js/details.js"></script>
<script src="index.js"></script>
<script src="details.js"></script>
</body>
</html>
24 changes: 24 additions & 0 deletions details.js
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"));
Copy link
Member

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 :

  1. Make sure that this function is only doing 1 thing, right now the function really overloaded with different things to do
  2. You can extract the localStorage logic into service and call the service methods from there
  3. Do not repeat 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 js
  4. Do not use innerHTML, it is a dangerous method


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();
96 changes: 27 additions & 69 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,30 @@ <h1>Where in the world?</h1>
</div>
<div class="dropdown-body">
<ul>
<li data-region="all">All</li>
<li data-region="africa">Africa</li>
<li data-region="americas">America</li>
<li data-region="asia">Asia</li>
<li data-region="europe">Europe</li>
<li data-region="oceania">Oceania</li>
<li>
<input type="checkbox" id="all" value="all" onclick="filterCountries()" />
<label for="all">All</label>
</li>
<li>
<input type="checkbox" id="Africa" value="Africa" onclick="filterCountries()" />
<label for="Africa">Africa</label>
</li>
<li>
<input type="checkbox" id="Americas" value="Americas" onclick="filterCountries()" />
<label for="Americas">America</label>
</li>
<li>
<input type="checkbox" id="Asia" value="Asia" onclick="filterCountries()" />
<label for="Asia">Asia</label>
</li>
<li>
<input type="checkbox" id="Europe" value="Europe" onclick="filterCountries()" />
<label for="Europe">Europe</label>
</li>
<li>
<input type="checkbox" id="Oceania" value="Oceania" onclick="filterCountries()" />
<label for="Oceania">Oceania</label>
</li>
</ul>
</div>
</div>
Expand All @@ -88,71 +106,11 @@ <h1>Where in the world?</h1>
<main class="main">
<div class="container">
<section class="countries-grid">
<a
href="#"
class="country scale-effect"
data-country-name="Afghanistan"
>
<div class="country-flag">
<img
src="https://upload.wikimedia.org/wikipedia/commons/5/5c/Flag_of_the_Taliban.svg"
alt="Afghanistan FLag"
/>
</div>
<div class="country-info">
<h2 class="country-title">Afghanistan</h2>
<ul class="country-brief">
<li><strong>population: </strong>40218234</li>
<li><strong>Region: </strong>Asia</li>
<li><strong>capital: </strong>Kabul</li>
</ul>
</div>
</a>
<a
href="#"
class="country scale-effect"
data-country-name="Åland Islands"
>
<div class="country-flag">
<img src="https://flagcdn.com/ax.svg" alt="Åland Islands FLag" />
</div>
<div class="country-info">
<h2 class="country-title">Åland Islands</h2>
<ul class="country-brief">
<li><strong>population: </strong>28875</li>
<li><strong>Region: </strong>Europe</li>
<li><strong>capital: </strong>Mariehamn</li>
</ul>
</div>
</a>
<a href="#" class="country scale-effect" data-country-name="Aruba">
<div class="country-flag">
<img src="https://flagcdn.com/aw.svg" alt="Aruba FLag" />
</div>
<div class="country-info">
<h2 class="country-title">Aruba</h2>
<ul class="country-brief">
<li><strong>population: </strong>106766</li>
<li><strong>Region: </strong>Americas</li>
<li><strong>capital: </strong>Oranjestad</li>
</ul>
</div>
</a>
<a href="#" class="country scale-effect" data-country-name="Belize">
<div class="country-flag">
<img src="https://flagcdn.com/bz.svg" alt="Belize FLag" />
</div>
<div class="country-info">
<h2 class="country-title">Belize</h2>
<ul class="country-brief">
<li><strong>population: </strong>397621</li>
<li><strong>Region: </strong>Americas</li>
<li><strong>capital: </strong>Belmopan</li>
</ul>
</div>
</a>
</section>
</div>
</main>

<script src="index.js"></script>
<script src="details.js"></script>
</body>
</html>
102 changes: 102 additions & 0 deletions index.js
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())
Copy link
Member

Choose a reason for hiding this comment

The 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----------------

Copy link
Member

Choose a reason for hiding this comment

The 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 = '';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment in this function about innerHTML

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);
})