Skip to content

Commit b9e03d6

Browse files
authored
Merge pull request #12 from MadinaSatem/module12-task1
2 parents bf5714c + 71df3b2 commit b9e03d6

File tree

3 files changed

+72
-25
lines changed

3 files changed

+72
-25
lines changed

js/filter.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { renderThumbnails } from './render.js';
2+
import { debounce } from './util.js';
3+
4+
const RANDOM_IMAGES_COUNT = 10;
5+
const filterForm = document.querySelector('.img-filters__form');
6+
const filterButtons = Array.from(filterForm.querySelectorAll('.img-filters__button'));
7+
8+
let photosData = [];
9+
10+
const getRandomPhotos = (arr) => {
11+
const shuffled = arr.slice().sort(() => Math.random() - 0.5);
12+
return shuffled.slice(0, RANDOM_IMAGES_COUNT);
13+
};
14+
15+
const getDiscussedPhotos = (arr) => arr.slice().sort((a, b) => b.comments.length - a.comments.length);
16+
17+
const applyFilter = (filterId) => {
18+
let filtered = [];
19+
switch (filterId) {
20+
case 'filter-random':
21+
filtered = getRandomPhotos(photosData);
22+
break;
23+
case 'filter-discussed':
24+
filtered = getDiscussedPhotos(photosData);
25+
break;
26+
default:
27+
filtered = photosData.slice();
28+
}
29+
renderThumbnails(filtered);
30+
};
31+
32+
const onFilterClick = debounce((evt) => {
33+
const btn = evt.target;
34+
if (!btn.classList.contains('img-filters__button')) {
35+
return;
36+
}
37+
38+
filterButtons.forEach((button) => button.classList.remove('img-filters__button--active'));
39+
btn.classList.add('img-filters__button--active');
40+
applyFilter(btn.id);
41+
});
42+
43+
const initFilters = (photos) => {
44+
photosData = photos.slice();
45+
filterForm.addEventListener('click', onFilterClick);
46+
};
47+
48+
export { initFilters };

js/main.js

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,27 @@
1-
import './util.js';
2-
import './form-message.js';
3-
import './form.js';
4-
import './effects.js';
5-
import { initEffects } from './effects.js';
1+
import { getPhotos } from './api.js';
62
import { renderThumbnails, setThumbnailClickHandler } from './render.js';
7-
import { openUserModal, initModalListeners } from './modal.js';
3+
import { initFilters } from './filter.js';
4+
import { initModalListeners } from './modal.js';
85
import { initFormListeners } from './form.js';
9-
import { getPhotos } from './api.js';
6+
import { initEffects } from './effects.js';
7+
8+
const filtersContainer = document.querySelector('.img-filters');
109

1110
const init = async () => {
1211
try {
13-
const photoDescriptions = await getPhotos();
12+
const photos = await getPhotos();
1413

15-
renderThumbnails(photoDescriptions);
14+
renderThumbnails(photos);
15+
initFilters(photos);
1616

17-
setThumbnailClickHandler((photoId) => {
18-
const photo = photoDescriptions.find((item) => item.id === photoId);
19-
if (photo) {
20-
openUserModal(photo);
21-
}
22-
});
23-
24-
document.querySelector('.img-filters').classList.remove('img-filters--inactive');
17+
filtersContainer.classList.remove('img-filters--inactive');
2518
} catch (err) {
26-
const template = document.querySelector('#data-error');
27-
const errorElement = template.content.cloneNode(true);
28-
document.body.appendChild(errorElement);
29-
19+
const errorTemplate = document.querySelector('#data-error').content.cloneNode(true);
20+
document.body.appendChild(errorTemplate);
3021
setTimeout(() => {
31-
const element = document.querySelector('.data-error');
32-
if (element) {
33-
element.remove();
22+
const el = document.querySelector('.data-error');
23+
if (el) {
24+
el.remove();
3425
}
3526
}, 5000);
3627
}
@@ -41,4 +32,3 @@ const init = async () => {
4132
};
4233

4334
init();
44-

js/util.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,14 @@ const isEscapeKey = (evt) => evt.key === 'Escape';
77

88
const isEnterKey = (evt) => evt.key === 'Enter';
99

10+
function debounce(callback, delay = 500) {
11+
let timeoutId;
12+
return (...args) => {
13+
clearTimeout(timeoutId);
14+
timeoutId = setTimeout(() => callback(...args), delay);
15+
};
16+
}
17+
1018
export {getRandomArrayElement, isEscapeKey, isEnterKey};
1119
export {getRandomInteger};
20+
export { debounce };

0 commit comments

Comments
 (0)