Skip to content

Commit

Permalink
v1.9 (vB => vA)
Browse files Browse the repository at this point in the history
  • Loading branch information
alundiak committed Oct 21, 2024
1 parent f4ded52 commit 48a98d3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 38 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@ But now, having this small app/page, I can always have a handy way on my Desktop
![img](./eu.svg)


## Data

As of Oct-2024 The [UN](https://en.wikipedia.org/wiki/United_Nations) recognizes `195` countries (`193` member states and `2` observer states).

Public API https://restcountries.com/v3.1/all returns `250` records:
- with different groups: Sovereign countries, Dependencies and Territories, Disputed or partially recognized region, Autonomous regions, Smaller entities.
- designated by 5 regions.
- with field are `unMember`: true/false.
- there are also `independent`: true/false.



## TODO (for sure)
- Introduce regions.
- Use `unMember`
- Fixup Mobile view, although not really needed.
- Cleanup/Refactor code.

Expand Down Expand Up @@ -54,7 +67,7 @@ http-server .
## Credits

Inspired by:
- flags themselves :)
- flags themselves :) since childhood.
- https://en.wikipedia.org/wiki/Flags_of_Europe
- https://en.wikipedia.org/wiki/Flags_of_Asia
- https://emojipedia.org/flags#grid
Expand Down
54 changes: 35 additions & 19 deletions flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,69 @@ async function getCountryFlags() {
const region = country.region;
composedFlagsData[region] = composedFlagsData[region] || {};
const code = country.cca2.toLowerCase();

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

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

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

function createFlagButtons(flagsData) {
const container = document.getElementById('flags-container');
const flagsContainer = document.getElementById('flags-container');
for (const [region, regionObject] of Object.entries(flagsData)) {
// <p> or <span> or <tr>
for (const [code, codeObject] of Object.entries(regionObject)) {
const spanElement = document.createElement('span');

// vA
// spanElement.textContent = `${codeObject.flag}`;
// spanElement.title = `${codeObject.name}`;
spanElement.textContent = `${codeObject.flag}`;
spanElement.title = `${codeObject.name}`;
if (!codeObject.unMember) {
spanElement.classList.add('notUnMember');
}

// vB
const keys = Object.keys(codeObject);
const flagEmoji = keys[0];
const countryName = codeObject[flagEmoji];
spanElement.textContent = `${flagEmoji}`;
spanElement.title = `${countryName}`;
// const keys = Object.keys(codeObject);
// const flagEmoji = keys[0];
// const countryName = codeObject[flagEmoji];
// spanElement.textContent = `${flagEmoji}`;
// spanElement.title = `${countryName}`;

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

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

function renderCountryFlagsTotal(countryFlagsCount) {
const flagsTotal = document.getElementById('flags-count');
const spanElement = document.createElement('span');
spanElement.textContent = `${countryFlagsCount} country flags here:`;
flagsTotal.appendChild(spanElement);
}

function copyToClipboard(codeObject) {
// vA
// const flagEmoji = codeObject.flag;
// const countryName = codeObject.name;
const flagEmoji = codeObject.flag;
const countryName = codeObject.name;

// vB
const keys = Object.keys(codeObject);
const flagEmoji = keys[0];
const countryName = codeObject[flagEmoji];
// const keys = Object.keys(codeObject);
// const flagEmoji = keys[0];
// const countryName = codeObject[flagEmoji];

navigator.clipboard.writeText(flagEmoji).then(() => {
// console.log(`Copied: ${flagEmoji}`);
Expand All @@ -82,8 +94,12 @@ function showToast(message) {
}, 2000);
}

getCountryFlags().then(flagsFromApi => {
getCountryFlags().then(response => {
const [countryFlagsCount, flagsFromApi] = response;

console.log(flagsFromApi);
// console.log(JSON.stringify(flagsFromApi));
createFlagButtons(flagsFromApi);

renderCountryFlagsTotal(countryFlagsCount);
});
30 changes: 12 additions & 18 deletions index.css
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
body {
display: flex;
/* display: flex;
justify-content: center;
align-items: center;
align-items: center; */
height: 100vh;
margin: 10px;
}

/* button {
width: 40px;
height: 40px;
font-size: 30px;
&:hover {
cursor: pointer;
}
} */
#flags-container {
span {
font-size: 50px;

&:hover {
cursor: pointer;
}

span {
/* width: 40px;
height: 40px; */
font-size: 50px;

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


.toast {
position: fixed;
bottom: 20px;
Expand Down
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
</head>

<body>
<h2 id="flags-count"></h2>
<p></p>
<div id="flags-container"></div>


<!-- <script type="text/javascript" src="index.js"></script> -->
<!-- <script src="./f1.js"></script> -->
<script src="./flags.js"></script>
Expand Down

0 comments on commit 48a98d3

Please sign in to comment.