-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
298 lines (286 loc) · 9.38 KB
/
Copy pathscript.js
File metadata and controls
298 lines (286 loc) · 9.38 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const theme_mode = document.querySelector(".theme-mode");
const dark_theme = document.querySelector(".dark-mode");
const light_theme = document.querySelector(".light-mode");
const icon_i = document.querySelector(".arrow");
const drop_down = document.querySelector(".drop-down");
const filter = document.querySelector("#filter");
theme_mode.addEventListener("click", () => {
light_theme.classList.toggle("show");
dark_theme.classList.toggle("show");
document.body.classList.toggle("dark-theme");
});
filter.addEventListener("click", () => {
drop_down.classList.toggle("show");
icon_i.classList.toggle("show");
});
// Fetch Countries API
const getCountriesData = () => {
fetch(`https://restcountries.com/v3.1/all`)
.then((response) => {
if (response.status != 200) {
console.log("Invalid country");
throw Error(response.statusText);
} else {
return response.json();
}
})
.then((data) => {
let output = "";
data.forEach((data) => {
output += `<div class="country">
<img class="flag" src="${
data.flags.png ? data.flags.png : `Flags Not Found`
}" alt="flag" />
<h1 class="name">${
data.name.common ? data.name.common : `Country name Not Found`
}</h1>
<div>
Population:
<span class="population">${
data.population ? data.population : `Population Not Found`
}</span>
</div>
<div>
Region:
<span class="region">${
data.region ? data.region : `Region Not Found`
}</span>
</div>
<div>
Capital:
<span class="capital">${
data.capital ? data.capital : `Capital Not Found`
}</span>
</div>
</div>`;
});
document.querySelector(".countries-card").innerHTML = output;
getDetails();
});
};
document.addEventListener("DOMContentLoaded", getCountriesData);
// Input - Filter by Country Name
const search = document.querySelector(".search-filter");
search.addEventListener("input", filterInput);
function filterInput() {
const inputValue = document.querySelector(".input").value;
fetch(`https://restcountries.com/v3.1/name/${inputValue}`)
.then((response) => {
if (response.status != 200) {
console.log("Invalid Country name");
throw Error(response.statusText);
} else {
return response.json();
}
})
.then((data) => {
let output = "";
data.forEach((data) => {
output += `<div class="country">
<img class="flag" src="${
data.flags.png ? data.flags.png : `Flags Not Found`
}" alt="flag" />
<h1 class="name">${
data.name.common ? data.name.common : `Country name Not Found`
}</h1>
<div>
Population:
<span class="population">${
data.population ? data.population : `Population Not Found`
}</span>
</div>
<div>
Region:
<span class="region">${
data.region ? data.region : `Region Not Found`
}</span>
</div>
<div>
Capital:
<span class="capital">${
data.capital ? data.capital : `Capital Not Found`
}</span>
</div>
</div>`;
});
document.querySelector(".countries-card").innerHTML = output;
filterInput();
getDetails();
});
}
// Filter by Region
const region = document.querySelectorAll(".drop-down li");
region.forEach((regionAccess) => {
regionAccess.addEventListener("click", () => {
const regionName = regionAccess.textContent;
drop_down.classList.toggle("show");
fetch(`https://restcountries.com/v3.1/region/${regionName}`)
.then((response) => {
if (response.status != 200) {
console.log("Invalid Country name");
throw Error(response.statusText);
} else {
return response.json();
}
})
.then((data) => {
let output = "";
data.forEach((data) => {
output += `<div class="country">
<img class="flag" src="${
data.flags.png ? data.flags.png : `Flags Not Found`
}" alt="flag" />
<h1 class="name">${
data.name.common ? data.name.common : `Country name Not Found`
}</h1>
<div>
Population:
<span class="population">${
data.population ? data.population : `Population Not Found`
}</span>
</div>
<div>
Region:
<span class="region">${
data.region ? data.region : `Region Not Found`
}</span>
</div>
<div>
Capital:
<span class="capital">${
data.capital ? data.capital : `Capital Not Found`
}</span>
</div>
</div>`;
});
document.querySelector(".countries-card").innerHTML = output;
getDetails();
});
});
});
const getDetails = () => {
const country = document.querySelectorAll(".country");
country.forEach((country) => {
country.addEventListener("click", () => {
const countryName = country.querySelector(".name").textContent;
fetch(`https://restcountries.com/v3.1/name/${countryName}`)
.then((response) => {
if (response.status != 200) {
console.log("Invalid country");
throw Error(response.statusText);
} else {
return response.json();
}
})
.then((data) => {
createDetails(data);
});
document.querySelector(".countries-details").style.display = "block";
document.querySelector(".countries-card").style.display = "none";
document.querySelector(".countries").style.display = "none";
document.querySelector(".search-filter").style.display = "none";
backButton();
});
});
};
function backButton() {
const back = document.querySelector(".back");
back.addEventListener("click", (e) => {
if (e.target.className == "back-btn") {
document.querySelector(".countries-card").style.display = "grid";
document.querySelector(".countries-card").style.gap = "2rem";
document.querySelector(".countries").style.display = "block";
document.querySelector(".search-filter").style.display = "block";
document.querySelector(".countries-details").style.display = "none";
}
});
}
const borderDetails = () => {
const border = document.querySelectorAll(".border-btns button");
border.forEach((bord) => {
bord.addEventListener("click", () => {
const newBorder = bord.textContent;
fetch(`
https://restcountries.com/v3.1/alpha/${newBorder}`)
.then((response) => {
if (response.status != 200) {
console.log("Invalid country");
throw Error(response.statusText);
} else {
return response.json();
}
})
.then((data) => {
createDetails(data);
});
document.querySelector(".countries-details").style.display = "block";
document.querySelector(".countries-card").style.display = "none";
document.querySelector(".countries").style.display = "none";
document.querySelector(".search-filter").style.display = "none";
backButton();
});
});
};
function createDetails(data) {
let output = "";
data.forEach((data) => {
console.log(data);
let currency = "";
let languages = "";
let border = "";
data.borders.forEach((bord) => {
border += `<button>${bord}</button>`;
});
for (let key in data.currencies) {
currency += data.currencies[key].name;
}
for (let key in data.languages) {
languages += " " + data.languages[key];
}
output += `<button class="back-btn">
<i class="fa-solid fa-arrow-left-long"></i> Back
</button>
<div class="wrap">
<img class="country-flag" style="box-shadow: 0 0 2px hsl(209, 23%, 22%);" src="${
data.flags.png ? data.flags.png : `Flags Not Found`
}" alt="flag" />
<div class="row">
<ul class="row-one">
<h1 class="country-name">${
data.name.common ? data.name.common : `Country name Not Found`
}</h1>
<li>Native Name: <span class="nativeName">${
data.name.official ? data.name.official : `Native name Not Found`
}</span></li>
<li>Population: <span class="population">${
data.population ? data.population : `Population Not Found`
}</span></li>
<li>Region: <span class="region">${
data.region ? data.region : `Region Not Found`
}</span></li>
<li>Sub Region: <span class="subregion">${
data.subregion ? data.subregion : `Sub region Not Found`
}</span></li>
<li>Capital: <span class="capital">${
data.capital ? data.capital : `Capital Not Found`
}</span></li>
</ul>
<ul class="row-two">
<li>Top Level Domain: <span class="tld">${
data.tld ? data.tld : `Top level domain Not Found`
}</span></li>
<li>Currencies: <span class="currencies">${currency}</span></li>
<li>Languages: <span class="language">${languages}</span></li>
</ul>
<div class="border">
<h3>Border Countries:</h3>
<div class="border-btns">
${border}
</div>
</div>
</div>
</div>`;
});
document.querySelector(".countries-details").innerHTML = output;
borderDetails();
}