Skip to content

Commit

Permalink
Открывается и закрывается (часть 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayronhayd committed Mar 17, 2024
1 parent f4e6379 commit 29e655b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
42 changes: 31 additions & 11 deletions js/components/render-comments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bigPicture } from './show-big-picture';
const COMMENTS_PORTION = 5;

const createComment = ({
avatar,
Expand All @@ -17,24 +17,44 @@ const createComment = ({
return comment;
};

const renderComments = (comments) => {

const renderComments = (comments, bigPicture) => {
const commentList = bigPicture.querySelector('.social__comments');
const commentsLoader = bigPicture.querySelector('.comments-loader');
const commentShowCount = bigPicture.querySelector('.social__comment-shown-count');
let commentsShown = 0;

const template = commentList.children[0];
const fragment = document.createDocumentFragment();


commentList.innerHTML = '';

comments.forEach((comment) => {
const commentElement = createComment(comment, template);
fragment.append(commentElement);
const shownComments = () => {
const startIndex = commentsShown * COMMENTS_PORTION;
commentsShown++;
const endIndex = Math.min(
startIndex + COMMENTS_PORTION,
comments.length
);

const slicedComments = comments.slice(startIndex, endIndex);

slicedComments.forEach((comment) => {
commentList.append(createComment(comment, template));
});

if (startIndex >= comments.length) {
commentsLoader.classList.add('hidden');
commentsShown = comments.length;
} else {
commentsLoader.classList.remove('hidden');
}
commentShowCount.textContent = endIndex;

};
shownComments();

commentsLoader.addEventListener('click', () => {
shownComments();
});

commentList.innerHTML = '';
commentList.append(fragment);
};

export {
Expand Down
2 changes: 1 addition & 1 deletion js/components/show-big-picture.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ const showBigPicture = ({
cancelButton.addEventListener('click', onCancelButtonClick);

export {
showBigPicture, bigPicture
showBigPicture
};

0 comments on commit 29e655b

Please sign in to comment.