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

Lian countries #10

Open
wants to merge 3 commits 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
4 changes: 2 additions & 2 deletions CountriesData.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"name": "Anguilla",
"flag": "https://flagcdn.com/w320/ai.png",
"population": 13452,
"region": "Americas",
"region": "America",
"capital": "The Valley"
},
{
Expand Down Expand Up @@ -80,7 +80,7 @@
"name": "Jamaica",
"flag": "https://flagcdn.com/w320/jm.png",
"population": 2723246,
"region": "Americas",
"region": "America",
"capital": "Kingston"
},
{
Expand Down
6 changes: 4 additions & 2 deletions details.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ <h1>Where in the world?</h1>
<section class="country-details"></section>
</div>
</main>
<script src="./js/common.js"></script>
<script src="./js/details.js"></script>
<!-- <script src="./js/common.js"></script>
<script src="./js/details.js"></script> -->
</body>
Copy link
Member

Choose a reason for hiding this comment

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

Don`t use comments, remove non used code

<script src="details.js"></script>

</html>
38 changes: 38 additions & 0 deletions details.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
document.addEventListener("DOMContentLoaded", () => {

const urlParams = new URLSearchParams(window.location.search);
const name = urlParams.get("name");
const flag = urlParams.get("flag");
const population = urlParams.get("population");
Copy link
Member

Choose a reason for hiding this comment

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

You should extract this into service and use his methods from there, for example :

paramsService.getValue()

const region = urlParams.get("region");
const capital = urlParams.get("capital");

const countryDetailsSection = document.querySelector(".country-details");
const loader = document.querySelector(".loader");

loader.style.display = "flex";

if (name && flag && population && region && capital) {
const cardHTML = `
<div class="country">
<div class="country-flag">
<img src="${flag}" alt="${name} Flag" class="country-image" />
</div>
<div class="country-info">
<h2 class="country-title">${name}</h2>
<ul>
<li><strong>Population:</strong> ${Number(population).toLocaleString()}</li>
<li><strong>Region:</strong> ${region}</li>
<li><strong>Capital:</strong> ${capital}</li>
</ul>
</div>
</div>
`;

countryDetailsSection.innerHTML = cardHTML;
loader.style.display = "none";
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Please do not use innerHTML its a dangerous method

Also don`t change elements styles from js, use classes.

loader.style.display = "none";
alert("Failed to load country details. Missing data.");
    }
});
235 changes: 85 additions & 150 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,158 +1,93 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta
name="description"
content="This is a simple project to display countries of the world with the ability to search for the country you want, be side the ability to show countries by continent."
/>
<meta name="author" content="Mohamed Aridah" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Google Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Nunito+Sans:wght@300;600;800&display=swap"
rel="stylesheet"
/>
<!-- Fontawesome Library -->
<link
rel="stylesheet"
href="https://site-assets.fontawesome.com/releases/v6.0.0/css/all.css"
/>
<!-- Shared CSS Styles File -->
<link rel="stylesheet" href="./css/common.css" />
<!-- Page Custom CSS File -->
<link rel="stylesheet" href="./css/main.css" />
<title>Countries Info</title>
</head>
<body>
<!-- Scroll Top -->
<button
type="button"
aria-label="Scroll Top Button"
class="button scroll-top flex flex-ai-c flex-jc-c scale-effect"
>
<i class="fa-regular fa-chevron-up icon"></i>
</button>
<!-- Header -->
<header class="header">
<div class="container flex flex-jc-sb flex-ai-c">
<div class="logo">
<a href="index.html">
<h1>Where in the world?</h1>
</a>
</div>
<button
type="button"
aria-label="Theme Switcher Button"
class="theme-toggle flex flex-jc-sb flex-ai-c"
>
<i class="fa-regular fa-moon theme-icon"></i>
<span class="theme-text">Dark Mode</span>
</button>

<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description"
content="This is a simple project to display countries of the world with the ability to search for the country you want, be side the ability to show countries by continent." />
<meta name="author" content="Mohamed Aridah" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Google Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Nunito+Sans:wght@300;600;800&display=swap" rel="stylesheet" />
<!-- Fontawesome Library -->
<link rel="stylesheet" href="https://site-assets.fontawesome.com/releases/v6.0.0/css/all.css" />
<!-- Shared CSS Styles File -->
<link rel="stylesheet" href="./css/common.css" />
<!-- Page Custom CSS File -->
<link rel="stylesheet" href="./css/main.css" />
<title>Countries Info</title>
</head>

