Skip to content

Reto finalizado Jose Luis Martinez #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<meta name="descripcion" content="Ethereum en Platzi">
<meta name="author" content="Jose Luis Martinez">
<title>PlatziStore</title>
<link type="text/css" href="styles.css" rel="stylesheet">
</head>

Expand Down
66 changes: 46 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,56 @@ const $app = document.getElementById('app');
const $observe = document.getElementById('observe');
const API = 'https://api.escuelajs.co/api/v1/products';

const getData = api => {
fetch(api)
.then(response => response.json())
.then(response => {
let products = response;
let output = products.map(product => {
// template
});
let newItem = document.createElement('section');
newItem.classList.add('Item');
newItem.innerHTML = output;
$app.appendChild(newItem);
})
.catch(error => console.log(error));
}

const loadData = () => {
getData(API);
const max = 10;
let start = 5;
let intersect = 1;
const mensaje = "Todos los productos Obtenidos."
const limit = 200;
const addElement = (items) => {
return items.map((item) => {
return `<article class= "Card"><img src=${item.images[0]}/>
<h2>${item.id} - ${item.title} <small>$${item.price}</small></h2>
</article>`;
});
};

const getData = async (api) => {
const response = await fetch(api);
const data = await response.json();

localStorage.removeItem("pagination");
try{
let output = addElement(data);
let newItem = document.createElement("section");
newItem.classList.add("Items");
newItem.innerHTML = output.join("");
$app.append(newItem);

localStorage.setItem("pagination", parseInt(start));
start += 10;
}
catch{
(error => console.log(error));
}
};

const loadData = async () => {
let pagination = `?start=${start}&max=${max}`;
getData(API + pagination);
};

const intersectionObserver = new IntersectionObserver(entries => {
// logic...
}, {
rootMargin: '0px 0px 100% 0px',
// logic...
if (entries[0].isIntersecting){
loadData();
}else if (limit){
document.getElementById(mensaje);
$observe.innerHTML = mensaje;
intersectionObserver.disconnect();
}},
{
rootMargin: '0px 0px 100% 0px',
});

intersectionObserver.observe($observe);