diff --git a/js/big-picture-popup.js b/js/big-picture-popup.js
index 2037cc6..9d2a09c 100644
--- a/js/big-picture-popup.js
+++ b/js/big-picture-popup.js
@@ -7,7 +7,7 @@ const popupCancelElement = document.querySelector('.big-picture__cancel');
const commentsLoadBtn = bigPhotoPopup.querySelector('.comments-loader');
let currentComments = null;
-let currentCommentsCount = 0;
+let currentShownCommentsCount = 0;
function onEscapeKeydown(e) {
if (e.key === 'Escape') {
@@ -21,8 +21,8 @@ function onLoadBtnClick(e) {
}
function loadComments() {
- if (currentComments.length > currentCommentsCount) {
- currentComments.slice(currentCommentsCount, currentCommentsCount + COMMENTS_STEP).forEach((comment) => {
+ if (currentComments.length > currentShownCommentsCount) {
+ currentComments.slice(currentShownCommentsCount, currentShownCommentsCount + COMMENTS_STEP).forEach((comment) => {
const commEl = document.createElement('li');
commEl.className = 'social__comment';
@@ -40,16 +40,17 @@ function loadComments() {
commEl.append(imgEl);
commEl.append(pEl);
bigPhotoPopup.querySelector('.social__comments').append(commEl);
- currentCommentsCount++;
+ currentShownCommentsCount++;
});
bigPhotoPopup.querySelector('.social__comment-shown-count').textContent = bigPhotoPopup.querySelectorAll('.social__comment').length;
+ checkShownCommentsCount();
}
}
function clearComments() {
bigPhotoPopup.querySelector('.social__comments').innerHTML = '';
- currentCommentsCount = 0;
+ currentShownCommentsCount = 0;
}
function renderComments(comments) {
@@ -85,6 +86,14 @@ function openBigPhotoPopup(photoId) {
}
}
+function checkShownCommentsCount() {
+ if (currentComments.length <= currentShownCommentsCount) {
+ hide(commentsLoadBtn);
+ } else {
+ show(commentsLoadBtn);
+ }
+}
+
popupCancelElement.addEventListener('click', hidePhotoPopup);
commentsLoadBtn.addEventListener('click', onLoadBtnClick);
export { openBigPhotoPopup };