-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
99 lines (82 loc) · 2.58 KB
/
app.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
//*=========================================================
//* FLAG-APP
//*=========================================================
const input = document.querySelector('.inp');
const button = document.querySelector('.btn');
const error1 = document.querySelector('.error');
button.addEventListener("click" ,() => {
fetchCountry(input.value);
})
input.addEventListener("keydown", (e)=> {
if(e.keyCode === 13){
fetchCountry(input.value);
}
})
window.addEventListener("load", () => {
input.focus()
})
const fetchCountry = async (name) => {
const url = `https://restcountries.com/v3.1/name/${name}`;
try {
const res = await fetch(url);
if (!res.ok) {
renderError(`Something went wrong:${res.status}`)
setTimeout(() => {
error1.textContent = "";
}, 3000);
throw new Error();
}
const data = await res.json();
renderCountry(data[0]);
input.value = "";
} catch (error) {
console.log(error);
}
};
const renderError = (err) => {
error1.innerHTML = `
<h1 class="text-danger">${err}</h1>
<img src="./img/404.png" alt="" />
`;
input.value = "";
input.focus()
};
const renderCountry = (country) => {
console.log(country);
const countriesDiv = document.querySelector('.countries');
//!destr
const {
capital,
name: { common },
region,
flags: { svg },
languages,
currencies,
} = country;
// console.log(capital, common, region, svg);
// console.log(Object.values(languages));
// console.log(Object.values(currencies)[0].name);
// console.log(Object.values(currencies)[0].symbol);
countriesDiv.innerHTML += `
<div class="card shadow-lg" style="width: 18rem;">
<img src="${svg}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">${common}</h5>
<p class="card-text">${region}</p>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item"> <i class="fas fa-lg fa-landmark"></i> ${capital}</li>
<li class="list-group-item"> <i class="fas fa-lg fa-comments"></i> ${Object.values(
languages
)}</li>
<li class="list-group-item"> <i class="fas fa-lg fa-money-bill-wave"></i> ${
Object.values(currencies)[0].name
}, ${Object.values(currencies)[0].symbol} </li>
</ul>
</div>
`;
};
// fetchCountry('turkey');
// fetchCountry('usa');
// fetchCountry('belgium');
// fetchCountry('south africa');