-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
64 lines (56 loc) · 2.3 KB
/
script.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
'use strict';
const searchBar = document.querySelector('#search-bar');
const btnSearch = document.querySelector('.btn-search');
const countriesContainer = document.querySelector('.countries');
// Function to render country data
const renderCountry = function (data, className = '') {
const titleHtml = className === 'neighbour' ? '<div class="country__title">Neighbour country</div>' : '';
const html = `
<article class="country ${className}">
${titleHtml}
<img class="country__img" src="${data.flags.svg}" />
<div class="country__data">
<h3 class="country__name">${data.name.common}</h3>
<h4 class="country__region">${data.region}</h4>
<p class="country__row"><span>👫</span>${(data.population / 1000000).toFixed(1)}M people</p>
<p class="country__row"><span>🗣️</span>${Object.values(data.languages)[0]}</p>
<p class="country__row"><span>💰</span>${Object.values(data.currencies)[0].name}</p>
</div>
</article>
`;
countriesContainer.insertAdjacentHTML('beforeend', html);
countriesContainer.style.opacity = 1;
};
// Function to fetch country data
const getCountryData = function (country) {
fetch(`https://restcountries.com/v3.1/name/${country}`)
.then(response => response.json())
.then(data => {
countriesContainer.innerHTML = ''; // Clear previous results
renderCountry(data[0]);
const neighbor = data[0].borders?.[0];
if (!neighbor) return;
// Fetch neighbor country data
return fetch(`https://restcountries.com/v3.1/alpha/${neighbor}`);
})
.then(response => response.json())
.then(data => renderCountry(data[0], 'neighbour'))
.catch(err => console.error(`${err} 💥💥💥`));
};
// Event listener for search button
btnSearch.addEventListener('click', function () {
const country = searchBar.value;
if (country) {
getCountryData(country);
}
});
// Optional: trigger search on pressing Enter key
searchBar.addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
const country = searchBar.value;
if (country) {
getCountryData(country);
}
}
});
getCountryData('usa');