Uvod radjeno Dan 24
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="main.js" defer></script>
</head>
<body></body>
</html>
// const promise = new Promise((resolve, reject) => {
// const x = 10;
// const y = 3;
// if (x === y) {
// resolve({ status: "uspesno realizovan promise", statusCode: 200 });
// } else {
// reject({
// reason: "Page not found",
// statusCode: 404,
// });
// }
// });
// console.log(promise);
//AJAX poziv
function getData() {
const data = fetch("https://jsonplaceholder.typicode.com/todos/1")
.then((resolve) => {
console.log(resolve);
return resolve.json();
})
.then((result) => console.log(result));
console.log(data);
}
getData();
//zivotni ciklus promisa:
// 1.pending -pocetno stanje, pre nego sto se ijedna vrednost vrati
//2.settled: fulfilled, rejected
// const promise = new Promise((resolve, reject) => {
// const x = 10;
// const y = 3;
// if (x === y) {
// resolve({ status: "uspesno realizovan promise", statusCode: 200 });
// } else {
// reject({
// reason: "Page not found",
// statusCode: 404,
// });
// }
// });
// console.log(promise);
//AJAX poziv
function getData() {
const data = fetch("https://jsonplaceholder.typicode.com/todos/1")
.then((resolve) => {
console.log(resolve);
return resolve.json();
})
.then((result) => console.log(result));
console.log(data);
}
getData();
//zivotni ciklus promisa:
// 1.pending -pocetno stanje, pre nego sto se ijedna vrednost vrati
//2.settled: fulfilled, rejected
<!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>
</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;
}
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%;
height: 150vh;
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 {
width: 100%;
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;
// z-index: -5;
margin: 0;
background: linear-gradient(to top, rgba(255, 255, 255, 0.5)),
url("piece.png");
background-repeat: repeat;
// height: 100vh;
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%;
height: 150vh;
// height: calc(100vh - 112px - 60px - 2rem);
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) {
// 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(() => {});
}