-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaincards.js
169 lines (139 loc) · 5.55 KB
/
maincards.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
//----------------------------------------------------------Create cards-------------------------------------------
function getCountries () {
fetch("https://restcountries.com/v3.1/all")
.then(function(response) {
return response.json();
})
.then(function(result) {
CreateCards(result);
addEventListeners(result);
addEvents();
})
.catch(function(error) {
console.log(error)
alert(error);
})
}
getCountries();
function CreateCards(array) {
const cardsDiv = document.getElementById("cards-container");
cardsDiv.innerHTML = "";
for (let i = 0; i < array.length; i++) {
const card = document.createElement("div");
cardsDiv.classList.add("col", "row-cols-1", "row-cols-md-3", "g-4")
card.classList.add("card", "col-lg");
card.setAttribute("style", "width: 13rem; border-radius: 120px; text-align:center");
cardsDiv.appendChild(card);
const img = document.createElement("img");
img.setAttribute("src", array[i].flags.png);
img.setAttribute("alt", array[i].flags.alt);
img.setAttribute("style", "card-img-top");
const cardBody = document.createElement("div");
cardBody.classList.add("card-body");
card.append(img, cardBody);
const title = document.createElement("h5");
title.classList.add("card-title");
title.innerHTML = array[i].name.common;
const cardcapital = document.createElement("p");
cardcapital.classList.add("card-text");
cardcapital.innerHTML = array[i].capital;
cardBody.append(title, cardcapital);
const button = document.createElement("button");
button.classList.add ("btn-success");
button.classList.add ("btn");
button.innerHTML = "More";
cardBody.append(button);
button.addEventListener("click", function(e) {
// e.stopPropagation();
if ( button.innerHTML === "More" ) {
const list = document.createElement ("ul");
const cardlenguage = document.createElement("li");
const cardpop = document.createElement("li");
cardlenguage.innerHTML = "Population";
cardpop.innerHTML = array[i].population;
list.classlist = ("list-group list-group-flush");
cardlenguage.classList = ("list-group-item");
cardpop.classList = ("list-group-item");
cardlenguage.id =("cardlang");
cardpop.id = ("cardpopo");
const link = document.createElement("a");
link.setAttribute("href", `details.html?name=${array[i].name.common}`);
link.innerHTML = "see more..";
link.id = ("linki");
button.classList.remove("btn-success");
button.classList.add ("btn-danger");
cardBody.append (cardlenguage, cardpop, link);
button.innerHTML = "Less";
} else {
const cardlenguages = document.getElementById ("cardlang");
const cardpopi = document.getElementById("cardpopo");
const link = document.getElementById("linki");
cardlenguages.innerHTML = "";
cardpopi.innerHTML = "";
link.textContent = "";
button.classList.remove("btn-danger");
button.classList.add ("btn-success");
cardBody.append (cardlenguages, cardpopi, link);
button.innerHTML = "More";
}
});
}
}
const addEventListeners = (countries) => {
const checkBoxes = document.querySelectorAll("input[type='checkbox']");
checkBoxes.forEach((checkbox) => {
checkbox.addEventListener("click", () => {
filterByCheckbox(countries)
});
});
};
const filterByCheckbox = (countries) => {
const checkboxes = document.querySelectorAll("input[type='checkbox']:checked");
const checkboxesValues = Array.from(checkboxes).map((checkbox) => {
return checkbox.value;
});
const filteredCountries = countries.filter((country) => {
return (
checkboxesValues.includes(country.region) ||
checkboxesValues.length === 0
);
});
CreateCards(filteredCountries);
};
// const cleanDOM = (city, tbody, astronomyCards) => {
// city.innerHTML = "";
// tbody.innerHTML = "";
// astronomyCards.innerHTML = "";
// document.getElementById("city-search").value = "";
// };
//BUSCAR A TRAVES DEL INPUT TEXT
const addEvents = () => {
let country = "";
const searchInput = document.getElementById("buscador");
searchInput.addEventListener("input", (event) => {
country = event.target.value;
});
searchInput.addEventListener("keydown", (event) => {
console.log("key event", event);
if (event.key === "Enter") {
fetchCountry(country);
// window.location.href = `details.html?name=${country}`
}
});
const buttonsearch = document.getElementById("searchbutton")
buttonsearch.addEventListener("click", (event) => {
fetchCountry(country);
// window.location.href = `details.html?name=${country}`
});
};
const fetchCountry =(countryName)=> {
console.log('countryName :>> ', countryName);
fetch(`https://restcountries.com/v3.1/name/${countryName}?fullText=true`)
.then((res) => res.json())
.then((res) => {
const fetchedCountry = res[0];
console.log('fetchedCountry :>> ', fetchedCountry);
CreateCards(fetchedCountry);
})
.catch((err) => console.log(err));
}