diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index fb4df26..2a54980 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -2,11 +2,11 @@ Solución al reto 03 de Escuela de JavaScript -Nombre: -Usuario Platzi: +Nombre:Jhoan Canchila +Usuario Platzi:jcanchila ## Reto: - - [ ] Primer problema - - [ ] Segundo problema - - [ ] Tercer problema - - [ ] Cuarto Problema (Opcional) \ No newline at end of file + - [ok] Primer problema + - [ok] Segundo problema + - [ok] Tercer problema + - [ok] Cuarto Problema (Opcional) \ No newline at end of file diff --git a/src/index.js b/src/index.js index 7a07752..c127cc5 100644 --- a/src/index.js +++ b/src/index.js @@ -7,6 +7,9 @@ const getData = api => { .then(response => response.json()) .then(response => { const characters = response.results; + const nextURL = response.info.next; + // save next url + saveUrlLocalStorage(nextURL); let output = characters.map(character => { return `
@@ -23,16 +26,44 @@ const getData = api => { .catch(error => console.log(error)); } -const loadData = () => { - getData(API); +const loadData = async (url) => { + if (url === "null") { + alert('Ya no hay personajes...'); + intersectionObserver.unobserve($observe); + } else { + await getData(url); + } } const intersectionObserver = new IntersectionObserver(entries => { if (entries[0].isIntersecting) { - loadData(); + loadData(getDataLocalStorage()); } }, { rootMargin: '0px 0px 100% 0px', }); -intersectionObserver.observe($observe); \ No newline at end of file +intersectionObserver.observe($observe); + +const saveUrlLocalStorage = (url) => { + localStorage.setItem("next_fetch", url) +} + +function getDataLocalStorage() { + + const urlSave = localStorage.getItem('next_fetch'); + + if (urlSave) { + return urlSave; + } else { + return API; + } +} + +const removeLocalStorage = () => { + if (localStorage.getItem('next_fetch')) { + localStorage.removeItem('next_fetch'); + } +} + +window.addEventListener('DOMContentLoaded', removeLocalStorage);