11const COUNT_STEP = 5 ;
2- let currentCount = 0 ;
3- let comments = [ ] ;
2+
3+ const state = {
4+ currentCount : 0 ,
5+ comments : [ ] ,
6+ } ;
47
58const bigPictureNode = document . querySelector ( '.big-picture' ) ;
69const socialCommentsNode = bigPictureNode . querySelector ( '.social__comments' ) ;
@@ -9,7 +12,6 @@ const commentsTotalCountNode = bigPictureNode.querySelector('.social__comment-to
912const socialCommentTemplate = socialCommentsNode . querySelector ( '.social__comment' ) ;
1013const commentsCountNode = bigPictureNode . querySelector ( '.social__comment-count' ) ;
1114const commentsLoaderNode = bigPictureNode . querySelector ( '.social__comments-loader' ) ;
12- // socialCommentsNode.innerHTML = '';
1315
1416const createComment = ( { avatar, name, message} ) => {
1517 const socialCommentNode = socialCommentTemplate . cloneNode ( true ) ;
@@ -24,38 +26,46 @@ const createComment = ({ avatar, name, message}) => {
2426
2527const 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
4348const 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
5257const 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