Skip to content
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
13 changes: 7 additions & 6 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

Solución al reto 03 de Escuela de JavaScript

Nombre:
Usuario Platzi:
Nombre: Adriana Ardila
Usuario Platzi: @ardila_adri

## Reto:
- [ ] Primer problema
- [ ] Segundo problema
- [ ] Tercer problema
- [ ] Cuarto Problema (Opcional)

- [x] Primer problema
- [x] Segundo problema
- [x] Tercer problema
- [x] Cuarto Problema (Opcional)
34 changes: 16 additions & 18 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
<!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>EscuelaJS Reto 03</title>
<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>EscuelaJS Reto 05</title>
<link type="text/css" href="styles.css" rel="stylesheet">
</head>
<body>
<div class="Main">
<h1>100tifi.co</h1>
<div class="2" id="app"></div>
<div id="observe"></div>
</div>
</body>

<body>
<div class="Main">
<h1>100tifi.co</h1>
<div class="2" 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>
80 changes: 57 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,72 @@
const $app = document.getElementById('app');
const $observe = document.getElementById('observe');
const API = 'https://rickandmortyapi.com/api/character/';
const $app = document.getElementById("app");
const $observe = document.getElementById("observe");
const API = "https://rickandmortyapi.com/api/character/";

const getData = api => {
const getData = (api) => {
fetch(api)
.then(response => response.json())
.then(response => {
.then((response) => response.json())
.then((response) => {
const characters = response.results;
let output = characters.map(character => {
return `
const nextURL = response.info.next;
urlSavedLocalStorage(nextURL);
let output = characters
.map((character) => {
return `
<article class="Card">
<img src="${character.image}" />
<h2>${character.name}<span>${character.species}</span></h2>
</article>
`
}).join('');
let newItem = document.createElement('section');
newItem.classList.add('Items');
`;
})
.join("");
let newItem = document.createElement("section");
newItem.classList.add("Items");
newItem.innerHTML = output;
$app.appendChild(newItem);
})
.catch(error => console.log(error));
}
.catch((error) => console.log(error));
};

const loadData = async (url) => {
if (url === "null") {
alert("Ya no hya personajes...");
intersectionObserver.unobserve($observe);
} else {
await getData(url);
}
};

const intersectionObserver = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) {
loadData(getDataLocalStorage());
}
},
{
rootMargin: "0px 0px 100% 0px",
}
);

const loadData = () => {
getData(API);
intersectionObserver.observe($observe);

const urlSavedLocalStorage = (url) => {
localStorage.setItem("next_fetch", url);
};

function getDataLocalStorage() {
const urlSave = localStorage.getItem("next_fetch");

if (urlSave) {
return urlSave;
} else {
return API;
}
}

const intersectionObserver = new IntersectionObserver(entries => {
if (entries[0].isIntersecting) {
loadData();
const removeLocalStorage = () => {
if (localStorage.getItem("next_fetch")) {
localStorage.removeItem("next_fetch");
}
}, {
rootMargin: '0px 0px 100% 0px',
});
};

intersectionObserver.observe($observe);
window.addEventListener("DOMContentLoaded", removeLocalStorage);