-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
126 lines (120 loc) · 4.87 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const search = document.getElementById('search');
const option = document.getElementsByClassName('option')
const countryName = document.getElementById('country-name');
const alertText = document.getElementById('alert');
const population = document.getElementById('population');
const area = document.getElementById('area');
const currency = document.getElementById('currency');
const language = document.getElementById('language');
const flag = document.getElementById('flag');
const coatOfArms = document.getElementById('coatOfArms');
const neighboursList = document.getElementById('neighbours-list');
const loading = document.getElementById("loading");
const loadingImages = () => {
if (flag.complete && coatOfArms.complete) {
loading.style.display = "none";
} else {
loading.style.display = "block";
setTimeout(loadingImages, 100);
}
}
fetch(`https://restcountries.com/v3.1/all`)
.then((response) => {
return response.json();
})
.then((data) => {
data.sort(function (a, b) {
let textA = a.name.common;
let textB = b.name.common;
return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
});
data.forEach((n) => {
const opt = document.createElement('option');
opt.textContent = n.name.common;
opt.value = n.cca2;
search.appendChild(opt);
})
return data;
});
search.onchange = () => {
loading.style.display = "block";
const selectedCountry = search.value;
fetch(`https://restcountries.com/v3.1/alpha?codes=${selectedCountry}`)
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
if (data.status === 404) {
let e = new Error('Negalime pasiekti informacijos apie šalį.');
e.name = 'nerastaSalis';
throw e;
}
population.textContent = data[0].population.toLocaleString() || '-';
area.textContent = data[0].area.toLocaleString() || '-';
flag.src = data[0].flags.png || '-';
coatOfArms.src = data[0].coatOfArms.png || '-';
countryName.textContent = data[0].name.common;
language.textContent = data[0].languages ? Object.values(data[0].languages).join(', ') : '-';
currency.textContent = data[0].currencies ? Object.values(data[0].currencies).map((c) => c.name).join(', ') : '-';
const borders = data[0].borders;
return borders;
})
.then((borders) => {
if (!borders) {
const li = document.createElement('li');
li.textContent = 'Kaimynų neturi.';
li.className = 'neighbour';
neighboursList.appendChild(li);
loading.style.display = "none";
return;
}
fetch(`https://restcountries.com/v3.1/alpha?codes=${borders.join(',')}`)
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
if (data.status === 400) {
let e = new Error('Negalime pasiekti informacijos apie kaimynus.');
e.name = 'nerastiKaimynai';
throw e;
}
data.forEach((c) => {
const li = document.createElement('li');
li.textContent = `${c.name.common} (${c.population.toLocaleString()}).`;
li.className = 'neighbour';
neighboursList.appendChild(li);
})
setTimeout(loadingImages, 100);
})
.catch((e) => {
loading.style.display = "none";
if (e.name == 'nerastiKaimynai') {
const li = document.createElement('li');
li.textContent = `Įvyko klaida ieškant kaimynų.`;
li.className = 'neighbour';
neighboursList.appendChild(li);
}
alertText.style.display = 'block';
})
})
.catch((e) => {
loading.style.display = "none";
if (e.name == "Not") {
alertText.innerHTML = `Klaida, serveris neveikia arba nėra interneto.`;
} else if (e.name == 'nerastaSalis') {
alertText.innerHTML = `Klaida ieškant šalies.`;
}
alertText.style.display = 'block';
})
search.value = '';
neighboursList.innerHTML = '';
population.textContent = '';
area.textContent = '';
currency.textContent = '';
language.textContent = '';
flag.src = '';
coatOfArms.src = '';
countryName.textContent = '';
};