Skip to content

Commit

Permalink
Перламутровые пуговицы--
Browse files Browse the repository at this point in the history
Решить проблему с удалением
  • Loading branch information
Ayronhayd committed Mar 31, 2024
1 parent a227c57 commit 7d22d03
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
59 changes: 59 additions & 0 deletions js/components/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { debounce } from '../utils/debounce.js';

const PICTURES_COUNT = 10;
const Filter = {
DEFAULT: 'filter-default',
RANDOM: 'filter-random',
DISCUSSED: 'filter-discussed',
};

const filtersElement = document.querySelector('.img-filters');

let currentFilter = '';
let pictures = [];

const randomSort = () => Math.random() - 0.5;

const discussedSort = (pictureA, pictureB) =>
pictureB.comments.length - pictureA.comments.length;

const filterPictures = () => {
switch (currentFilter) {
case Filter.RANDOM:
return [...pictures].sort(randomSort).slice(0, PICTURES_COUNT);
case Filter.DISCUSSED:
return [...pictures].sort(discussedSort);
default:
return [...pictures];
}
};

const turnFilterOn = (loadedPictures) => {
filtersElement.classList.remove('img-filters--inactive');
pictures = [...loadedPictures];
currentFilter = Filter.DEFAULT = '';
};

const setOnFilterClick = (cb) => {
const debouncedCallback = debounce(cb);

filtersElement.addEventListener('click', (evt) => {
if (!evt.target.classList.contains('img-filters__button')) {
return;
}

const clickedButton = evt.target;
if (clickedButton.id === currentFilter) {
return;
}

filtersElement
.querySelector('.img-filters__button--active')
.classList.remove('img-filters__button--active');
clickedButton.classList.add('img-filters__button--active');
currentFilter = clickedButton.id;
debouncedCallback(filterPictures());
});
};

export { setOnFilterClick, turnFilterOn, filterPictures };
15 changes: 14 additions & 1 deletion js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ import {
showErrorMessage
} from './components/message.js';

import {
setOnFilterClick,
turnFilterOn,
filterPictures
} from './components/filter.js';


const onGetDataSuccess = (data) => {
turnFilterOn(data);
renderPictures(filterPictures());
setOnFilterClick(renderPictures);
};

const onSendDataSuccess = () => {
hideModal();
showSuccessMessage();
Expand All @@ -30,4 +43,4 @@ setOnFormSubmit(async (data) => {
await sendData(onSendDataSuccess, onSendDataError, data);
});

getData(renderPictures, showAlert);
getData(onGetDataSuccess, showAlert);
9 changes: 9 additions & 0 deletions js/utils/debounce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const debounce = (callback, timeoutDelay = 500) => {
let timeoutId;
return (...rest) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => callback.apply(this, rest), timeoutDelay);
};
};

export { debounce };

0 comments on commit 7d22d03

Please sign in to comment.