-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
277 lines (250 loc) · 9.99 KB
/
ui.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
274
275
276
const http = new EasyHTTP();
class UI {
changeToDarkMode() {
if (
localStorage.theme === "dark" ||
(!("theme" in localStorage) &&
window.matchMedia("(prefers-color-scheme: dark)").matches)
) {
document.documentElement.classList.toggle("dark");
} else {
localStorage.setItem("theme", "dark");
document.documentElement.classList.toggle("dark");
}
}
checkStorage(key) {
if (!localStorage.getItem(key)) {
return false;
}
return localStorage.getItem(key);
}
store(key, value) {
const hide = btoa(value);
localStorage.setItem(key, hide);
}
get(key) {
if (this.checkStorage() == true) {
this.store(key, "");
return;
}
const value = localStorage.getItem(key);
const reveal = atob(value);
return reveal;
}
displayOptions(e) {
const filterList = document.querySelector("#filter-list");
if (!e.target.classList.contains("result")) {
filterList.classList.toggle("hidden");
}
}
displayFilteredItem(e) {
const filterContainer = document.querySelector("#filter-container");
const filterList = document.querySelector("#filter-list");
let result;
if (!document.querySelector("#result")) {
result = document.createElement("div");
result.id = "result";
result.innerHTML = `
<div class="result bg-gray-400 text-sm rounded-full px-2 flex justify-between items-center">
<p class="result">${e.target.textContent}</p>
<span id="cancel-filter-btn" class="result p-1 cursor-pointer font-bold">X</span>
</div>
`;
filterContainer.append(result);
filterList.classList.add("hidden");
}
result = document.querySelector("#result");
result.children[0].children[0].textContent = e.target.textContent;
filterList.classList.add("hidden");
}
displayLoading(selector) {
const loading = `
<div class="col-span-4 flex flex-col justify-center items-center gap-y-2">
<img src="./img/loading-buffering.gif" alt="" class="w-32 h-32">
<p class="">Loading</p>
</div>
`;
selector.innerHTML = loading;
}
displayError(err, selector) {
const loading = `
<div class="col-span-4 border-2 flex flex-col justify-center items-center gap-y-2">
<h2 class="text-3xl text-red-700 font:bold dark:text-red-300">Error</h2>
<p class="">${err.message}</p>
</div>
`;
selector.innerHTML = loading;
}
getCountries() {
const countries = [];
const countriesNum = [];
this.displayLoading(document.querySelector("#countries-container"));
http.get("https://restcountries.com/v2/all").then((res) => {
let i = 0;
while (i < 12) {
const random = Math.floor(Math.random() * res.length);
if (!countriesNum.includes(random)) {
countriesNum.push(random);
countries.push(res[random]);
i++;
}
}
this.renderCountryToUI(
countries,
document.querySelector("#countries-container"),
'official'
);
});
}
getCountriesByRegion(e) {
if (e.target.classList.contains("filter-options")) {
const search = document.querySelector("#search-input");
search.value = "";
this.displayLoading(document.querySelector("#countries-container"));
const text = e.target.textContent;
http
.get(`https://restcountries.com/v3.1/subregion/${text.toLowerCase()}`)
.then((res, err) => {
if (res.status !== 404) {
this.renderCountryToUI(
res,
document.querySelector("#countries-container"),
"official"
);
} else {
this.displayError(res, document.querySelector('#countries-container'));
}
})
.catch((err) => {
this.displayError(err, document.querySelector('#countries-container'));
});
}
}
getCountryDetails(e) {
this.displayLoading(document.querySelector("#country-details"));
const value = this.get("restCountries");
http
.get(`https://restcountries.com/v3.1/name/${value}?fullText=true`)
.then((res, err) => {
if(this.status !== 404){
this.renderCountryDetailsToUI(res, document.querySelector('#country-details'));
} else {
this.displayError(err, this.displayError(res, document.querySelector('#country-details')))
}
}).catch(err => {
this.displayError(err, document.querySelector('#country-details'))
})
}
linkToCountryPage(e) {
e.preventDefault();
if (e.target.attributes.getNamedItem("country-name")) {
const name = e.target.attributes.getNamedItem("country-name").value;
this.store("restCountries", name);
window.location = "country.html";
}
}
searchForACountry() {
const search = document.querySelector("#search-input");
http
.get(`https://restcountries.com/v3.1/name/${search.value}`)
.then((res, err) => {
this.displayLoading(document.querySelector("#countries-container"));
this.renderCountryToUI(
res,
document.querySelector("#countries-container"),
"common"
);
});
}
renderCountryToUI(items, selector, common) {
selector.innerHTML = "";
if (items.length < 1) {
return;
}
items.forEach((item) => {
const post = `
<a country-name = "${
item.name[common] || item.name
}" class="max-w-xs shadow-md cursor-pointer rounded-sm overflow-hidden grid grid-rows-2 light-mode mode bg-white text-gray-700 dark:text-white dark:bg-gray-800">
<div country-name = "${
item.name[common] || item.name
}" class="border-b-2 dark:border-b-0 cursor-pointer">
<img src=${item.flags.png} country-name = "${
item.name[common] || item.name
}" class=" w-full h-[185px] object-container cursor-pointer object-center">
</div>
<div country-name = "${
item.name[common] || item.name
}" class="px-6 pt-8 overflow-hidden cursor-pointer">
<h2 country-name = "${
item.name[common] || item.name
}" class="text-xl font-bold text-ellipsis overflow-hidden whitespace-nowrap cursor-pointer">${
item.name[common] || item.name
}</h2>
<div country-name = "${
item.name[common] || item.name
}" class="mt-2 space-y-1.5 cursor-pointer">
<p country-name = "${
item.name[common] || item.name
}" class="font-[800] cursor-pointer">Population: <span class="font-[400] ml-1">${
item.population
}</span></p>
<p country-name = "${
item.name[common] || item.name
}" class="font-[800]">Region: <span class="font-[400] ml-1 cursor-pointer">${
item.region
}</span></p>
<p country-name = "${
item.name[common] || item.name
}" class="font-[800]">Capital: <span class="font-[400] ml-1 cursor-pointer">${
item.capital
}</span></p>
</div>
</div>
</a>
`;
selector.innerHTML += post;
});
}
renderCountryDetailsToUI(array, selector){
const data = array[0];
const details = `
<div class="w-full h-full">
<img src=${data.flags.png} class="w-full h-full object-container object-center" alt="">
</div>
<div class=" ">
<div class="">
<h1 class="text-3xl font-bold">${data.name.common}</h1>
<div class="md:grid grid-cols-2 gap-x-4 items-start">
<ul class="mt-8 space-y-4">
<li class="font-[500] capitalize">Native Name:<span class="font-[400] ml-2">${data.name.nativeName[Object.keys(data.name.nativeName)[Object.keys(data.name.nativeName).length - 1]].official}</span></li>
<li class="font-[500] capitalize">population:<span class="font-[400] ml-2">${data.population}</span></li>
<li class="font-[500] capitalize">region:<span class="font-[400] ml-2">${data.region}</span></li>
<li class="font-[500] capitalize">sub region:<span class="font-[400] ml-2">${data.subregion}</span></li>
<li class="font-[500] capitalize">capital:<span class="font-[400] ml-2">${data.capital}</span></li>
</ul>
<ul class="mt-8 space-y-4">
<li class="font-[500] capitalize">top level domain:<span class="font-[400] ml-2">${data.tld}</span></li>
<li class="font-[500] capitalize">currencies:<span class="font-[400] ml-2">${Object.keys(data.currencies)}</span></li>
<li class="font-[500] capitalize">languages:<span class="font-[400] ml-2">${Object.keys(data.languages)}</span></li>
</ul>
</div>
</div>
<div class="mt-10 md:flex gap-x-5 items-baseline">
<h1 class="text-lg md:text-base font-[500]">Border Countries</h1>
<div class="flex items-center flex-wrap mt-4 gap-2">
${
data.borders ? data.borders.map(item => `<button class="py-1.5 px-4 bg-white dark:bg-gray-700 rounded-lg shadow-xl border-2 dark:border-0 cursor-default">${item}</button>`): 'No borders'
}
</div>
</div>
</div>
`;
selector.innerHTML = details;
}
cancelFilter(e) {
if (e.target.id === "cancel-filter-btn") {
e.target.parentElement.parentElement.remove();
}
}
}