Skip to content

Conversation

@nadav-cohen11
Copy link

No description provided.

Copy link
Member

@Tamir198 Tamir198 left a comment

Choose a reason for hiding this comment

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

Some things that you should fix generally :

Avoid Using innerHTML

Extract Repeating Concepts into a Service

<!-- <script src="./js/common.js"></script>
<script src="./js/details.js"></script> -->
<script src="script.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.

Delete code dont comment it out

if (countryFetchedData) {
return;
}
try {
Copy link
Member

Choose a reason for hiding this comment

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

  1. you can write this like if (countryFetchedData) return

  2. This function should do 1 thing and that fetch the data I recommend writing your logic before calling the function

}
try {
const response = await fetch('./CountriesData.json');
if (!response.ok) {
Copy link
Member

Choose a reason for hiding this comment

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

Also you can save your strings inside constants file or variable and not use them as hardcoded values

function getDropdownsRegions(data) {
const dropdownRegionFilter = document.querySelector('.dropdown-body');
const uniqueRegionsSet = new Set(data.map(country => country.region));
const uniqueRegions = [...uniqueRegionsSet]
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 you can destruct your object, it is generally a good practice:

const uniqueRegionsSet = new Set(data.map(({ region }) => region));

analyzeMainContainer(filteredData);
});
}

Copy link
Member

Choose a reason for hiding this comment

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

  • Never use innetHTMl, its a dangerous method

  • The name of the function does not reflect what the function is actually doing (adding event listener is not getting data). so please separate your code into helper functions

const countryData = { name, flag, population, region, capital };
localStorage.setItem('countryDetails', JSON.stringify(countryData));
});

Copy link
Member

Choose a reason for hiding this comment

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

Instead of using localStorage like this, create a service that expose method related to localStorage.

This will help you avoid repeat your code and seperate different concepts

data.forEach(({ name, flag, population, region, capital }) => {
const countryCard = document.createElement('a');
countryCard.href = './details.html';
countryCard.addEventListener('click', function () {
Copy link
Member

Choose a reason for hiding this comment

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

Same thing here about constants


countryCard.innerHTML = `
<div class="country-flag">
<img src="${flag}" alt="${name} Flag">
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 here - do not use innerHTMl

async function displayData() {
await fetchCountriesData();
if (countryFetchedData) {
analyzeMainContainer(countryFetchedData);
Copy link
Member

Choose a reason for hiding this comment

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

You can use guard clause for early return making the code cleaner :

async function displayData() {
    await fetchCountriesData();

    if (!countryFetchedData) {
        console.error('Error displaying countries data');
        return;
    }

    analyzeMainContainer(countryFetchedData);
    getDropdownsRegions(countryFetchedData);
    getSearchdata();
    handleDropdownClick();
}

function displayCountryDetails() {
const countryInfo = document.querySelector('.country-details');
const countryData = 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.

This for example could be a function coming from localsessionService and this is why this is a good practice. In your approach you will have to write that parsing logic again and again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants