Spoiler for JS:
const searchInput = document.getElementById("country-name");
const searchButton = document.getElementById("search-country");
const countries = document.getElementsByClassName("countries-container")[0];
const cardElement = document.getElementsByClassName("card")[0];
function getData(countryName) {
fetch(`https://restcountries.com/v3.1/name/${countryName}`)
.then(function (response) {
console.log(response);
console.log(typeof response);
if (!response.ok)
throw new Error(`Country not found, status:${response.status}`);
return response.json();
})
.then(function (result) {
console.log(result[0]);
renderCountry(result[0]);
const neigbourCounty = result[0].borders ? result[0].borders[0] : null;
console.log(neigbourCounty);
if (!neigbourCounty) throw new Error("Neighbour country do not exist! ");
return fetch(`https://restcountries.com/v3.1/alpha/${neigbourCounty}`);
})
.then(function (resolve) {
return resolve.json();
})
.then(function (data) {
console.log(data[0]);
renderCountry(data[0], "card-neigbour");
})
.catch(function (error) {
// console.log(error);
renderError("Something went wrong!", error.message);
})
.finally(function () {});
// console.log(fetchdata);
}
// getData("serbia");
function renderCountry(item, classCard = "") {
const language = Object.values(item.languages)[0];
console.log(language);
const currency = Object.values(item.currencies)[0];
currency.template = `${currency.name},\n
symbol: ${currency.symbol}`;
console.log(currency.template);
const cardEl = `
<div class="card ${classCard}">
<img class="card-img" src="${item.flags.png}" alt="${
item.flags.alt
}" />
<div class="card-info">
<div class="card-main-info">
<h2>${item.name.common}</h2>
<h4>Capital: ${item.capital[0]}</h4>
</div>
<div class="info">
<p>Region: ${item.region}</p>
<p>Population: ${(+item.population / 1000000).toFixed(2)} mil</p>
<p>Language: ${language}</p>
<p>Currency: ${currency.template}</p>
</div>
</div>
</div>`;
countries.insertAdjacentHTML("beforeend", cardEl);
}
searchInput.addEventListener("keyup", function (event) {
if (event.key === "Enter") {
handlerFunction();
}
});
searchButton.addEventListener("click", handlerFunction);
function resetInput() {
searchInput.value = "";
}
function handlerFunction() {
if (searchInput.value.trim()) {
// getData(searchInput.value);
getDataI(searchInput.value);
resetInput();
} else {
alert("Unesite vrednot u input element!");
}
}
function renderError(message, errMsg) {
countries.innerHTML += `${message} -> ${errMsg}`;
}
//II nacin - renderovanje drzava po regionu
function getDataI(region) {
fetch(`https://restcountries.com/v3.1/region/${region}`)
.then((response) => {
if (!response.ok)
throw new Error(`Country not found, status:${response.status}`);
return response.json();
})
.then((result) => {
renderCountry(result[0]);
});
// const neigbourCounty = result[0].borders ? result[0].borders[0] : null;
// console.log(neigbourCounty);
// if (!neigbourCounty) throw new Error("Neighbour country do not exist! ");
// return fetch(`https://restcountries.com/v3.1/alpha/${neigbourCounty}`);
// })
// .then((resolve) => {
// return resolve.json();
// })
// .then((data) => {
// console.log(data[0]);
// renderCountry(data[0], "card-neigbour");
// })
// .catch((error) => {
// renderError("Something went wrong!", error.message);
// })
// .finally(() => {});
}
const searchInput = document.getElementById("country-name");
const searchButton = document.getElementById("search-country");
const countries = document.getElementsByClassName("countries-container")[0];
const cardElement = document.getElementsByClassName("card")[0];
function getData(countryName) {
fetch(`https://restcountries.com/v3.1/name/${countryName}`)
.then(function (response) {
console.log(response);
console.log(typeof response);
if (!response.ok)
throw new Error(`Country not found, status:${response.status}`);
return response.json();
})
.then(function (result) {
console.log(result[0]);
renderCountry(result[0]);
const neigbourCounty = result[0].borders ? result[0].borders[0] : null;
console.log(neigbourCounty);
if (!neigbourCounty) throw new Error("Neighbour country do not exist! ");
return fetch(`https://restcountries.com/v3.1/alpha/${neigbourCounty}`);
})
.then(function (resolve) {
return resolve.json();
})
.then(function (data) {
console.log(data[0]);
renderCountry(data[0], "card-neigbour");
})
.catch(function (error) {
// console.log(error);
renderError("Something went wrong!", error.message);
})
.finally(function () {});
// console.log(fetchdata);
}
// getData("serbia");
function renderCountry(item, classCard = "") {
const language = Object.values(item.languages)[0];
console.log(language);
const currency = Object.values(item.currencies)[0];
currency.template = `${currency.name},\n
symbol: ${currency.symbol}`;
console.log(currency.template);
const cardEl = `
<div class="card ${classCard}">
<img class="card-img" src="${item.flags.png}" alt="${
item.flags.alt
}" />
<div class="card-info">
<div class="card-main-info">
<h2>${item.name.common}</h2>
<h4>Capital: ${item.capital[0]}</h4>
</div>
<div class="info">
<p>Region: ${item.region}</p>
<p>Population: ${(+item.population / 1000000).toFixed(2)} mil</p>
<p>Language: ${language}</p>
<p>Currency: ${currency.template}</p>
</div>
</div>
</div>`;
countries.insertAdjacentHTML("beforeend", cardEl);
}
searchInput.addEventListener("keyup", function (event) {
if (event.key === "Enter") {
handlerFunction();
}
});
searchButton.addEventListener("click", handlerFunction);
function resetInput() {
searchInput.value = "";
}
function handlerFunction() {
if (searchInput.value.trim()) {
// getData(searchInput.value);
getDataI(searchInput.value);
resetInput();
} else {
alert("Unesite vrednot u input element!");
}
}
function renderError(message, errMsg) {
countries.innerHTML += `${message} -> ${errMsg}`;
}
//II nacin - renderovanje drzava po regionu
function getDataI(region) {
fetch(`https://restcountries.com/v3.1/region/${region}`)
.then((response) => {
if (!response.ok)
throw new Error(`Country not found, status:${response.status}`);
return response.json();
})
.then((result) => {
renderCountry(result[0]);
});
// const neigbourCounty = result[0].borders ? result[0].borders[0] : null;
// console.log(neigbourCounty);
// if (!neigbourCounty) throw new Error("Neighbour country do not exist! ");
// return fetch(`https://restcountries.com/v3.1/alpha/${neigbourCounty}`);
// })
// .then((resolve) => {
// return resolve.json();
// })
// .then((data) => {
// console.log(data[0]);
// renderCountry(data[0], "card-neigbour");
// })
// .catch((error) => {
// renderError("Something went wrong!", error.message);
// })
// .finally(() => {});
}