-
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
added js #18
base: main
Are you sure you want to change the base?
added js #18
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,18 @@ | ||
const themeToggleButton = document.getElementById("theme-toggle"); | ||
|
||
const changeBackgroundColor = () => { | ||
// Toggle the 'Dark' class on the <body> | ||
document.body.classList.toggle('Dark'); | ||
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 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 | ||
} | ||
}; |
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"); | ||
|
||
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 dont need to add this comment, you already have a variable names |
||
if (!countryName) { | ||
countryDetailsSection.innerHTML = `<p>Country not specified in the query.</p>`; | ||
loader.style.display = "none"; | ||
return; | ||
} | ||
|
||
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 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; | ||
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 do not use |
||
} | ||
|
||
countryDetailsSection.innerHTML = ` | ||
<div class="country-details"> | ||
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 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"; | ||
}); | ||
|
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()) | ||
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. Why not extract his into a function and call if |
||
.then((data) => { | ||
renderCountries(data); | ||
setupFilters(data); | ||
}); | ||
|
||
// Function to render countries | ||
const renderCountries = (data) => { | ||
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. 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"); | ||
}); | ||
}); | ||
} |
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.
You already have class of container, why do you need also id ?