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

Solution by @Sdann26 #12

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
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:
Nombre: Israel Danilo Blas Salas
Usuario Platzi: Sdann26

## Reto:

- [ ] Primer problema
- [ ] Segundo problema
- [ ] Tercer problema
- [ ] Cuarto Problema
- [ ] Quinto Problema
- [ ] Sexto Problema
- [✔️] Primer problema
- [✔️] Segundo problema
- [✔️] Tercer problema
- [✔️] Cuarto Problema
- [✔️] Quinto Problema
- [✔️] Sexto Problema
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "FakeStore",
"main": "index.js",
"scripts": {
"build": "vite build",
"start": "live-server --open=public --entry-file=index.html",
"e2e": "cypress open"
},
Expand All @@ -19,7 +20,8 @@
},
"homepage": "https://github.com/platzi/laboratorio-fakestore#readme",
"devDependencies": {
"live-server": "^1.1.0"
"live-server": "^1.1.0",
"vite": "^4.3.9"
},
"dependencies": {
"cypress": "^10.2.0"
Expand Down
10 changes: 7 additions & 3 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
<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 http-equiv="Content-Language" content="es">
<meta name="description" content="Aplicativo web para mostrar productos del API FakeStore de Platzi.">
<meta name="keywords" content="tienda online, productos, ofertas, FakeStore">
<meta name="author" content="Danilo Blas">
<title>FakeStore</title>
<link type="text/css" href="styles.css" rel="stylesheet">
</head>

<body>
<div class="Main">
<h1>FakeStore</h1>
<div id="app"></div>
<div id="app" style="min-height: calc(100vh + 200px);"></div>
<div id="observe"></div>
</div>
</body>

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

</html>
76 changes: 57 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,69 @@
const $app = document.getElementById('app');
const $observe = document.getElementById('observe');
const API = 'https://api.escuelajs.co/api/v1/products';
const pagination = Number(localStorage.getItem('pagination')) || 4
let cantMaxProducts = null

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));
}
/* Functions */
const getData = async (api, pagination, limit) => {
try {
if(cantMaxProducts && (cantMaxProducts < pagination * limit)) {
intersectionObserver.disconnect();
}

const response = await fetch(`${api}?offset=${pagination * limit}&limit=${limit}`);
const products = await response.json();

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

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

localStorage.setItem('pagination', pagination + 1);
} catch (error) {
console.log(error);
}
};

const loadData = () => {
getData(API);
const loadData = async () => {
await getData(API, pagination, 10);

const cantMaxProductsRawr = await fetch(API)
cantMaxProducts = await cantMaxProductsRawr.json()
cantMaxProducts = cantMaxProducts.length
}

/* Browser API's */
const intersectionObserver = new IntersectionObserver(entries => {
// logic...
entries.forEach(entry => {
if(entry.isIntersecting) {
const pagination = Number(localStorage.getItem('pagination'))
console.log(pagination)
getData(API, pagination, 10);
}
})
}, {
rootMargin: '0px 0px 100% 0px',
rootMargin: '0px 0px 200px 0px',
});

intersectionObserver.observe($observe);

/* Events */
window.addEventListener('beforeunload', () => {
localStorage.clear()
});

/* First Render */
loadData()
12 changes: 12 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
build: {
target: 'es2018',
outDir: 'dist',
minify: true,
rollupOptions: {
input: {
main:'/public/index.html',
},
},
},
};