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

Challenge complete #243

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
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: Edwin Olaff Cuba Davila
Usuario Platzi: edwinolaffcubadavila
Correo Electronico: edwincubad@gmail.com

## Reto:

- [ ] Primer problema
- [ ] Segundo problema
- [ ] Tercer problema
- [ ] Cuarto Problema
- [ ] Quinto Problema
- [x] Primer problema
- [x] Segundo problema
- [x] Tercer problema
- [x] Cuarto Problema
- [x] Quinto Problema
35 changes: 17 additions & 18 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<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>PlatziStore</title>
<meta name="description" content="Primer reto de platzi para participar en los cursos con Ethereum Foundation." />
<link type="text/css" href="styles.css" rel="stylesheet" />
</head>

<head>
<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>
<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>
</body>

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

<script type="text/javascript" src="../src/index.js"></script>

</html>
<script type="text/javascript" src="../src/index.js"></script>
</html>
85 changes: 60 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,65 @@ 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 intersectionObserver = new IntersectionObserver(entries => {
// logic...
}, {
rootMargin: '0px 0px 100% 0px',
});
localStorage.clear();

const PAGINTAION_STORAGE = 'pagination';
const initialPagination = 5;

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

if (!products.length) {
intersectionObserver.unobserve($observe);
const message = document.createElement('p');
message.innerHTML = 'Todos los productos Obtenidos';
return $app.appendChild(message);
}

const output = products.map(
(product) => `
<article class="Card">
<img src="${product.images[0]}" />
<h2>
${product.title}
<small>$ ${product.price}</small>
</h2>
</article>
`,
);

const newItem = document.createElement('section');
newItem.classList.add('Items');
newItem.innerHTML = output.join('');
$app.appendChild(newItem);
};

const getNewPagination = () => {
const pagination = localStorage.getItem(PAGINTAION_STORAGE);
localStorage.setItem(PAGINTAION_STORAGE, pagination ? Number(pagination) + 10 : initialPagination);
return localStorage.getItem(PAGINTAION_STORAGE);
};

const loadData = async () => {
const pagination = getNewPagination();
const objPAarams = { offset: pagination, limit: 10 };
const strParams = new URLSearchParams(objPAarams).toString();
const PARAMETERIZED_API = `${API}?${strParams}`;
await getData(PARAMETERIZED_API);
};

let = false;
const intersectionObserver = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) {
loadData();
}
},
{
rootMargin: '0px 0px 100% 0px',
threshold: 1,
},
);

intersectionObserver.observe($observe);