-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38514ec
commit 402a626
Showing
4 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {createFragment} from './thumbnails.js'; | ||
import {renderModal} from './modal.js'; | ||
import {getRandomArrayElement} from './util.js'; | ||
import {debounce} from './util.js'; | ||
|
||
const POST_COUNT = 10; | ||
const RERENDER_DELAY = 500; | ||
|
||
const imgFiltersForm = document.querySelector('.img-filters__form'); | ||
const buttonsElements = imgFiltersForm.querySelectorAll('.img-filters__button'); | ||
|
||
const getRandomPost = (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) => { | ||
imgFiltersForm.addEventListener('click', (evt) => { | ||
const targetButton = evt.target.closest('.img-filters__button'); | ||
if (targetButton) { | ||
buttonsElements.forEach((el) => el.classList.remove('img-filters__button--active')); | ||
targetButton.classList.add('img-filters__button--active'); | ||
|
||
switch (evt.target.id) { | ||
case 'filter-default': | ||
debounce(() => updatePosts(data), RERENDER_DELAY)(); | ||
break; | ||
|
||
case 'filter-random': | ||
debounce(() => updatePosts(getRandomPost(data), RERENDER_DELAY))(); | ||
break; | ||
|
||
case 'filter-discussed': | ||
debounce(() => updatePosts(data.toSorted((a, b) => b.comments.length - a.comments.length)), RERENDER_DELAY)(); | ||
break; | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
export {getRandomPost, onChangefilterPosts}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters