-
Notifications
You must be signed in to change notification settings - Fork 0
/
app2.js
115 lines (93 loc) · 3.06 KB
/
app2.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
const input = document.querySelector('.inp');
const button = document.querySelector('.btn');
const error1 = document.querySelector('.error');
const select1 = document.querySelector('.select1');
async function fetchCountry1() {
const url1 = "https://restcountries.com/v3.1/all"
try {
const res = await fetch(url1);
if(!res.ok){
throw new Error(`${res.status} has been returned`);
}
const data = await res.json();
data.forEach(element => {
const {name : {common}} = element
select1.innerHTML += `<option value="${common}">${common}</option>`
})
}
catch (error) {
console.log(error);
}
}
fetchCountry1();
select1.addEventListener("click" ,(e) => {
fetchCountry(e.target.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]);
select1.value = "";
} catch (error) {
console.log(error);
}
};
const renderError = (err) => {
error1.innerHTML = `
<h1 class="text-danger">${err}</h1>
<img src="./img/404.png" alt="" />
`;
};
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>
`;
};