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

added js #18

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
4 changes: 4 additions & 0 deletions css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@
font-weight: inherit;
}



.filters .dropdown-wrapper {
position: relative;
width: 230px;

}

@media (min-width: 64em) {
Expand All @@ -92,6 +95,7 @@
z-index: 9;
}


.filters .dropdown-header,
.filters .dropdown-body {
position: relative;
Expand Down
1 change: 1 addition & 0 deletions details.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ <h1>Where in the world?</h1>
<button
type="button"
aria-label="Theme Switcher Button"
id="theme-toggle" onclick="changeBackgroundColor()""
class="theme-toggle flex flex-jc-sb flex-ai-c"
>
<i class="fa-regular fa-moon theme-icon"></i>
Expand Down
11 changes: 8 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</button>
<!-- Header -->
<header class="header">
<div class="container flex flex-jc-sb flex-ai-c">
<div id="container" class="container flex flex-jc-sb flex-ai-c">
<div class="logo">
Copy link
Member

Choose a reason for hiding this comment

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

You already have class of container, why do you need also id ?

<a href="index.html">
<h1>Where in the world?</h1>
Expand All @@ -47,6 +47,8 @@ <h1>Where in the world?</h1>
<button
type="button"
aria-label="Theme Switcher Button"
id="theme-toggle" onclick="changeBackgroundColor()"

class="theme-toggle flex flex-jc-sb flex-ai-c"
>
<i class="fa-regular fa-moon theme-icon"></i>
Expand All @@ -66,12 +68,12 @@ <h1>Where in the world?</h1>
placeholder="Search for a country..."
/>
</div>
<div class="dropdown-wrapper">
<div class="dropdown-wrapper" onclick="openOptions()">
<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">
<div class="dropdown-body" >
<ul>
<li data-region="all">All</li>
<li data-region="africa">Africa</li>
Expand Down Expand Up @@ -154,5 +156,8 @@ <h2 class="country-title">Belize</h2>
</section>
</div>
</main>

<script src="./js/common.js"></script>
<script src="./js/index.js"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions js/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const themeToggleButton = document.getElementById("theme-toggle");

const changeBackgroundColor = () => {
// Toggle the 'Dark' class on the <body>
document.body.classList.toggle('Dark');
Copy link
Member

Choose a reason for hiding this comment

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

In here please use styling via css classes and not js (this way you don`t need all of your logic)


if (document.body.classList.contains('Dark')) {
themeToggleButton.querySelector('.theme-text').textContent = 'Light Mode';
document.body.style.backgroundColor = 'hsl(207, 26%, 17%)';
document.querySelector('header').style.backgroundColor = 'hsl(209, 23%, 22%)'; // Dark header
document.querySelector('header').style.color = 'white'; // Light text color
} else {
themeToggleButton.querySelector('.theme-text').textContent = 'Dark Mode';
document.body.style.backgroundColor = 'white'; // Light background
document.querySelector('header').style.backgroundColor = 'white'; // Light header
document.querySelector('header').style.color = 'black'; // Dark text color
}
};
43 changes: 43 additions & 0 deletions js/details.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
document.addEventListener("DOMContentLoaded", () => {
const countryDetailsSection = document.querySelector(".country-details");
const loader = document.querySelector(".loader");

// Extract country name
const urlParams = new URLSearchParams(window.location.search);
const countryName = urlParams.get("country");

Copy link
Member

Choose a reason for hiding this comment

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

You dont need to add this comment, you already have a variable names countryName

if (!countryName) {
countryDetailsSection.innerHTML = `<p>Country not specified in the query.</p>`;
loader.style.display = "none";
return;
}

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 strigns should be saved into constants variables

.then((data) => {
const country = data.find((c) => c.name === countryName);

if (!country) {
countryDetailsSection.innerHTML = `<p>Country details not found.</p>`;
return;
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 it is a dangerous method

}

countryDetailsSection.innerHTML = `
<div class="country-details">
Copy link
Member

Choose a reason for hiding this comment

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

Same here

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

loader.style.display = "none";
});

79 changes: 79 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const countriesGrid = document.querySelector(".countries-grid");
const searchInput = document.querySelector(".search-input");
const dropdownHeader = document.querySelector(".dropdown-header");
const dropdownBody = document.querySelector(".dropdown-body");
const dropdownItems = document.querySelectorAll(".dropdown-body ul li");

// Fetch data from CountriesData.json
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.

Why not extract his into a function and call if getCountriesDate()
And call it instead of putting a comment

.then((data) => {
renderCountries(data);
setupFilters(data);
});

// Function to render countries
const renderCountries = (data) => {
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.

The name of the function should indicate what it is doing

data.forEach((country) => {
countriesGrid.innerHTML += `
<a href="#" class="country scale-effect" data-country-name="${country.name}" data-region="${country.region.toLowerCase()}" onclick="setupCountryLinks(event)">
<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>`;
});
};


const setupCountryLinks = (event) => {

event.preventDefault(); // Prevent default anchor behavior
const countryName = event.currentTarget.getAttribute("data-country-name");
window.location.href = `details.html?country=${encodeURIComponent(countryName)}`;
};



const openOptions = () => {
const dropdownWrapper = document.querySelector('.dropdown-wrapper');
dropdownWrapper.classList.toggle('open');
};



// Set up search and region filters
const setupFilters = (data) => {

searchInput.addEventListener("input", (e) => {
const query = e.target.value.toLowerCase();
const filteredData = data.filter((country) =>
country.name.toLowerCase().includes(query)
);
renderCountries(filteredData);
});


// Dropdown filter
dropdownItems.forEach((item) => {
item.addEventListener("click", () => {
const region = item.getAttribute("data-region");
const filteredData =
region === "all"
? data
: data.filter((country) => country.region.toLowerCase() === region);
renderCountries(filteredData);

// Close dropdown after selection
dropdownBody.classList.remove("visible");
});
});
}