-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplayImages.js
45 lines (41 loc) · 1.44 KB
/
displayImages.js
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
// Fetch and display a background image for a given city
const updateBackgroundImage = async (city, uniqueId) => {
try {
//const apiKeyUnsplash = config.API_KEY_UNSPLASH
const apiKeyUnsplash = import.meta.env.VITE_API_KEY_UNSPLASH
const response = await fetch(
`https://api.unsplash.com/search/photos?query=${city}&client_id=${apiKeyUnsplash}`
)
if (!response.ok) {
throw new Error("Failed to get city image from Unsplash")
}
const data = await response.json()
let imageCarousel = document.querySelector(`#image-carousel-${uniqueId}`)
if (imageCarousel) {
imageCarousel.innerHTML = ""
data.results.forEach((image) => {
let img = document.createElement("img")
img.src = image.urls.regular
img.classList.add("carousel-image")
imageCarousel.appendChild(img)
})
startCarousel(uniqueId)
}
} catch (error) {
console.error(error)
}
}
//Image Carousel
function startCarousel(uniqueId) {
let carouselImages = document.querySelectorAll(
`#image-carousel-${uniqueId} .carousel-image`
)
let currentIndex = 0
carouselImages[currentIndex].style.display = "block"
setInterval(() => {
carouselImages[currentIndex].style.display = "none"
currentIndex = (currentIndex + 1) % carouselImages.length
carouselImages[currentIndex].style.display = "block"
}, 3000)
}
export default updateBackgroundImage