Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Открывается и закрывается (часть 2) #9

Merged
merged 3 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions js/big-photo-viewer.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
import {fillCommentsList} from './comments-viewer.js';
import {fillCommentsList, loadMoreComments} from './comments-viewer.js';

const picturesContainerElement = document.querySelector('.pictures');
const bigPhotoElement = document.querySelector('.big-picture');
const looadMoreCommentsElement = document.querySelector('.comments-loader');

const bodyElement = document.body;
let photoData = [];

picturesContainerElement.addEventListener('click', (evt) => {
const targetPhotoId = evt.target.closest('.picture');
const targetPhoto = photoData.find((photo) => photo.photoId === Number(targetPhotoId.dataset.photoId));
const {url, description, likes, comments} = targetPhoto;

bigPhotoElement.querySelector('.social__comment-count').classList.add('hidden');
bigPhotoElement.querySelector('.comments-loader').classList.add('hidden');
const {url, description, likes} = targetPhoto;

bigPhotoElement.querySelector('.big-picture__img img').setAttribute('src', url);
bigPhotoElement.querySelector('.likes-count').textContent = likes;
bigPhotoElement.querySelector('.social__comment-total-count').textContent = comments.length;
bigPhotoElement.querySelector('.social__caption').textContent = description;

bigPhotoElement.classList.remove('hidden');
bodyElement.classList.add('modal-open');

fillCommentsList(targetPhoto.comments);

bigPhotoElement.querySelector('.social__comment-shown-count').textContent = comments.length;

bigPhotoElement.setAttribute('tabindex', '0');
bigPhotoElement.focus();
});
Expand All @@ -42,6 +37,10 @@ bigPhotoElement.addEventListener('keydown', (evt) => {
}
});

looadMoreCommentsElement.addEventListener('click', () => {
loadMoreComments();
});

function closeBigPhoto() {
bigPhotoElement.classList.add('hidden');
bodyElement.classList.remove('modal-open');
Expand Down
63 changes: 52 additions & 11 deletions js/comments-viewer.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,59 @@
const socialCommentsElement = document.querySelector('.social__comments');
const commentTemplateElement = socialCommentsElement.querySelector('.social__comment');
const bigPictureElement = document.querySelector('.big-picture');
const loadCommentElement = bigPictureElement.querySelector('.social__comment-count');
const currentCommentsCountElement = bigPictureElement.querySelector('.social__comment-shown-count');
const totalCommentsCountElement = bigPictureElement.querySelector('.social__comment-total-count');
const loadCommentsButtonElement = bigPictureElement.querySelector('.social__comments-loader');
const loadingStep = 5;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Оформи как константу плиз

let commentsArray = [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

У массива не let, а const

let totalComments;
let currentCommentsLoaded = 0;

function fillCommentsList(comments) {
socialCommentsElement.innerHTML = '';
comments.forEach((comment) => {
const {avatar, name, commentsList} = comment;
const newCommentElement = commentTemplateElement.cloneNode(true);
const commentPictureElement = newCommentElement.querySelector('.social__picture');

commentPictureElement.setAttribute('src', avatar);
commentPictureElement.setAttribute('alt', name);
newCommentElement.querySelector('.social__text').textContent = commentsList;
socialCommentsElement.appendChild(newCommentElement);
});

if (comments.length > 0) {
commentsArray = comments;
currentCommentsLoaded = 0;
totalComments = comments.length;

const commentsToLoad = Math.min(loadingStep, totalComments);
for (let i = 0; i < commentsToLoad; i++) {
addCommentAndUpdateCounter(commentsArray[i]);
}
loadCommentsButtonElement.classList.toggle('hidden', currentCommentsLoaded >= totalComments);
} else {
loadCommentElement.innerHTML = '';
loadCommentElement.textContent = 'Нет комментариев';
loadCommentsButtonElement.classList.toggle('hidden', currentCommentsLoaded >= totalComments);
}
}

function loadMoreComments() {
const commentsToLoad = Math.min(loadingStep, totalComments - currentCommentsLoaded);

for (let i = 0; i < commentsToLoad; i++) {
addCommentAndUpdateCounter(commentsArray[currentCommentsLoaded]);
}
loadCommentsButtonElement.classList.toggle('hidden', currentCommentsLoaded >= totalComments);
}

function addComment({avatar, name, commentsList}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addComment можно вообще вынести в отдельный файл и обозвать например renderComment

const newCommentElement = commentTemplateElement.cloneNode(true);
const commentPictureElement = newCommentElement.querySelector('.social__picture');

commentPictureElement.setAttribute('src', avatar);
commentPictureElement.setAttribute('alt', name);
newCommentElement.querySelector('.social__text').textContent = commentsList;
socialCommentsElement.appendChild(newCommentElement);
}

function addCommentAndUpdateCounter(comment) {
addComment(comment);
currentCommentsLoaded++;
currentCommentsCountElement.textContent = currentCommentsLoaded;
totalCommentsCountElement.textContent = totalComments;
}

export {fillCommentsList};
export {fillCommentsList, loadMoreComments};
2 changes: 1 addition & 1 deletion js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function generateUniqueInteger(getRandomInt, min, max) {
function getRandomArrayElements(array, getRandomInt, maxLength = 1) {
const elements = [];
for (let i = 0; i < maxLength; i++) {
elements.push(array[getRandomInt(0, array.length)]);
elements.push(array[getRandomInt(0, array.length - 1)]);
}

return elements.join(' ');
Expand Down
Loading