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

PR: Reto JS #242

Open
wants to merge 7 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
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.

5 changes: 3 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
<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>
<meta name="description" content="Somos comercio en línea con una gran cantidad de productos a comercializar. Estamos por lanzar nuestra implementación y necesitamos resolver los problemas que presenta nuestra aplicación.">
<link type="text/css" href="styles.css" rel="stylesheet">
</head>

<body>
<div class="Main">
<h1>PlatziStore</h1>
<div id="app"></div>
<div id="observe"></div>
<div id="observe">Loading...</div>
</div>
</body>

Expand Down
18 changes: 18 additions & 0 deletions public/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,21 @@ body {
opacity: 1;
}
}

@media (max-width: 680px){
.Card h2{
flex-direction: column;
align-items: center;
}

.Card h2 small{
padding: 10px 0px 0px 0px;
}

.Items {
grid-template-columns: repeat(1, 1fr);
grid-gap: 1.5rem;
grid-row-gap: 1.5em;
display: grid;
}
}
71 changes: 50 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,60 @@
const $app = document.getElementById('app');
const $observe = document.getElementById('observe');
const API = 'https://api.escuelajs.co/api/v1/products';
const initOffset = 5;
const productLength = 10;

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 getPagination = () => parseInt(localStorage.getItem("pagination"));

const getData = async api => {
const actualOffset = getPagination();
try {
let response = await fetch(`${api}?offset=${actualOffset}&limit=${productLength}`);
let products = await response.json();
let output = products.map(product => {
// template
return `<article class="Card">
<img src="${product.images[0]}" />
<h2>
${product.title}
<small>$ ${product.price}</small>
</h2>
</article>`
});
let newItem = document.createElement('section');
newItem.classList.add('Items');
newItem.innerHTML = output.join('');
$app.appendChild(newItem);
}
catch (error) {
console.log(error);
}
}

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

const intersectionObserver = new IntersectionObserver(entries => {
// logic...
}, {
rootMargin: '0px 0px 100% 0px',
});
const intersectionObserver = new IntersectionObserver(
async entries =>
// logic...
entries.forEach(entry => {
let pagination = getPagination();
if (entry.isIntersecting) {
if(pagination>=200) {
alert("Todos los productos Obtenidos");
intersectionObserver.unobserve($observe);
$observe.innerHTML = "";
return;
};
let actualOffset = pagination? pagination + productLength: initOffset;
localStorage.setItem("pagination", actualOffset);
loadData();
}
})
, {
rootMargin: '0px 0px 100% 0px',
});

intersectionObserver.observe($observe);
window.onbeforeunload = () => localStorage.clear();