-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
273 lines (242 loc) · 7.96 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
'use strict';
const showLocationBtn = document.querySelector('.btn-location');
const main = document.querySelector('main');
const btnContainer = document.querySelector('.btn-container');
// *** FUNCTIONS ***
const geolocation = () => {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => resolve(position),
() =>
reject(
new Error(
"Sorry, we couldn't get your location. This happens if you have blocked the location access! 😞"
)
)
);
});
};
const getJSON = (
url,
errorMsg = 'Failed to get your location from the server.. 😞'
) => {
return fetch(url).then((res) => {
if (!res.ok) throw new Error(errorMsg);
return res.json();
});
};
const populationNumFormatter = (pop) => {
if (Math.abs(pop) >= 1000000000) {
return Math.sign(pop) * (Math.abs(pop) / 1000000000).toFixed(1) + 'B';
}
if (Math.abs(pop) >= 1000000) {
return Math.sign(pop) * (Math.abs(pop) / 1000000).toFixed(1) + 'M';
}
if (Math.abs(pop) > 999) {
return Math.sign(pop) * (Math.abs(pop) / 1000).toFixed(1) + 'K';
}
return Math.sign(pop) * Math.abs(pop);
};
const renderLocation = (data, location) => {
const html = `
<div class="container">
<article class="location">
<div class="location-current center-align">
<h3>Your Current Location:</h3>
<h4>🚩 <em>${location}</em></h4>
</div>
<div class="row">
<div class="col s12 m6 offset-m3 xl4 offset-xl4">
<div class="card">
<div class="card-image">
<img
src="${data[0].flags.png}"
alt="country flag"
/>
</div>
<div class="card-content">
<span class="card-title">${data[0].name.common}</span>
<p class="grey-text region">${data[0].region}</p>
<p><span class="card-icons">👫</span>${populationNumFormatter(
data[0].population
)} people</p>
<p><span class="card-icons">🗣️</span>${
Object.values(data[0].languages)[0]
}</p>
<p><span class="card-icons">💰</span>${
Object.values(data[0].currencies)[0].name
}</p>
<p><span class="card-icons">🏫</span>${data[0].capital[0]}</p>
</div>
<div class="card-action center-align">
<button
class="waves-effect waves-light btn green btn-neighbours"
>
Show Neighbours
</button>
</div>
</div>
</div>
</div>
</article>
</div>
`;
main.insertAdjacentHTML('beforeend', html);
};
const renderNeighbours = (country = []) => {
if (country.length === 0) {
return main.insertAdjacentHTML(
'beforeend',
`
<div class="container neighbours-container">
<h5 class="center-align">This country has no neighbours!</h5>
</div>
`
);
}
const neighboursContainer = document.createElement('div');
neighboursContainer.classList.add('container', 'neighbours-container');
const neighboursHtml = `
<article class="neighbours">
<h5 class="center-align">Neighbours:</h5>
<div class="row neighbours__grid">
</div>
</article>
`;
neighboursContainer.insertAdjacentHTML('afterbegin', neighboursHtml);
const neighboursGrid = neighboursContainer.querySelector('.neighbours__grid');
country.forEach((data) => {
const html = `
<div class="col s12 m6 l4">
<div class="card">
<div class="card-image">
<img
src="${data[0].flags.png}"
alt="country flag"
/>
</div>
<div class="card-content">
<span class="card-title">${data[0].name.common}</span>
<p class="grey-text region">${data[0].region}</p>
<p><span class="card-icons">👫</span>${populationNumFormatter(
data[0].population
)} people</p>
<p><span class="card-icons">🗣️</span>${
Object.values(data[0].languages)[0]
}</p>
<p><span class="card-icons">💰</span>${
Object.values(data[0].currencies)[0].name
}</p>
<p><span class="card-icons">🏫</span>${data[0].capital[0]}</p>
</div>
</div>
</div>
`;
neighboursGrid.insertAdjacentHTML('beforeend', html);
});
main.insertAdjacentElement('beforeend', neighboursContainer);
};
const renderError = (err) => {
const html = `
<div class="error center-align">
<div class="red lighten-2 error-box center-align">
<p class="white-text flow-text">
${err.message}
</p>
</div>
<a href="index.html" class="waves-effect waves-light btn-large green btn-try-again">
<i class="material-icons left">rocket</i>HOME PAGE
</a>
</div>
`;
main.insertAdjacentHTML('beforeend', html);
};
const hideShowLocationBtn = () => btnContainer.classList.add('hidden');
// const whereAmI = () => {
// let exactLocation;
// const errorMsg = 'Failed to get your location from the server.. 😞';
// geolocation()
// .then((pos) => {
// const { latitude: lat, longitude: lon } = pos.coords;
// return getJSON(
// `https://api.geoapify.com/v1/geocode/reverse?lat=${lat}&lon=${lon}&apiKey=4956001598b44d1d86e2bf247340924e`,
// errorMsg
// );
// })
// .then((data) => {
// exactLocation = `${data.features[0].properties.city}, ${data.features[0].properties.country}`;
// return getJSON(
// `https://restcountries.com/v3.1/alpha/${data.features[0].properties.country_code}`,
// errorMsg
// );
// })
// .then((data) => {
// console.log(data);
// main.textContent = '';
// hideShowLocationBtn();
// renderLocation(data, exactLocation);
// })
// .catch((err) => {
// console.error(err);
// main.textContent = '';
// hideShowLocationBtn();
// renderError(err);
// });
// };
// whereAmI();
let borders = [];
const whereAmI = async () => {
try {
const position = await geolocation();
const { latitude: lat, longitude: lon } = position.coords;
const currLocation = await fetch(
`https://api.geoapify.com/v1/geocode/reverse?lat=${lat}&lon=${lon}&apiKey=4956001598b44d1d86e2bf247340924e`
);
if (!currLocation.ok) throw new Error(errorMsg);
const currLocationRes = await currLocation.json();
const exactLocation = `${currLocationRes.features[0].properties.city}, ${currLocationRes.features[0].properties.country}`;
const country = await fetch(
`https://restcountries.com/v3.1/alpha/${currLocationRes.features[0].properties.country_code}`
);
if (!country.ok) throw new Error(errorMsg);
const countryRes = await country.json();
console.log(countryRes);
if (countryRes[0].borders?.length > 0) borders = [...countryRes[0].borders];
main.textContent = '';
hideShowLocationBtn();
renderLocation(countryRes, exactLocation);
} catch (err) {
console.error(err);
main.textContent = '';
hideShowLocationBtn();
renderError(err);
}
};
const showNeighbours = async () => {
try {
const neighbours = borders.map(
async (border) =>
await getJSON(
`https://restcountries.com/v3.1/alpha/${border}`,
'Failed retrieving the neighbours, sorry! 😔'
)
);
const neighboursRes = await Promise.all(neighbours);
renderNeighbours(neighboursRes);
} catch (err) {
console.error(err);
main.textContent = '';
renderError(err);
}
};
// *** EVENTS ***
let neighboursBtnClickCounter = 0;
showLocationBtn.addEventListener('click', whereAmI);
main.addEventListener('click', (e) => {
if (e.target.classList.contains('btn-try-again')) return whereAmI();
if (e.target.classList.contains('btn-neighbours')) {
if (neighboursBtnClickCounter > 0) return;
neighboursBtnClickCounter++;
return showNeighbours();
}
});