Skip to content
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

Retos solucionados: Cristian Buenaventura #241

Open
wants to merge 3 commits 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
6 changes: 3 additions & 3 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

Solución al reto:

Nombre:
Usuario Platzi:
Correo Electronico:
Nombre:Cristian Buenaventura
Usuario Platzi:Cristian Buenaventura
Correo Electronico:cristianbuenaventura15@gmail.com

## Reto:

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
<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="description"
content="Web site that show the information of Platzi FakeApi"
/>
<title>PlatziStore</title>
<link type="text/css" href="styles.css" rel="stylesheet">
</head>

Expand Down
61 changes: 46 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,61 @@ 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));

localStorage.removeItem('pagination');
const limit = 10;

const getData = async api => {
const pagination = localStorage.getItem('pagination') || 1;
const offset = pagination * limit - 6;
let output;
try {
const getData = await fetch(`${api}?offset=${offset}&limit=${limit}`);
const response = await getData.json();

if (response.length) {
output = response.map(product => {
return `
<article class="Card">
<img src="${product.images[0]}" />
<h2>
${product.title}
<small>$ ${product.price}</small>
</h2>
</article>
`;
})
} else {
intersectionObserver.unobserve($observe);
output = `
<h2>Todos los productos Obtenidos</h2>
`
};

let newItem = document.createElement('section');
newItem.classList.add('Item');
newItem.innerHTML = output;
$app.appendChild(newItem);
} catch (error) {
consol.log(error);
}
localStorage.setItem('pagination', (parseInt(pagination) + 1));
}

const loadData = () => {
getData(API);
}


const intersectionObserver = new IntersectionObserver(entries => {
// logic...
entries.forEach(entry => {
if (entry.isIntersecting) {
loadData();
}
})
}, {
rootMargin: '0px 0px 100% 0px',
});

intersectionObserver.observe($observe);