Skip to content

Commit

Permalink
Поправила фильтрацию постов
Browse files Browse the repository at this point in the history
  • Loading branch information
lenapokrovskaya committed Jan 20, 2025
1 parent d900fa8 commit 760d8a6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
2 changes: 1 addition & 1 deletion js/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const BASE_URL = 'https://31.javascript.htmlacademy.pro/kekstagram';
const BASE_URL = 'https://32.javascript.htmlacademy.pro/kekstagram';

const Route = {
GET_DATA: '/data',
Expand Down
14 changes: 5 additions & 9 deletions js/filter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {createFragment} from './thumbnails.js';
import {renderModal} from './modal.js';
import {getRandomArrayElement} from './util.js';
import {debounce} from './util.js';

const POST_COUNT = 10;
Expand All @@ -9,17 +8,14 @@ const RERENDER_DELAY = 500;
const imgFiltersForm = document.querySelector('.img-filters__form');
const buttonsElements = imgFiltersForm.querySelectorAll('.img-filters__button');

const getRandomPosts = (postArray) =>
postArray.slice(0, POST_COUNT).map(() => getRandomArrayElement(postArray));

const updatePosts = (posts) => {
const pictureElements = document.querySelectorAll('.picture');
pictureElements.forEach((el) => el.remove());
createFragment(posts);
renderModal(posts);
};

const onChangeFilterPosts = (data) => {
const onChangeFilterPosts = (pictures) => {
imgFiltersForm.addEventListener('click', (evt) => {
const targetButton = evt.target.closest('.img-filters__button');
if (targetButton) {
Expand All @@ -28,19 +24,19 @@ const onChangeFilterPosts = (data) => {

switch (evt.target.id) {
case 'filter-default':
debounce(() => updatePosts(data), RERENDER_DELAY)();
debounce(() => updatePosts(pictures), RERENDER_DELAY)();
break;

case 'filter-random':
debounce(() => updatePosts(getRandomPosts(data), RERENDER_DELAY))();
debounce(() => updatePosts(pictures.toSorted(() => 0.5 - Math.random()).slice(0, POST_COUNT)), RERENDER_DELAY)();
break;

case 'filter-discussed':
debounce(() => updatePosts(data.toSorted((a, b) => b.comments.length - a.comments.length)), RERENDER_DELAY)();
debounce(() => updatePosts(pictures.toSorted((pictureA, pictureB) => pictureB.comments.length - pictureA.comments.length)), RERENDER_DELAY)();
break;
}
}
});
};

export {getRandomPosts, onChangeFilterPosts};
export {onChangeFilterPosts};

0 comments on commit 760d8a6

Please sign in to comment.