-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
109 lines (100 loc) · 2.68 KB
/
main.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
const filter_switch = document.querySelector(".filter_select");
const filter_option_box = document.querySelector(".filter_options");
const filter_options = document.querySelectorAll(".filter_options .option");
filter_switch.addEventListener("click", () => {
filter_option_box.classList.toggle("hidden");
});
filter_options.forEach((item) => {
item.addEventListener("click", () => {
if (item.innerText == "None") {
filter_switch.innerText = "Filter by Region";
} else {
filter_switch.innerText = item.innerText;
}
filter_option_box.classList.toggle("hidden");
search_and_filter();
});
});
const container = document.querySelector("#country_data_container");
const search_input = document.querySelector("#search_input");
search_input.addEventListener("input", (e) => {
e.preventDefault();
search_and_filter();
});
function search_and_filter() {
if (filter_switch.innerText == "Filter by Region") {
renderData(search_input.value);
} else {
renderData(search_input.value, filter_switch.innerText);
}
}
let data = JSON.parse(localStorage.getItem("REST_Country_api_data_V3.1")) || "";
/*
*
*
*
//depreceted
*
*
*
*/
if (data == "") {
fetch("https://restcountries.com/v3.1/all")
.then((res) => {
return res.json();
})
.then((res) => {
data = res;
localStorage.setItem("REST_Country_api_data_V3.1", JSON.stringify(data));
renderData();
})
.catch((err) => {
console.log(err);
container.innerHTML = "<h3>NO DATA FOUND</h3>";
});
} else {
renderData();
}
function renderData(term = "", term2 = "") {
container.innerHTML = "";
tempdata = data.filter((item) => {
item = Object.values(item);
item = JSON.stringify(item).toLocaleLowerCase();
return (
item.includes(term.toLocaleLowerCase()) &&
item.includes(term2.toLocaleLowerCase())
);
});
//
tempdata.forEach((item, index) => {
if (index < 20) {
console.log(item);
container.innerHTML += `
<a class="card" href="https://amishranpariya.github.io/Country_details/About/country.html?cc=${
item.cca3
}">
<div class="upper">
<img src="${item.flags.svg}" alt="flag">
</div>
<div class="lower">
<h3>${item.name.common}</h3>
<p>
<span class="label" >Population: </span>
<span class="label_value" >${item.population.toLocaleString(
"en-US"
)}</span>
</p>
<p>
<span class="label" >Region: </span>
<span class="label_value" >${item.region}</span>
</p>
<p>
<span class="label" >Capital: </span>
<span class="label_value" >${item.capital}</span>
</p>
</div>
</a>
`;
}
});
}