-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (96 loc) · 3.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
let searchInput = document.getElementById('country-name');
let countries_list = document.querySelector('.list-countries');
let base_api_url = `https://restcountries.com/v3.1`;
let getCountries = () => {
let api_url = `${base_api_url}/all?fields=name,cca3`;
fetch(api_url).then((resp) => resp.json()).then((data) => {
let countries = data.sort((a,b) => (a.name.common > b.name.common) ? 1 : ((b.name.common > a.name.common) ? -1 : 0));
countries_list.querySelector('.loader-wrapper').remove();
if (countries.length > 0) {
countries.forEach(function(country){
countries_list.insertAdjacentHTML('beforeend', `
<li data-cca3="${country.cca3}">${country.name.common}</li>
`
);
});
}
else {
countries_list.innerHTML = `<div class="msg">No country found</div>`;
}
});
}
let filterList = () => {
let list_items = countries_list.getElementsByTagName('li');
for (var i = 0; i < list_items.length; i++) {
let item_text = list_items[i].textContent || list_items[i].innerText;
if (item_text.toUpperCase().indexOf(searchInput.value.toUpperCase()) > -1) {
list_items[i].style.display = '';
}
else {
list_items[i].style.display = 'none';
}
}
}
let getCountry = (cca3) => {
let result = document.getElementById("result");
result.innerHTML = `<div class="loader-wrapper"><div class="loader"></div></div>`;
if (cca3.length <= 0) {
result.innerHTML = `<div class="msg">Please enter a country name </div>`;
}
else {
let api_url = `${base_api_url}/alpha/${cca3}?fields=name,cca3,capital,currencies,flags,population,maps`;
fetch(api_url).then((resp) => resp.json()).then((data) => {
if (typeof data.status != 'undefined' && data.status == '404')
result.innerHTML = `<div class="msg">${data.message}</div>`;
else {
document.querySelectorAll(`.list-countries li.active`).forEach(function(elt){
elt.classList.remove('active');
});
document.querySelector(`.list-countries li[data-cca3="${cca3}"]`).classList.add('active');
let country = data;
showCountryDetails(country);
}
});
}
}
let showCountryDetails = (country) => {
let result = document.getElementById("result");
let details = '';
if ('currencies' in country && Object.values(country.currencies).length > 0) {
let currency = Object.values(country.currencies)[0];
details += `
<li><span class="label">Currency: </span><span>${currency.name} (${currency.symbol})</span></li>
`;
}
if ('capital' in country && country.capital != '') {
details += `
<li><span class="label">Capital: </span><span>${country.capital}</span></li>
`;
}
if ('population' in country && country.population != '') {
details += `
<li><span class="label">Population: </span><span>${new Intl.NumberFormat().format(country.population)}</span></li>
`;
}
result.innerHTML = `
<div class="info">
<img src="${country.flags.png}" class="flag" title="${country.flags.alt}">
<div>
<h2>${country.name.common}</h2>
<div class="official-name">
${country.cca3} - ${country.name.official}
</div>
</div>
</div>
<ul class="details">${details}</ul>
<div class="map">
🗺️ <a href="${country.maps.googleMaps}" target="_blank">View in Google Maps</a>
</div>
`;
}
window.addEventListener("load", getCountries);
countries_list.addEventListener("click", (e) => {
e.preventDefault();
getCountry(e.target.getAttribute('data-cca3'));
});
searchInput.addEventListener("input", filterList);