-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (135 loc) · 4.28 KB
/
script.js
File metadata and controls
147 lines (135 loc) · 4.28 KB
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
'use strict';
const btn = document.querySelector('.btn-country');
const countriesContainer = document.querySelector('.countries');
const endpoint = 'https://restcountries.com/v3.1/';
const title = str =>
str
.split(' ')
.map(w => w[0].toUpperCase() + w.slice(1).toLowerCase())
.join(' ');
const geocodeEndpoint = (lat = 0, lng = 0) =>
`https://geocode.xyz/${lat},${lng}?geoit=json`;
const getPosition = () => {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(resolve, reject);
});
};
const getJSON = async function (url, errorMsg = 'Something went wrong') {
const response = await fetch(url);
if (!response.ok) throw new Error(`${errorMsg} (${response.status})`);
return response.json();
};
const renderCountry = function (country, isNeighbor) {
const html = `
<article class="country${isNeighbor ? ' neighbour' : ''}">
<img class="country__img" src="${country.flags.png}" />
<div class="country__data">
<h3 class="country__name">${country.name.common}</h3>
<h4 class="country__region">${country.region}</h4>
<p class="country__row"><span>👫</span>${(
country.population / 1_000_000
).toFixed(1)} people</p>
<p class="country__row"><span>🗣️</span>${
Object.values(country.languages)[0]
}</p>
<p class="country__row"><span>💰</span>${
Object.values(country.currencies)[0].name
}</p>
</div>
</article>
`;
countriesContainer.insertAdjacentHTML('beforeend', html);
};
const renderError = function (msg) {
countriesContainer.insertAdjacentHTML(
'beforeend',
`<div style="text-align: center"><h3>😣 Something went wrong! 😣</h3><p>${msg}<p/></div>`
);
};
const countryErrorMessage = c => `Country ${c} not found.`;
/*
// AJAX
const getCountryRequest = function (url, callback = null) {
const request = new XMLHttpRequest();
request.open('GET', url);
request.send();
request.addEventListener('load', function () {
const [data] = JSON.parse(this.responseText);
console.log(data);
if (callback) return callback(data);
return data;
});
};
const getCountryAndNeighborData = function (country) {
getCountryRequest(`${endpoint}name/${country}`, function (data) {
renderCountry(data);
const [neighbor] = data.borders;
if (!neighbor) return;
return getCountryRequest(
`${endpoint}alpha/${neighbor.toLowerCase()}`,
function (data2) {
renderCountry(data2, true);
}
);
});
};
*/
/*
// Fetch API
const getCountryAndNeighborData = () => {
getPosition()
.then(pos => {
const { latitude: lat, longitude: lng } = pos.coords;
return getJSON(geocodeEndpoint(lat, lng), 'Problem with geocoding');
})
.then(pos => pos.country)
.then(c => getJSON(`${endpoint}name/${c}`, countryErrorMessage(c)))
.then(data => {
renderCountry(data[0]);
const neighbors = data[0].borders;
if (!neighbors) throw new Error('No neighbor found!');
return getJSON(
`${endpoint}alpha/${neighbors[0]}`,
countryErrorMessage(neighbors[0])
);
})
.then(data2 => renderCountry(data2[0], true))
.catch(err => (console.error(err), renderError(err.message)))
.finally(() => (countriesContainer.style.opacity = 1));
};
*/
// async/await
const getCountryAndNeighborData = async () => {
try {
const pos = await getPosition();
const { latitude: lat, longitude: lng } = pos.coords;
const { country } = await getJSON(
geocodeEndpoint(lat, lng),
'Problem with geocoding'
);
const data = await getJSON(
`${endpoint}name/${country}`,
countryErrorMessage(country)
);
renderCountry(data[0]);
const neighbors = data[0].borders;
if (!neighbors) throw new Error('No neighbor found!');
const [neighbor] = await getJSON(
`${endpoint}alpha/${neighbors[0]}`,
countryErrorMessage(neighbors[0])
);
renderCountry(neighbor, true);
} catch (err) {
console.error(err);
renderError(err.message);
return err;
}
countriesContainer.style.opacity = 1;
};
btn.addEventListener('click', function () {
countriesContainer.innerHTML = '';
countriesContainer.style.opacity = 0;
(async function () {
await getCountryAndNeighborData();
})();
});