-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
87 lines (76 loc) · 2.52 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict';
const btn = document.querySelector('.btn-country');
const countriesContainer = document.querySelector('.countries');
const renderCountry = function (data, className = '') {
let html = `
<article class="country ${className}">
<img class="country__img" src="${data.flag}" />
<div class="country__data">
<h3 class="country__name">${data.name}</h3>
<h4 class="country__region">${data.region}</h4>
<p class="country__row"><span>👫</span>${(
+data.population / 1000000
).toFixed(1)} mill. people</p>
<p class="country__row"><span>🗣️</span>${data.languages[0].name}</p>
<p class="country__row"><span>💰</span>${data.currencies[0].name}</p>
</div>
</article>
`;
countriesContainer.insertAdjacentHTML('beforeend', html);
countriesContainer.style.opacity = 1;
};
const renderError = function (msg) {
const errMsg = `
<p class = "pErr">${msg}</p>
<br>
<br>
<img class = "imgErr" src = "img/error-icon.png">
`;
countriesContainer.innerHTML += errMsg;
countriesContainer.style.backgroundColor = 'whitesmoke';
countriesContainer.style.opacity = 1;
};
const getJSON = function (url, errorMsg = 'Something went wrong') {
return fetch(url).then(response => {
if (!response.ok) throw new Error(`${errorMsg} (${response.status})`);
return response.json();
});
};
const getCountryData = function (country) {
// Country 1
getJSON(`https://restcountries.com/v2/name/${country}`, 'Country not found')
.then(data => {
countriesContainer.innerHTML = '';
countriesContainer.style.backgroundColor = 'transparent';
renderCountry(data[0]);
const neighbour = data[0].borders[0];
if (!neighbour) throw new Error('No neighbour found!');
// Country 2
return getJSON(
`https://restcountries.com/v2/alpha/${neighbour}`,
'Country not found'
);
})
.then(data => renderCountry(data, 'neighbour'))
.catch(err => {
console.error(`${err} 💥💥💥`);
renderError(`Something went wrong 💥💥💥 ${err.message}. Try again! `);
})
.finally(() => {
countriesContainer.style.opacity = 1;
});
};
btn.addEventListener('keyup', function (e) {
if (e.keyCode == 13) {
let enterCountry = btn.value;
if (enterCountry == '' || enterCountry == null) {
alert('Enter country');
}
getCountryData(enterCountry);
btn.value = '';
}
});
btn.addEventListener('click', function () {
countriesContainer.style.opacity = 0;
});
// window.location.reload(true);