-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
178 lines (148 loc) · 4.67 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
"use strict";
class CountriesDOM {
constructor() {
this.utils = new CountriesUtils();
}
addBtnListener(btnElement, onClickAsync) {
const that = this;
btnElement.addEventListener("click", async () => {
that.clearContent();
that.renderLoader();
btnElement.disabled = true;
try {
await onClickAsync();
} catch (error) {
this.renderError(error.message);
} finally {
this.selectors().countriesContainer.style.opacity = 1;
}
that.removeLoader();
btnElement.disabled = false;
});
}
renderCountry(country) {
const languageCode =
this.utils.langCodeMapper.get(country.name.common) ?? undefined;
const html = `
<article class="country">
<img class="country__img" src="${country.flags.svg}" />
<div class="country__data">
<h3 class="country__name">${country.name.common}</h3>
<h4 class="country__region">${country.region}</h4>
<p class="country__info"><span>🧑🤝🧑</span>${this.utils.formatPopulation(
country.population
)}</p>
<p class="country__info"><span>🗣️</span>${
country.languages[languageCode] ?? "Not defined"
}</p>
<p class="country__info"><span>🪙</span>${
country.currencies.EUR.name
}</p>
</div>
</article>`;
this.selectors().countriesContainer.insertAdjacentHTML("beforeend", html);
}
renderError(message) {
const html = `<p class="error-text">${message}</p>`;
this.selectors().countriesContainer.insertAdjacentHTML("beforeend", html);
}
renderLoader() {
const html = `<div class="loader"></div>`;
this.selectors().mainContainer.insertAdjacentHTML("beforeend", html);
}
removeLoader() {
document.querySelector(".loader").remove();
}
clearContent() {
this.selectors().countriesContainer.innerHTML = "";
this.selectors().countriesContainer.style.opacity = 0;
}
selectors() {
return {
mainContainer: document.querySelector(".main"),
countriesContainer: document.querySelector(".countries"),
countriesBtn: document.querySelector(".btn-country"),
currentCountryBtn: document.querySelector(".btn-whereami"),
};
}
}
class CountriesUtils {
langCodeMapper = new Map([
["Portugal", "por"],
["France", "fra"],
["Germany", "deu"],
["Spain", "spa"],
["Greece", "ell"],
]);
formatPopulation(population) {
if (typeof population !== "number") return;
return `${(population / 1000000).toFixed(1)} M`;
}
}
class CountriesService {
mainCountriesUrl = "https://restcountries.com/v3.1/alpha";
mainCountryUrl = "https://restcountries.com/v3.1/name";
async fetchCountries({ codeOne, codeTwo, codeThree, codeFour }) {
const response = await fetch(
`${this.mainCountriesUrl}/?codes=${codeOne},${codeTwo},${codeThree},${codeFour}`
);
const data = await response.json();
return data;
}
async fetchCountry(countryName) {
const response = await fetch(`${this.mainCountryUrl}/${countryName}`);
if (response.ok) {
const [data] = await response.json();
return data;
}
const data = await response.json();
if (response.status === 404) throw new Error(data.message);
return data;
}
}
class GeolocationService {
constructor() {
this.run();
}
currentCountry = "";
run() {
navigator.geolocation.getCurrentPosition(
async (position) => {
const currentCountry = await this.fetch(
position.coords.latitude,
position.coords.longitude
);
this.currentCountry = currentCountry;
},
() => (this.currentCountry = undefined)
);
}
mainUrl = "https://geocode.xyz";
async fetch(lat, long) {
const response = await fetch(`${this.mainUrl}/${lat},${long}?geoit=json`);
if (response.ok) {
const data = await response.json();
return data.country;
}
}
}
const geoLocator = new GeolocationService();
const domMutator = new CountriesDOM();
const service = new CountriesService();
const countriesBtn = domMutator.selectors().countriesBtn;
const onClickCountries = async () => {
const data = await service.fetchCountries({
codeOne: "PT",
codeTwo: "ES",
codeThree: "FR",
codeFour: "DE",
});
data.map((c) => domMutator.renderCountry(c));
};
domMutator.addBtnListener(countriesBtn, onClickCountries);
const currentCountryBtn = domMutator.selectors().currentCountryBtn;
const onClickCurrentCountry = async () => {
const data = await service.fetchCountry(geoLocator.currentCountry);
domMutator.renderCountry(data);
};
domMutator.addBtnListener(currentCountryBtn, onClickCurrentCountry);