Skip to content

Commit

Permalink
(bigpicture)[script]clear comments
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanMalkS committed Mar 25, 2024
1 parent b66730a commit c9f7ab4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
26 changes: 19 additions & 7 deletions js/bigPicture.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const renderImageAndDescription = (post) => {

const renderComments = (post) => {
const commentsContainer = document.querySelector('.social__comments');

while (commentsContainer.firstChild) {
commentsContainer.removeChild(commentsContainer.firstChild);
}

post.comments.forEach((comment) => {
const commentItem = document.createElement('li');
commentItem.classList.add('social__comment');
Expand Down Expand Up @@ -59,7 +64,15 @@ const close = () => {
bigPicture.classList.add('hidden');
};

export const closeBigPicture = () => {
const clickHandler = () => {
close();
const closeButton = document.getElementById('picture-cancel');
closeButton.removeEventListener('click', clickHandler);
};

const setupCloseHandlers = () => {
const closeButton = document.getElementById('picture-cancel');

const closeHandler = (event) => {
if (event.key === 'Escape' || event.code === 'Escape') {
close();
Expand All @@ -68,17 +81,16 @@ export const closeBigPicture = () => {
};

document.addEventListener('keydown', closeHandler);

const closeButton = document.getElementById('picture-cancel');
const clickHandler = () => {
close();
closeButton.removeEventListener('click', clickHandler);
};
closeButton.addEventListener('click', clickHandler);
};

export const showBigPicture = (post) => {
openModal();
renderImageAndDescription(post);
renderComments(post);

const closeButton = document.getElementById('picture-cancel');
closeButton.removeEventListener('click', clickHandler);

setupCloseHandlers(clickHandler);
};
4 changes: 1 addition & 3 deletions js/thumbnails.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { generateImagePosts } from './util.js';
import { showBigPicture, closeBigPicture } from './bigPicture.js';
import { showBigPicture } from './bigPicture.js';

const pictureTemplate = document.querySelector('#picture')
.content.querySelector('.picture');
Expand Down Expand Up @@ -36,5 +36,3 @@ export const createThumbnails = () => {

pictures.appendChild(fragment);
};

closeBigPicture();

0 comments on commit c9f7ab4

Please sign in to comment.