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 solved #253

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
17 changes: 9 additions & 8 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

Solución al reto:

Nombre:
Usuario Platzi:
Correo Electronico:
Nombre: Adrian Perez
Usuario Platzi: adrianjose
Correo Electronico: [email protected]

GitHub Page: https://adrianjp2.github.io/js-challenge/public/
## Reto:

- [ ] Primer problema
- [ ] Segundo problema
- [ ] Tercer problema
- [ ] Cuarto Problema
- [ ] Quinto Problema
- [*] Primer problema
- [*] Segundo problema
- [*] Tercer problema
- [*] Cuarto Problema
- [*] Quinto Problema
3 changes: 2 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<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="..."/>
<link type="text/css" href="styles.css" rel="stylesheet">
</head>

Expand Down
73 changes: 50 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,58 @@
const $app = document.getElementById('app');
const $observe = document.getElementById('observe');
const API = 'https://api.escuelajs.co/api/v1/products';
const $app = document.getElementById("app");
const $observe = document.getElementById("observe");
const API = "https://api.escuelajs.co/api/v1/products";

const getData = api => {
const getData = (api) => {
let offset = null;
if (localStorage.getItem("offset")) {
offset = localStorage.getItem("offset");
} else {
offset = 5;
}
api += "?offset=" + offset + "&limit=10";

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));
}
.then((response) => response.json())
.then((response) => {
console.log(response);
let products = response;
let output = products.map((product) => {
let template = '<article class="Card"> <img src="'+product.images[0]+'" /> <h2> '+product.description+' <small>$ '+product.price+'</small> </h2> </article>';
return template;
});
let newItem = document.createElement("section");
newItem.classList.add("Item");
newItem.innerHTML = output;
$app.appendChild(newItem);
if(response.length == 0){
intersectionObserver.disconnect();
let message = document.createElement("section");
message.innerHTML = 'Todos los productos Obtenidos';
$app.appendChild(message);
} else {
offset = response.slice(-1)[0].id + 1;
localStorage.setItem("offset", offset);
}
}).catch((error) => console.log(error));
};

const loadData = () => {
getData(API);
}
const loadData = async () => {
localStorage.removeItem('offset');
await getData(API);
};

const intersectionObserver = new IntersectionObserver(entries => {
// logic...
entries.forEach((entry) => {
if (entry.isIntersecting) {
getData(API)
}
});
}, {
rootMargin: '0px 0px 100% 0px',
root: null,
rootMargin: "0px",
threshold: 0.25
});

intersectionObserver.observe($observe);
window.addEventListener("load", function () {
intersectionObserver.observe($observe);
});