-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from TatianaSenatorova/module8-task2
- Loading branch information
Showing
3 changed files
with
64 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,81 @@ | ||
let indexCounter = 0; | ||
const OPEN_COMMENTS_ON_CLICK = 5; | ||
let commentsData = []; | ||
|
||
const bigPicture = document.querySelector('.big-picture'); | ||
const bigPictureImg = bigPicture.querySelector('.big-picture__img img'); | ||
const likesCount = bigPicture.querySelector('.likes-count'); | ||
const commentsCountFullPhoto = bigPicture.querySelector('.social__comment-count'); | ||
const shownComments = commentsCountFullPhoto.querySelector('.social__comment-shown-count'); | ||
const totalComments = commentsCountFullPhoto.querySelector('.social__comment-total-count'); | ||
const commentsCountFullPhoto = bigPicture.querySelector( | ||
'.social__comment-count' | ||
); | ||
const shownComments = commentsCountFullPhoto.querySelector( | ||
'.social__comment-shown-count' | ||
); | ||
const totalComments = commentsCountFullPhoto.querySelector( | ||
'.social__comment-total-count' | ||
); | ||
const commentsList = bigPicture.querySelector('.social__comments'); | ||
const bigPictureDescription = bigPicture.querySelector('.social__caption'); | ||
const buttonMoreComments = bigPicture.querySelector('.comments-loader'); | ||
|
||
const renderCommentsFullPhoto = (comments) =>{ | ||
commentsCountFullPhoto.classList.add('hidden'); | ||
buttonMoreComments.classList.add('hidden'); | ||
comments.forEach((comment) => { | ||
const renderComments = (nextComments) => { | ||
nextComments.forEach((comment) => { | ||
commentsList.insertAdjacentHTML( | ||
'afterbegin', | ||
`<li class="social__comment"> | ||
'beforeend', | ||
`<li class='social__comment'> | ||
<img | ||
class="social__picture" | ||
src="${comment.avatar}" | ||
alt="${comment.name}" | ||
width="35" height="35"> | ||
<p class="social__text">${comment.message}</p> | ||
</li>`, | ||
class='social__picture' | ||
src='${comment.avatar}' | ||
alt='${comment.name}' | ||
width='35' height='35'> | ||
<p class='social__text'>${comment.message}</p> | ||
</li>` | ||
); | ||
}); | ||
}; | ||
|
||
const disableShowcomments = (commentsLength) => { | ||
buttonMoreComments.disabled = true; | ||
shownComments.textContent = commentsLength; | ||
}; | ||
|
||
const getCommentsToRender = (comments) => { | ||
if(comments.length === 0) { | ||
disableShowcomments(comments.length); | ||
return comments; | ||
} | ||
indexCounter += OPEN_COMMENTS_ON_CLICK; | ||
if (comments.length <= OPEN_COMMENTS_ON_CLICK) { | ||
disableShowcomments(comments.length); | ||
return comments; | ||
} | ||
if (indexCounter >= comments.length) { | ||
disableShowcomments(comments.length); | ||
} else { | ||
shownComments.textContent = indexCounter; | ||
} | ||
return comments.slice(indexCounter - OPEN_COMMENTS_ON_CLICK, indexCounter); | ||
}; | ||
|
||
const renderFullPhoto = (chosenPhotoID, listPhotos) => { | ||
const dataForBigPhoto = listPhotos.find((item) => item.id === chosenPhotoID); | ||
bigPictureImg.src = dataForBigPhoto.url; | ||
likesCount.textContent = dataForBigPhoto.likes; | ||
shownComments.textContent = dataForBigPhoto.comments.length; | ||
totalComments.textContent = dataForBigPhoto.comments.length; | ||
bigPictureDescription.textContent = dataForBigPhoto.description; | ||
renderCommentsFullPhoto(dataForBigPhoto.comments); | ||
commentsData = dataForBigPhoto.comments; | ||
renderComments(getCommentsToRender(commentsData)); | ||
}; | ||
|
||
buttonMoreComments.addEventListener('click', () => { | ||
renderComments(getCommentsToRender(commentsData)); | ||
}); | ||
|
||
const clearComments = () => { | ||
commentsList.innerHTML = ''; | ||
buttonMoreComments.disabled = false; | ||
commentsData = []; | ||
indexCounter = 0; | ||
}; | ||
|
||
export { renderFullPhoto }; | ||
export { renderFullPhoto, clearComments }; |