Skip to content

Commit 03ad2c4

Browse files
authored
Merge pull request #8 from NoaVel/module8-task2
2 parents 4070160 + a74d6fa commit 03ad2c4

2 files changed

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

0 commit comments

Comments
 (0)