-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
62 lines (54 loc) · 1.93 KB
/
main.js
File metadata and controls
62 lines (54 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import './style.css'
import { createCardSection } from './src/components/imagesSection/images.js'
import { drawCard } from './src/components/cards/cards.js'
import { createButton } from './src/components/button/button.js'
import { createFooter } from './src/components/footer/footer.js'
import { navbar } from './src/components/navBar/navbar.js'
createCardSection()
let totalImages = 0
async function CallApi(inputValue, page = 1) {
const USER_KEY = '_VtxJJpqoAQc1RM-7y5fAouBNFTC22E4hVglMeLAOKg'
const API_URL = `https://api.unsplash.com/search/photos?query=${inputValue}&page=${page}&client_id=${USER_KEY}`
try {
const response = await fetch(API_URL)
const data = await response.json()
const app = document.querySelector('#app')
const cardSection = document.createElement('section')
cardSection.className = 'cardSection'
if (data && data.results && data.results.length > 0) {
const cardSection = document.querySelector('.cardSection')
cardSection.innerHTML = ''
data.results.forEach((item) => {
drawCard(item, cardSection)
})
app.append(cardSection)
} else {
createCardSection()
console.log('Data is empty or not defined:', data)
}
} catch (error) {
console.log('Error al obtener los datos de la API', error)
}
}
const handleKeyDown = (event) => {
if (event.key === 'Enter') {
const inputValue = event.target.value
CallApi(inputValue)
}
}
const searchInput = document.querySelector('#searchBar')
searchInput.addEventListener('keydown', handleKeyDown)
createButton()
const showMoreButton = document.getElementById('show-more')
let currentPage = 1
showMoreButton.addEventListener('click', async () => {
try {
currentPage++
const perPage = 10 + totalImages
const inputValue = searchInput.value
await CallApi(inputValue, currentPage, perPage)
} catch (error) {
console.log('Error al cargar más imágenes', error)
}
})
createFooter()