-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
97 lines (86 loc) · 3.2 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
88
89
90
91
92
93
94
95
96
97
const btn = document.querySelector('.btn-country');
const countriesContainer = document.querySelector(".countries");
const renderError = function (msg) {
countriesContainer.insertAdjacentText("beforeend", msg);
};
// Consumeing promises
// chaining promises
// Handling Rejected Promises
// Throwing Errors Manually
const getJSON = (url, errorMsg= 'Something went wrong') => {
return fetch(url)
.then(response => {
//if response.ok is false, undefined location in api list throw error message
if(!response.ok) throw new Error(`${errorMsg} (${response.status})`);
return response.json();
});
};
const renderCountry = function (data, className = "") {
const 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)} 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);
};
const getCountryData = function (country) {
getJSON(`https://countries-api-836d.onrender.com/countries/name/${country}`, 'Country not found')
.then(function (data) {
renderCountry(data[0]);
const neighbour = data[0].borders[0];
//if country has no neighbouring country throw error message
if (!neighbour) throw new Error('No neighbour found!');
return getJSON(
`https://countries-api-836d.onrender.com/countries/alpha/${neighbour}`, 'Country not found'
);
})
.then(function (data) {
renderCountry(data, "neighbour");
})
.catch(err => {
//Handling offline network connection
renderError(`Something went wrong 💥💥 ${err.message}. Try again!`)
})
.finally(() => {
countriesContainer.style.opacity = 1;
});
};
btn.addEventListener('click', function () {
var inputValue = document.getElementById('inputField').value;
getCountryData(inputValue);
});
// const getCountryData = function (country) {
// fetch(`https://countries-api-836d.onrender.com/countries/name/${country}`)
// .then(function (response) {
// return response.json();
// })
// .then(function (data) {
// renderCountry(data[0]);
// const neighbour = data[0].borders[0];
// if (!neighbour) return;
// return fetch(
// `https://countries-api-836d.onrender.com/countries/alpha/${neighbour}`
// );
// })
// .then(function (response) {
// return response.json();
// })
// .then(function (data) {
// renderCountry(data, "neighbour");
// })
// .catch(err => {
// renderError(`Something went wrong 💥💥 ${err.message}. Try again!`)
// })
// .finally(() => {
// countriesContainer.style.opacity = 1;
// });
// };