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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package-lock.json
# Logs
logs
*.log
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
"devDependencies": {
"live-server": "^1.2.1"
}
}
}
47 changes: 35 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const $app = document.getElementById('app');
const $observe = document.getElementById('observe');
const API = 'https://rickandmortyapi.com/api/character/';
const localStorage = window.localStorage

const getData = api => {
fetch(api)
.then(response => response.json())
.then(response => {
const characters = response.results;
let output = characters.map(character => {
localStorage.setItem('next_fetch',`${API}`)

const getData = async (api) => {
try {
const request = await fetch(api)
const response = await request.json()
const characters = response.results

let output = await characters.map(character => {
return `
<article class="Card">
<img src="${character.image}" />
Expand All @@ -19,12 +23,30 @@ const getData = api => {
newItem.classList.add('Items');
newItem.innerHTML = output;
$app.appendChild(newItem);
})
.catch(error => console.log(error));
}
const url = response.info.next
localStorage.removeItem('next_fetch')
localStorage.setItem('next_fetch',`${url}`)
}
catch(error){
console.log(error)
}
}

const loadData = () => {
getData(API);

const loadData = async () => {
const URL = localStorage.getItem('next_fetch')
try {
if (URL === 'null'){
let h1 = document.createElement('h1');
h1.innerHTML = 'Ya no hay mas personajes ...!'
$observe.appendChild(h1)
localStorage.removeItem('next_fetch')
intersectionObserver.unobserve($observe);
}else {
await getData(URL)
}
}
catch(error){console.log(error)}
}

const intersectionObserver = new IntersectionObserver(entries => {
Expand All @@ -35,4 +57,5 @@ const intersectionObserver = new IntersectionObserver(entries => {
rootMargin: '0px 0px 100% 0px',
});

intersectionObserver.observe($observe);
intersectionObserver.observe($observe);