Skip to content

Js Challenge v1 #247

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 5 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
Binary file added .DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,8 @@ typings/

# next.js build output
.next

#mac
.DS_Store


16 changes: 8 additions & 8 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

Solución al reto:

Nombre:
Usuario Platzi:
Correo Electronico:
Nombre: Eduardo
Usuario Platzi: masterarias
Correo Electronico: [email protected]

## Reto:

- [ ] Primer problema
- [ ] Segundo problema
- [ ] Tercer problema
- [ ] Cuarto Problema
- [ ] Quinto Problema
- [] Primer problema
- [] Segundo problema
- [] Tercer problema
- [] Cuarto Problema
- [] Quinto Problema -> https://masterarias.github.io/
6 changes: 3 additions & 3 deletions cypress/e2e/test.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ describe("PlatziStore Tests", () => {
});

it("Obtener los primeros 10 Productos", () => {
cy.get('*[class^="Items"]').find('article').should('have.length', 10)
cy.get('*[class^="Item"]').find('article').should('have.length', 10)
});

it('Desplácese hacia abajo y renderice nuevos productos', () => {
cy.scrollTo('bottom')
cy.get('*[class^="Items"]').should('have.length', 2);
cy.get('*[class^="Item"]').should('have.length', 2);
});

it('Comprobar el nombre del Producto', () => {
cy.scrollTo('bottom');
cy.get('*[class^="Items"]').find('article').find('h2').eq(0).should('exist');
cy.get('*[class^="Item"]').find('article').find('h2').eq(0).should('exist');
});

});
3 changes: 2 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +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>
<title>PlatziStore</title>
<link type="text/css" href="styles.css" rel="stylesheet">
<meta name="description" content="PlatziStore sitio web demo">
</head>

<body>
Expand Down
60 changes: 45 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,60 @@ 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.clear('pagination');

const getData = async api => {
let current = parseInt(localStorage.getItem('pagination'));
if (current > 200) {
$observe.innerHTML = "<h3>Todos los productos Obtenidos</h3>";
intersectionObserver.unobserve($observe);
return;
}
const getData = await fetch(`${api}?offset=${current}&limit=10`);
const response = await getData.json();
let products = response;
let output = products.map(product => {
return `<article class="Card" id="Card-${product.id}">
<img src="${product.images[0]}" alt="${product.title}" />
<h2>
${product.title}
<small>$ ${product.price}</small>
</h2>
</article>`;
}).join('');
let newItem = document.createElement('section');
newItem.classList.add('Item');
newItem.innerHTML = output;
$app.appendChild(newItem);
}

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


const intersectionObserver = new IntersectionObserver(entries => {
// logic...
entries.forEach(entry => {
if (entry.isIntersecting) {
const check = localStorage.getItem("pagination");
if (!check) {
localStorage.setItem("pagination", 5);
}
else
{
localStorage.setItem("pagination", parseInt(check) + 10);
}



loadData();
}
})
}, {
rootMargin: '0px 0px 100% 0px',
});

intersectionObserver.observe($observe);