-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
247 lines (238 loc) · 9.37 KB
/
index.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
class CustomerError extends Error {
constructor(message) {
super(message);
this.name = 'CustomerError';
}
}
async function getData(url) {
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
redirect: 'follow',
});
if (!response.ok) {
throw new CustomerError(`Server return bad response code (${response.status} ${response.statusText}) `);
}
return response.json();
}
async function loadCountriesData() {
const countries = await getData('https://restcountries.com/v3.1/all?fields=name&fields=cca3');
return countries.reduce((result, country) => {
result[country.name.common] = country.cca3;
return result;
}, []);
}
const contentCountry = document.getElementById('content_country');
const output = document.getElementById('output');
const searchInput = document.getElementById('search');
const searchWrapper = document.querySelector('.wrapper');
const resultsWrapper = document.querySelector('.results');
async function loadDetailCountry(countryCode) {
const country = await getData(
`https://restcountries.com/v3.1/alpha/${countryCode}?fields=cca3&fields=capital&fields=region&fields=area&fields=population&fields=flags&fields=name`
);
const countryInfo = {};
countryInfo.flag = country.flags.png;
countryInfo.name = country.name.common;
countryInfo.region = country.region;
countryInfo.area = country.area;
countryInfo.population = country.population;
countryInfo.capital = country.capital.reduce((result, capital) => {
result.push(capital);
return result;
}, []);
return countryInfo;
}
function printInformationCountry(country) {
const nameCountry = document.getElementById('name_country');
nameCountry.innerHTML = `<h5>${country.name}</h5>`;
const flagCountry = document.getElementById('flag_country');
flagCountry.src = country.flag;
const capitalCountry = document.getElementById('capital_country');
capitalCountry.innerHTML = `<h5>${country.capital.join(';')}</h5>`;
const areaCountry = document.getElementById('area_country');
areaCountry.innerHTML = `<h5>${country.area}</h5>`;
const populationCountry = document.getElementById('population_country');
populationCountry.innerHTML = `<h5>${country.population}</h5>`;
const regionCountry = document.getElementById('region_country');
regionCountry.innerHTML = `<h5>${country.region}</h5>`;
contentCountry.style.visibility = 'visible';
}
function printHistorySearch(arrCountry) {
for (let i = 0; i < arrCountry.length; i++) {
const country = arrCountry[i];
const nameCountry = document.getElementById(`historycolumn_name_country_${i + 1}`);
nameCountry.innerHTML = `<h5>${country.name}</h5>`;
const flagCountry = document.getElementById(`historycolumn_flag_country_${i + 1}`);
flagCountry.src = country.flag;
}
for (let i = 1; i < 4; i++) {
const rowHistory = document.getElementById(`history_row_${i}`);
if (i <= arrCountry.length) {
rowHistory.style.visibility = 'visible';
} else {
rowHistory.style.visibility = 'hidden';
}
}
}
function setComponentStatus(flag) {
if (flag) {
searchInput.disabled = true;
output.innerHTML = 'Loading…';
contentCountry.style.visibility = 'visible';
} else {
searchInput.disabled = false;
output.innerHTML = '';
contentCountry.style.visibility = 'hidden';
}
}
async function loadDataForCountry(countryCode) {
searchInput.disabled = true;
output.innerHTML = 'Loading…';
contentCountry.style.visibility = 'hidden';
try {
const countryInfo = await loadDetailCountry(countryCode);
localStorage.setItem(countryCode, JSON.stringify(countryInfo));
printInformationCountry(countryInfo);
} catch (err) {
if (err instanceof CustomerError) {
output.innerHTML = `<p style="color:red"> findPath : ${err.message} </p>`;
} else {
output.innerHTML = `<p style="color:red">It seems that you do not have an internet connection or the server is not available ${err.message}</p>`;
}
searchInput.disabled = false;
contentCountry.style.visibility = 'hidden';
return;
}
searchInput.disabled = false;
output.innerHTML = '';
contentCountry.style.visibility = 'visible';
}
function uploadLastSaveCountryes() {
const lastUploadKeyCountryCodes = localStorage.getItem('last_upload_country');
if (lastUploadKeyCountryCodes !== null) {
const arrCodes = JSON.parse(lastUploadKeyCountryCodes);
const lastUploadCountry = [];
for (let i = 0; i < arrCodes.length; i++) {
const cn = localStorage.getItem(arrCodes[i]);
if (cn !== null) {
lastUploadCountry.push(JSON.parse(cn));
}
}
printHistorySearch(lastUploadCountry);
} else {
printHistorySearch([]);
}
}
function updateUploadCountryes(countryCode) {
const lastUploadKeyCountryCodes = localStorage.getItem('last_upload_country');
if (lastUploadKeyCountryCodes !== null) {
const codeArray = JSON.parse(lastUploadKeyCountryCodes);
if (!codeArray.includes(countryCode)) {
codeArray.unshift(countryCode);
localStorage.setItem('last_upload_country', JSON.stringify(codeArray.slice(0, 3)));
} else {
const arr = codeArray.filter((item) => item !== countryCode);
arr.unshift(countryCode);
localStorage.setItem('last_upload_country', JSON.stringify(arr));
}
} else {
const codeArray = [countryCode];
localStorage.setItem('last_upload_country', JSON.stringify(codeArray));
}
}
(async () => {
setComponentStatus(true);
let arrNametoCCA3 = [];
try {
arrNametoCCA3 = await loadCountriesData();
} catch (err) {
if (err instanceof CustomerError) {
output.innerHTML = `<p style="color:red"> loadCountries : ${err.message} </p>`;
} else {
output.innerHTML = `<p style="color:red">It seems that you do not have an internet connection or the server is not available (${err.message})</p>`;
}
searchInput.disabled = false;
contentCountry.style.visibility = 'hidden';
return;
}
setComponentStatus(false);
// load history for last country
uploadLastSaveCountryes();
// key event
searchInput.addEventListener('keyup', () => {
let results = [];
const input = searchInput.value;
const searchable = Object.keys(arrNametoCCA3);
if (input.length) {
results = searchable.filter((item) => {
return item.toLowerCase().includes(input.toLowerCase());
});
}
renderResults(results);
contentCountry.style.visibility = 'hidden';
});
async function serverRequest(countryCode) {
try {
await loadDataForCountry(countryCode);
updateUploadCountryes(countryCode);
uploadLastSaveCountryes();
} catch (err) {
if (err instanceof CustomerError) {
output.innerHTML = `<p style="color:red"> loadCountries : ${err.message} <p>`;
} else {
output.innerHTML = `<p style="color:red">It seems that you do not have an internet connection or the server is not available ${err.message}</p>`;
}
}
}
// choose country
resultsWrapper.addEventListener('click', (e) => {
e = e || window.event;
const text = 'textContent' in document ? 'textContent' : 'innerText';
searchWrapper.classList.remove('show');
if (arrNametoCCA3.hasOwnProperty(e.target[text])) {
const countryCode = arrNametoCCA3[e.target[text]];
const record = localStorage.getItem(countryCode);
searchInput.value = '';
if (record === null) {
serverRequest(countryCode);
} else {
printInformationCountry(JSON.parse(record));
updateUploadCountryes(countryCode);
uploadLastSaveCountryes();
}
} else {
output.innerHTML = `<h4>Some countries do not yet exist on our list(${e.target[text]})</h4>`;
}
});
// show 10 element
function renderResults(results) {
if (!results.length) {
searchWrapper.classList.remove('show');
return;
}
const contentStorage = [];
let content = [];
for (let i = 0; i < results.length; i++) {
const item = results[i];
if (arrNametoCCA3.hasOwnProperty(item)) {
if (localStorage.getItem(arrNametoCCA3[item]) !== null) {
if (contentStorage.length < 5) {
contentStorage.push(`<li class="red-text">${item}</li>`);
}
} else {
content.push(`<li class="green-text">${item}</li>`);
}
}
}
content = content.slice(0, 10 - contentStorage.length);
searchWrapper.classList.add('show');
resultsWrapper.innerHTML = `<ul>${contentStorage.join('')}${content.join('')}</ul>`;
}
// event from storage
window.addEventListener('storage', () => {
uploadLastSaveCountryes();
});
})();