-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (37 loc) · 1.12 KB
/
index.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
"use strict";
class ImagesGallery {
#imagesGalleryWrapper;
constructor(imagesGalleryWrapper) {
this.#imagesGalleryWrapper = imagesGalleryWrapper !== null ? imagesGalleryWrapper : document.body.appendChild(document.createElement("div"));
};
#renderImageCard = (url) => {
return `
<div class="image-wrapper">
<img class="img" src="${url}">
</div>
`;
};
#addImageCardsToDOM = (arr) => {
for (let item of arr) {
const imageWrapper = document.createElement("div");
imageWrapper.innerHTML = this.#renderImageCard(item.download_url);
this.#imagesGalleryWrapper.appendChild(imageWrapper);
}
};
fetchImages = async () => {
try {
const response = await fetch(
"https://picsum.photos/v2/list?page=1&limit=10"
);
const img = await response.json();
this.#addImageCardsToDOM(img);
} catch (error) {
throw new Error('Data loading error:', error)
}
};
};
(() => {
const imagesGalleryWrapper = document.querySelector(".images-gallery");
const imagesGallery = new ImagesGallery(imagesGalleryWrapper);
imagesGallery.fetchImages();
})();