Skip to content

Commit 973fddb

Browse files
committed
feat: batch comment loading (5 at a time)
1 parent 4070160 commit 973fddb

2 files changed

Lines changed: 69 additions & 34 deletions

File tree

js/full-size-picture.js

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,14 @@
11
import { photos } from './create-array-photos.js';
2+
import { clearComments, renderComments } from './render-comments.js';
23

34
const bigPictureNode = document.querySelector('.big-picture');
45
const bigPictureImgNode = bigPictureNode.querySelector('.big-picture__img').querySelector('img');
56
const likesCountNode = bigPictureNode.querySelector('.likes-count');
6-
const commentsShownCountNode = bigPictureNode.querySelector('.social__comment-shown-count');
7-
const commentsTotalCountNode = bigPictureNode.querySelector('.social__comment-total-count');
8-
const socialCommentsNode = bigPictureNode.querySelector('.social__comments');
9-
const socialCommentTemplate = socialCommentsNode.querySelector('.social__comment');
107
const commentsCaptionNode = bigPictureNode.querySelector('.social__caption');
11-
const commentsCountNode = bigPictureNode.querySelector('.social__comment-count');
12-
const commentsLoaderNode = bigPictureNode.querySelector('.social__comments-loader');
138
const bigPictureCancelNode = bigPictureNode.querySelector('.big-picture__cancel');
149

15-
const createSocialComment = ({ avatar, name, message}) => {
16-
const socialCommentNode = socialCommentTemplate.cloneNode(true);
17-
18-
const socialPictureNode = socialCommentNode.querySelector('.social__picture');
19-
socialPictureNode.src = avatar;
20-
socialPictureNode.alt = name;
21-
socialCommentNode.querySelector('.social__text').textContent = message;
22-
23-
return socialCommentNode;
24-
};
25-
26-
const renderComments = (comments) => {
27-
const socialCommentsFragment = document.createDocumentFragment();
28-
29-
const appendComment = (comment) => {
30-
socialCommentsFragment.appendChild(createSocialComment(comment));
31-
};
32-
33-
comments.forEach(appendComment);
34-
socialCommentsNode.innerHTML = '';
35-
socialCommentsNode.appendChild(socialCommentsFragment);
36-
};
37-
3810
const closeBigPicture = () => {
11+
clearComments();
3912
bigPictureNode.classList.add('hidden');
4013
document.body.classList.remove('modal-open');
4114
document.removeEventListener('keydown', onEscKeydown);
@@ -62,15 +35,10 @@ const openBigPicture = (pictureId) => {
6235
bigPictureImgNode.src = currentPhoto.url;
6336
bigPictureImgNode.alt = currentPhoto.description;
6437
likesCountNode.textContent = currentPhoto.likes;
65-
commentsShownCountNode.textContent = currentPhoto.comments.length;
66-
commentsTotalCountNode.textContent = currentPhoto.comments.length;
6738
commentsCaptionNode.textContent = currentPhoto.description;
6839

6940
renderComments(currentPhoto.comments);
7041

71-
commentsCountNode.classList.add('hidden');
72-
commentsLoaderNode.classList.add('hidden');
73-
7442
bigPictureNode.classList.remove('hidden');
7543
document.body.classList.add('modal-open');
7644
document.addEventListener('keydown', onEscKeydown);

js/render-comments.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const COUNT_STEP = 5;
2+
let currentCount = 0;
3+
let comments = [];
4+
5+
const bigPictureNode = document.querySelector('.big-picture');
6+
const socialCommentsNode = bigPictureNode.querySelector('.social__comments');
7+
const commentsShownCountNode = bigPictureNode.querySelector('.social__comment-shown-count');
8+
const commentsTotalCountNode = bigPictureNode.querySelector('.social__comment-total-count');
9+
const socialCommentTemplate = socialCommentsNode.querySelector('.social__comment');
10+
const commentsCountNode = bigPictureNode.querySelector('.social__comment-count');
11+
const commentsLoaderNode = bigPictureNode.querySelector('.social__comments-loader');
12+
// socialCommentsNode.innerHTML = '';
13+
14+
const createComment = ({ avatar, name, message}) => {
15+
const socialCommentNode = socialCommentTemplate.cloneNode(true);
16+
const socialPictureNode = socialCommentNode.querySelector('.social__picture');
17+
18+
socialPictureNode.src = avatar;
19+
socialPictureNode.alt = name;
20+
socialCommentNode.querySelector('.social__text').textContent = message;
21+
22+
return socialCommentNode;
23+
};
24+
25+
const renderNextComments = () => {
26+
const socialCommentsFragment = document.createDocumentFragment();
27+
const nextComments = comments.slice(currentCount, currentCount + COUNT_STEP);
28+
29+
nextComments.forEach((comment) => {
30+
socialCommentsFragment.appendChild(createComment(comment));
31+
});
32+
33+
socialCommentsNode.appendChild(socialCommentsFragment);
34+
currentCount += nextComments.length;
35+
commentsShownCountNode.textContent = currentCount;
36+
commentsTotalCountNode.textContent = comments.length;
37+
38+
if (currentCount >= comments.length) {
39+
commentsLoaderNode.classList.add('hidden');
40+
}
41+
};
42+
43+
const clearComments = () => {
44+
currentCount = 0;
45+
comments = [];
46+
socialCommentsNode.innerHTML = '';
47+
commentsCountNode.classList.add('hidden');
48+
commentsLoaderNode.classList.add('hidden');
49+
commentsLoaderNode.removeEventListener('click', renderNextComments);
50+
};
51+
52+
const renderComments = (currentPhotoComments) => {
53+
comments = currentPhotoComments;
54+
currentCount = 0;
55+
socialCommentsNode.innerHTML = '';
56+
57+
commentsCountNode.classList.remove('hidden');
58+
commentsLoaderNode.classList.remove('hidden');
59+
60+
commentsLoaderNode.removeEventListener('click', renderNextComments);
61+
commentsLoaderNode.addEventListener('click', renderNextComments);
62+
63+
renderNextComments();
64+
};
65+
66+
export {clearComments, renderComments};
67+

0 commit comments

Comments
 (0)