From d08f4e7eb66299bdc32826aa2bbd8f7bf0b93847 Mon Sep 17 00:00:00 2001 From: Nikita Khrulev Date: Thu, 5 Dec 2024 11:15:10 +0300 Subject: [PATCH] fixes --- js/main.js | 1 - js/post.js | 17 +++++++++-------- js/utils.js | 8 +++++++- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/js/main.js b/js/main.js index 748a1d3..66df91a 100644 --- a/js/main.js +++ b/js/main.js @@ -1,4 +1,3 @@ -import './post.js'; import { createMockPosts } from './mock.js'; import { printThumbnails } from './thumbnails.js'; import { picturesContainer } from './thumbnails.js'; diff --git a/js/post.js b/js/post.js index 2d31cc0..93cb1e9 100644 --- a/js/post.js +++ b/js/post.js @@ -1,3 +1,5 @@ +import { escKeypress } from './utils'; + const postModal = document.querySelector('.big-picture'); const closeModalBtn = document.querySelector('.big-picture__cancel'); const postComments = document.querySelector('.social__comments'); @@ -9,11 +11,6 @@ const closePostModal = () => { document.body.classList.remove('modal-open'); }; -document.addEventListener('keydown', (evt) => { - if (evt.key === 'Escape') { - closePostModal(); - } -}); const insertPostComments = (commentsArr) => { const commentsFragment = document.createDocumentFragment(); @@ -33,6 +30,7 @@ const insertPostComments = (commentsArr) => { }; const openPostModal = (content) => { + postModal.classList.remove('hidden'); document.body.classList.add('modal-open'); postModal.querySelector('img').src = content.url; postModal.querySelector('.likes-count').textContent = content.likes; @@ -48,9 +46,12 @@ const openPostModal = (content) => { const findPostContent = (evt, data) => { const url = evt.target.closest('.picture').querySelector('img').src; const photoId = Number(url.split('/').pop().split('.')[0]); - const res = data.find((photo) => photo.id === photoId); - postModal.classList.remove('hidden'); - openPostModal(res); + const postData = data.find((photo) => photo.id === photoId); + openPostModal(postData); }; +document.addEventListener('keydown', (evt) => { + escKeypress(evt, closePostModal); +}); + export {findPostContent}; diff --git a/js/utils.js b/js/utils.js index c4b1eb7..a327d53 100644 --- a/js/utils.js +++ b/js/utils.js @@ -22,4 +22,10 @@ const createRandomId = (min, max) => { }; }; -export {createRandomId, getRandomInteger}; +const escKeypress = (evt, fn) => { + if (evt.key === 'Escape') { + fn(); + } +}; + +export {createRandomId, getRandomInteger, escKeypress};