Skip to content

Commit a74d6fa

Browse files
committed
refactor(comments): use state object for comment pagination
1 parent 973fddb commit a74d6fa

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

js/render-comments.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const COUNT_STEP = 5;
2-
let currentCount = 0;
3-
let comments = [];
2+
3+
const state = {
4+
currentCount: 0,
5+
comments: [],
6+
};
47

58
const bigPictureNode = document.querySelector('.big-picture');
69
const socialCommentsNode = bigPictureNode.querySelector('.social__comments');
@@ -9,7 +12,6 @@ const commentsTotalCountNode = bigPictureNode.querySelector('.social__comment-to
912
const socialCommentTemplate = socialCommentsNode.querySelector('.social__comment');
1013
const commentsCountNode = bigPictureNode.querySelector('.social__comment-count');
1114
const commentsLoaderNode = bigPictureNode.querySelector('.social__comments-loader');
12-
// socialCommentsNode.innerHTML = '';
1315

1416
const createComment = ({ avatar, name, message}) => {
1517
const socialCommentNode = socialCommentTemplate.cloneNode(true);
@@ -24,38 +26,46 @@ const createComment = ({ avatar, name, message}) => {
2426

2527
const renderNextComments = () => {
2628
const socialCommentsFragment = document.createDocumentFragment();
27-
const nextComments = comments.slice(currentCount, currentCount + COUNT_STEP);
29+
const nextComments = state.comments.slice(
30+
state.currentCount,
31+
state.currentCount + COUNT_STEP
32+
);
2833

2934
nextComments.forEach((comment) => {
3035
socialCommentsFragment.appendChild(createComment(comment));
3136
});
3237

3338
socialCommentsNode.appendChild(socialCommentsFragment);
34-
currentCount += nextComments.length;
35-
commentsShownCountNode.textContent = currentCount;
36-
commentsTotalCountNode.textContent = comments.length;
39+
state.currentCount += nextComments.length;
40+
commentsShownCountNode.textContent = state.currentCount;
41+
commentsTotalCountNode.textContent = state.comments.length;
3742

38-
if (currentCount >= comments.length) {
43+
if (state.currentCount >= state.comments.length) {
3944
commentsLoaderNode.classList.add('hidden');
4045
}
4146
};
4247

4348
const clearComments = () => {
44-
currentCount = 0;
45-
comments = [];
49+
state.currentCount = 0;
50+
state.comments = [];
4651
socialCommentsNode.innerHTML = '';
4752
commentsCountNode.classList.add('hidden');
4853
commentsLoaderNode.classList.add('hidden');
4954
commentsLoaderNode.removeEventListener('click', renderNextComments);
5055
};
5156

5257
const renderComments = (currentPhotoComments) => {
53-
comments = currentPhotoComments;
54-
currentCount = 0;
58+
state.comments = currentPhotoComments;
59+
state.currentCount = 0;
5560
socialCommentsNode.innerHTML = '';
5661

5762
commentsCountNode.classList.remove('hidden');
58-
commentsLoaderNode.classList.remove('hidden');
63+
if (state.comments.length > COUNT_STEP) {
64+
commentsLoaderNode.classList.remove('hidden');
65+
} else {
66+
commentsLoaderNode.classList.add('hidden');
67+
}
68+
// commentsLoaderNode.classList.remove('hidden');
5969

6070
commentsLoaderNode.removeEventListener('click', renderNextComments);
6171
commentsLoaderNode.addEventListener('click', renderNextComments);

0 commit comments

Comments
 (0)