diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index ddfff263..ae9f15f1 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -2,14 +2,15 @@ SoluciĆ³n al reto: -Nombre: -Usuario Platzi: -Correo Electronico: +Nombre: Adrian Perez +Usuario Platzi: adrianjose +Correo Electronico: adrianperez1223@gmail.com +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 diff --git a/public/index.html b/public/index.html index 5f8fbe0d..e6aed258 100755 --- a/public/index.html +++ b/public/index.html @@ -5,7 +5,8 @@ - + PlatziStore + diff --git a/src/index.js b/src/index.js index eb2631c2..ed13b463 100755 --- a/src/index.js +++ b/src/index.js @@ -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 = '

'+product.description+' $ '+product.price+'

'; + 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); +});