<body>
<!-- Scroll Top -->
<button type="button" aria-label="Scroll Top Button" class="button scroll-top flex flex-ai-c flex-jc-c scale-effect">
<i class="fa-regular fa-chevron-up icon"></i>
</button>
<!-- Header -->
<header class="header">
<div class="container flex flex-jc-sb flex-ai-c">
<div class="logo">
<a href="index.html">
<h1>Where in the world?</h1>
</a>
</div>
</header>
<!-- Filters -->
<button type="button" aria-label="Theme Switcher Button" class="theme-toggle flex flex-jc-sb flex-ai-c">
<i class="fa-regular fa-moon theme-icon"></i>
<span class="theme-text">Dark Mode</span>
</button>
</div>
</header>
<!-- Filters -->

<section class="filters">
<div class="container">
<div class="search-wrapper">
<i class="fa-regular fa-magnifying-glass search-icon"></i>
<input
type="text"
class="search-input"
placeholder="Search for a country..."
/>
<section class="filters">
<div class="container">
<div class="search-wrapper">
<i class="fa-regular fa-magnifying-glass search-icon"></i>
<input type="text" class="search-input" placeholder="Search for a country..." />
</div>
<div class="dropdown-wrapper">
<div class="dropdown-header flex flex-jc-sb flex-ai-c">
<span>Filter by Region</span>
<i class="fa-regular fa-chevron-down icon"></i>
</div>
<div class="dropdown-body">
<ul>
<li data-region="all">All</li>
<li data-region="africa">Africa</li>
<li data-region="america">America</li>
<li data-region="asia">Asia</li>
<li data-region="europe">Europe</li>
<li data-region="oceania">Oceania</li>
</ul>
</div>
<div class="dropdown-wrapper">
<div class="dropdown-header flex flex-jc-sb flex-ai-c">
<span>Filter by Region</span>
<i class="fa-regular fa-chevron-down icon"></i>
</div>
</div>
</section>
<!-- Countries -->
<main class="main">
<div class="container">
<section class="countries-grid">
<a href="#" class="country scale-effect" data-country-name="" style="display:none;">
<!-- remember to switch the style into -->
<div class="country-flag">
<img src="#" alt="country Flag" class="country-image" />
</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>
<div class="country-info">
<h2 class="country-title">Country Name</h2>
<ul class="country-brief">
<li><strong>population: </strong><span class="country-population"></span></li>
<li><strong>region: </strong><span class="country-region"></span></li>
<li><strong>capital: </strong><span class="country-capital"></span></li>
</ul>
</div>
</div>
</div>
</section>
<!-- Countries -->
<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>
</body>
</html>
</a>
</section>
</div>
</main>
<script src="script.js"></script>
</body>

</html>
63 changes: 63 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
document.addEventListener("DOMContentLoaded", () => {
fetch("CountriesData.json")
.then(response => {
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 extracted into constant service

if (!response.ok) {
throw new Error("Failed to load countries JSON");
}
return response.json();
})
.then(countries => {
const template = document.querySelector('.country[data-country-name]');
const grid = document.querySelector(".countries-grid");
function displayCountries(filteredCountries) {
grid.innerHTML = "";
filteredCountries.forEach(country => {
const card = template.cloneNode(true);
card.style.display = "block";
card.setAttribute("data-country-name", country.name);
card.setAttribute("href", `details.html?name=${encodeURIComponent(country.name)}&flag=${encodeURIComponent(country.flag)}&population=${country.population}&region=${encodeURIComponent(country.region)}&capital=${encodeURIComponent(country.capital || "N/A")}`);
card.querySelector(".country-image").src = country.flag;
card.querySelector(".country-image").alt = `${country.name} Flag`;
card.querySelector(".country-title").textContent = country.name;
Copy link
Member

Choose a reason for hiding this comment

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

You should save your element and not use 2 querySelectors

card.querySelector(".country-population").textContent = country.population.toLocaleString();
card.querySelector(".country-region").textContent = country.region;
card.querySelector(".country-capital").textContent = country.capital;

grid.appendChild(card);
});
}

displayCountries(countries);
const dropDownItems = document.querySelectorAll(".dropdown-body li");
dropDownItems.forEach(item => {
item.addEventListener("click", (event) => {
const region = event.target.getAttribute("data-region");
const filteredCountries = region === "all"
? countries
: countries.filter(country => country.region.toLowerCase() === region.toLowerCase());

displayCountries(filteredCountries);
const dropDownWrapper = document.querySelector(".dropdown-wrapper");
dropDownWrapper.classList.remove("open");
});
});
})


.catch(error => {
console.error("Error loading countries data: ", error);
});

const dropDownHeader = document.querySelector(".dropdown-header");
const dropDownWrapper = document.querySelector(".dropdown-wrapper");

dropDownHeader.addEventListener("click", () => {
dropDownWrapper.classList.toggle("open");
})
document.addEventListener("click", (event)=>{
if(!dropDownWrapper.contains(event.target) && !dropDownHeader.contains(event.target))
dropDownWrapper.classList.remove("open");
})


});