Skip to content

Commit

Permalink
1.9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
alundiak committed Oct 21, 2024
1 parent 48a98d3 commit 24e7880
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 35 deletions.
100 changes: 68 additions & 32 deletions flags.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,60 @@
async function getCountryFlags() {
async function getRestCountriesFromApi() {
try {
const response = await fetch('https://restcountries.com/v3.1/all');
const countries = await response.json();

const composedFlagsData = {};
countries.forEach(country => {
const region = country.region;
composedFlagsData[region] = composedFlagsData[region] || {};
const code = country.cca2.toLowerCase();

composedFlagsData[region][code] = {
// vA
name: country.name.common,
flag: country.flag,
unMember: country.unMember

// vB
// [country.flag]: country.name.common
};
});

return [countries.length, composedFlagsData];
return countries;
} catch (error) {
console.error('Error fetching country flags:', error);
console.error('Error fetching restcountries.com:', error);
return {};
}
}

function createFlagButtons(flagsData) {
function extractFilteringByUnMember(countriesApiData) {
return countriesApiData.reduce((result, country) => {
if (country.unMember) {
result.unMembers.push(country);
} else {
result.nonUnMembers.push(country);
}
return result;
}, { unMembers: [], nonUnMembers: [] });
}

function composeCountryFlagsData(countriesData) {
const simplifiedData = {};
countriesData.forEach(country => {
const region = country.region;
simplifiedData[region] = simplifiedData[region] || {};
const code = country.cca2.toLowerCase();

simplifiedData[region][code] = {
// vA
name: country.name.common,
flag: country.flag,
unMember: country.unMember // maybe not needed

// vB
// [country.flag]: country.name.common
};
});

return simplifiedData;
}

function createFlagButtons(flagsData, isUnMember, dataLength) {
const flagsContainer = document.getElementById('flags-container');
const divElement1 = document.createElement('div');

const h3Element = document.createElement('h3');
if (isUnMember) {
h3Element.textContent = `${dataLength} UN members`;
} else {
divElement1.classList.add('notUnMember');
h3Element.textContent = `${dataLength} non-UN members`;
}
divElement1.appendChild(h3Element);


for (const [region, regionObject] of Object.entries(flagsData)) {
// <p> or <span> or <tr>
for (const [code, codeObject] of Object.entries(regionObject)) {
Expand All @@ -37,9 +63,6 @@ function createFlagButtons(flagsData) {
// vA
spanElement.textContent = `${codeObject.flag}`;
spanElement.title = `${codeObject.name}`;
if (!codeObject.unMember) {
spanElement.classList.add('notUnMember');
}

// vB
// const keys = Object.keys(codeObject);
Expand All @@ -50,9 +73,15 @@ function createFlagButtons(flagsData) {

spanElement.onclick = () => copyToClipboard(codeObject);

flagsContainer.appendChild(spanElement);
divElement1.appendChild(spanElement);
}
}

flagsContainer.appendChild(divElement1);

// experimental
const divElement2 = document.createElement('div');
flagsContainer.appendChild(divElement2);
}

function renderCountryFlagsTotal(countryFlagsCount) {
Expand Down Expand Up @@ -94,12 +123,19 @@ function showToast(message) {
}, 2000);
}

getCountryFlags().then(response => {
const [countryFlagsCount, flagsFromApi] = response;
getRestCountriesFromApi().then(apiData => {
console.log(apiData);
// console.log(JSON.stringify(apiData));

renderCountryFlagsTotal(apiData.length);

const { unMembers, nonUnMembers } = extractFilteringByUnMember(apiData);
console.log(unMembers);
console.log(nonUnMembers);

console.log(flagsFromApi);
// console.log(JSON.stringify(flagsFromApi));
createFlagButtons(flagsFromApi);
const dataToRender1 = composeCountryFlagsData(unMembers);
const dataToRender2 = composeCountryFlagsData(nonUnMembers);

renderCountryFlagsTotal(countryFlagsCount);
createFlagButtons(dataToRender1, true, unMembers.length);
createFlagButtons(dataToRender2, false, nonUnMembers.length);
});
6 changes: 3 additions & 3 deletions index.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ body {
&:hover {
cursor: pointer;
}
}

&.notUnMember {
border: 1px dashed red;
}
.notUnMember {
border: 1px dashed red;
}
}

Expand Down

0 comments on commit 24e7880

Please sign in to comment.