<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="style.css" />
<!-- <script src="main.js" defer></script> -->
<script src="async-await.js" defer></script>
</head>
<body>
<header>
<h1>Explore the World 🌎</h1>
</header>
<main>
<div class="search-container">
<input
type="text"
id="country-name"
placeholder="Enter a country name ..."
/>
<button id="search-country">Search country</button>
</div>
<section class="countries-container">
<!-- <div class="card">
<img src="" alt="" />
<div class="card-info">
<div class="card-main-info">
<h3>Name:</h3>
<h4>Capital:</h4>
</div>
<div class="info">
<p>Region:</p>
<p>Population:</p>
<p>Language:</p>
<p>Currency:</p>
</div>
<button class="get">Get</button>
</div> -->
</div>
</section>
</main>
<footer>
<p>exploretheworld.com</p>
</footer>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="style.css" />
<!-- <script src="main.js" defer></script> -->
<script src="async-await.js" defer></script>
</head>
<body>
<header>
<h1>Explore the World 🌎</h1>
</header>
<main>
<div class="search-container">
<input
type="text"
id="country-name"
placeholder="Enter a country name ..."
/>
<button id="search-country">Search country</button>
</div>
<section class="countries-container">
<!-- <div class="card">
<img src="" alt="" />
<div class="card-info">
<div class="card-main-info">
<h3>Name:</h3>
<h4>Capital:</h4>
</div>
<div class="info">
<p>Region:</p>
<p>Population:</p>
<p>Language:</p>
<p>Currency:</p>
</div>
<button class="get">Get</button>
</div> -->
</div>
</section>
</main>
<footer>
<p>exploretheworld.com</p>
</footer>
</body>
</html>
* {
box-sizing: border-box;
}
body {
position: relative;
margin: 0;
background: linear-gradient(to top, rgba(255, 255, 255, 0.5)), url("piece.png");
background-repeat: repeat;
min-height: 100vh;
padding-bottom: 5rem;
}
body header {
background-color: rgb(54, 66, 85);
padding: 0.8rem;
display: flex;
justify-content: center;
align-items: center;
}
body header h1 {
color: white;
}
body main {
padding: 1rem;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
}
body main .search-container input, body main .search-container button {
padding: 0.5rem;
border-radius: 5px;
font-size: 1.2rem;
margin-bottom: 1rem;
}
body main .search-container button {
border: none;
color: white;
background-color: rgb(54, 66, 85);
box-shadow: 2px 0px 10px rgba(0, 0, 0, 0.2);
}
body main .search-container button:hover {
background-color: rgb(82, 99, 128);
}
body main .search-container button:active {
transform: translateY(3px);
box-shadow: 2px 0 20px rgba(0, 0, 0, 0.4);
}
body main .countries-container {
display: flex;
flex-wrap: wrap;
width: 100%;
height: 100%;
gap: 2rem;
justify-content: center;
color: white;
}
body main .countries-container .card, body main .countries-container .card-neigbour {
background-color: rgb(232, 231, 231);
width: 35%;
max-width: 350px;
height: -moz-fit-content;
height: fit-content;
max-height: 480px;
border-radius: 10px;
}
body main .countries-container .card .card-img, body main .countries-container .card-neigbour .card-img {
width: 100%;
max-height: 180px;
border-radius: 10px;
}
body main .countries-container .card .card-info, body main .countries-container .card-neigbour .card-info {
padding: 5px;
}
body main .countries-container .card .card-info .card-main-info, body main .countries-container .card-neigbour .card-info .card-main-info {
border-radius: 10px;
background-color: rgb(190, 210, 243);
color: rgb(54, 66, 85);
display: flex;
flex-direction: column;
align-items: center;
}
body main .countries-container .card .card-info .card-main-info h2, body main .countries-container .card-neigbour .card-info .card-main-info h2,
body main .countries-container .card .card-info .card-main-info h4,
body main .countries-container .card-neigbour .card-info .card-main-info h4 {
margin: 0.5rem;
}
body main .countries-container .card .card-info .info, body main .countries-container .card-neigbour .card-info .info {
color: rgb(54, 66, 85);
}
body main .countries-container .card .get, body main .countries-container .card-neigbour .get {
background-color: rgb(54, 66, 85);
color: white;
padding: 0.5rem;
border: none;
border-radius: 5px;
width: 100px;
}
body main .countries-container .card:hover, body main .countries-container .card-neigbour:hover {
transform: scale(1.05);
}
body main .countries-container .card-neigbour {
width: 35%;
max-width: 300px;
height: -moz-fit-content;
height: fit-content;
max-height: 400px;
}
body footer {
position: absolute;
width: 100%;
bottom: 0;
background-color: rgb(54, 66, 85);
display: flex;
justify-content: center;
align-items: center;
}
body footer p {
color: white;
font-size: 1.2rem;
}/*# sourceMappingURL=style.css.map */
$default-color: rgb(54, 66, 85);
$text-color: white;
* {
box-sizing: border-box;
}
body {
position: relative;
margin: 0;
background: linear-gradient(to top, rgba(255, 255, 255, 0.5)),
url("piece.png");
background-repeat: repeat;
min-height: 100vh;
padding-bottom: 5rem;
header {
background-color: $default-color;
padding: 0.8rem;
display: flex;
justify-content: center;
align-items: center;
h1 {
color: $text-color;
}
}
main {
padding: 1rem;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
.search-container {
input {
padding: 0.5rem;
border-radius: 5px;
font-size: 1.2rem;
margin-bottom: 1rem;
}
button {
@extend input;
border: none;
color: $text-color;
background-color: $default-color;
box-shadow: 2px 0px 10px rgba(0, 0, 0, 0.2);
&:hover {
background-color: rgb(82, 99, 128);
}
&:active {
transform: translateY(3px);
box-shadow: 2px 0 20px rgba(0, 0, 0, 0.4);
}
}
}
.countries-container {
display: flex;
flex-wrap: wrap;
width: 100%;
height: 100%;
gap: 2rem;
justify-content: center;
color: white;
.card {
background-color: rgb(232, 231, 231);
width: 35%;
max-width: 350px;
height: fit-content;
max-height: 480px;
border-radius: 10px;
.card-img {
width: 100%;
max-height: 180px;
border-radius: 10px;
}
.card-info {
padding: 5px;
.card-main-info {
border-radius: 10px;
background-color: rgb(190, 210, 243);
color: $default-color;
display: flex;
flex-direction: column;
align-items: center;
h2,
h4 {
margin: 0.5rem;
}
}
.info {
color: $default-color;
}
}
.get {
background-color: $default-color;
color: white;
padding: 0.5rem;
border: none;
border-radius: 5px;
width: 100px;
}
&:hover {
transform: scale(1.05);
}
}
.card-neigbour {
@extend .card;
width: 35%;
max-width: 300px;
height: fit-content;
max-height: 400px;
}
}
}
footer {
position: absolute;
width: 100%;
bottom: 0;
background-color: $default-color;
display: flex;
justify-content: center;
align-items: center;
p {
color: $text-color;
font-size: 1.2rem;
}
}
}
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) {
renderError("Something went wrong!", error.message);
});
}
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);
resetInput();
} else {
alert("Unesite vrednot u input element!");
}
}
function renderError(message, errMsg) {
countries.innerHTML += `${message} -> ${errMsg}`;
}
//II primer - 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) => {
console.log(result);
for (const state of result) {
renderCountry(state);
}
})
.catch((error) => {
renderError("Something went wrong!", error.message);
});
}
// getDataI("asia");
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 renderCountry(item, classCard = "") {
const language = Object.values(item.languages)[0];
const currency = Object.values(item.currencies)[0];
currency.template = `${currency.name},\n
symbol: ${currency.symbol}`;
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()) {
fetchData(searchInput.value);
resetInput();
} else {
alert("Unesite vrednot u input element!");
}
}
function renderError(message, errMsg) {
countries.innerHTML += `${message} -> ${errMsg}`;
}
async function fetchData(countryName) {
try {
const response = await fetch(
`https://restcountries.com/v3.1/name/${countryName}`
);
if (!response.ok)
throw new Error(`Country not found, status:${response.status}`);
const result = await response.json();
renderCountry(result[0]);
const neigbourCountry = result[0].borders ? result[0].borders[0] : null;
if (!neigbourCountry) throw new Error("Neighbour country don't exist!");
const neigbourResponse = await fetch(
`https://restcountries.com/v3.1/alpha/${neigbourCountry}`
);
const dataNeighbour = await neigbourResponse.json();
renderCountry(dataNeighbour[0]);
} catch (err) {
renderError("Something went wrong!", err.message);
}
}
//try-catch
// let x = 1;
// const y = 2;
// x = 3;
// // y = 4;
// try {
// let x = 1;
// const y = 2;
// x = 3;
// y = 4;
// } catch (err) {
// console.log(err);
// }
// k = 5;
// console.log(k);