Skip to content

Commit 9e8b9a1

Browse files
authored
Merge pull request #21 from bessaliy/master
2 parents f740649 + f42af78 commit 9e8b9a1

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

js/form.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ const errorElement = sendErrorTemplate.content.cloneNode(true);
1616
const errorModal = errorElement.querySelector('.error');
1717
const errorButton = errorModal.querySelector('.error__button');
1818

19+
let successShown = false;
20+
let errorShown = false;
21+
let shouldCloseForm = true;
22+
23+
1924
const blockSubmitButton = () => {
2025
submitButton.disabled = true;
2126
submitButton.textContent = 'Публикую...';
@@ -28,7 +33,7 @@ const unblockSubmitButton = () => {
2833

2934
const changeEventListeners = (action) => {
3035
const method = `${action}EventListener`;
31-
document[method]('keydown', onEscPress);
36+
document[method]('keydown', onHandleEscapeKey);
3237
document[method]('click', onOutsideClick);
3338
};
3439

@@ -40,6 +45,7 @@ const onCloseSuccessModal = () => {
4045
const onCloseErrorModal = () => {
4146
errorModal.remove();
4247
changeEventListeners('remove');
48+
shouldCloseForm = true;
4349
};
4450

4551
const switchForm = (param1, param2) => {
@@ -51,33 +57,39 @@ const onCloseForm = () => {
5157
switchForm('add', 'remove');
5258
resetForm();
5359
};
54-
55-
const onHandleEscapeKey = (evt) => {
60+
function onHandleEscapeKey(evt) {
5661
if (evt.key === 'Escape') {
62+
5763
const excludedFields = [hashtagField, commentField];
5864
const activeElement = document.activeElement;
65+
shouldCloseForm = true;
5966

60-
let shouldClose = true;
6167
excludedFields.forEach((field) => {
6268
if (field === activeElement) {
63-
shouldClose = false;
69+
shouldCloseForm = false;
6470
}
6571
});
6672

67-
if (shouldClose) {
68-
onCloseForm();
73+
if (errorShown) {
74+
shouldCloseForm = false;
75+
errorShown = false;
76+
onCloseErrorModal();
77+
document.addEventListener('keydown', onHandleEscapeKey);
78+
return;
6979
}
70-
}
71-
};
7280

73-
function onEscPress(evt) {
74-
if (evt.key === 'Escape') {
81+
if (successShown) {
82+
onCloseSuccessModal();
83+
}
7584

76-
onCloseSuccessModal();
77-
onCloseErrorModal();
85+
if (shouldCloseForm) {
86+
onCloseForm();
87+
}
7888
}
89+
shouldCloseForm = true;
7990
}
8091

92+
8193
function onOutsideClick(evt) {
8294
if (!evt.target.closest('.success__inner')) {
8395
onCloseSuccessModal();
@@ -112,7 +124,6 @@ function resetForm() {
112124
}
113125
}
114126

115-
116127
formOpener.addEventListener('click', () => {
117128
switchForm('remove', 'add');
118129
});
@@ -182,13 +193,14 @@ const showSuccessMessage = () => {
182193

183194
successButton.addEventListener('click', onCloseSuccessModal);
184195
changeEventListeners('add');
196+
successShown = true;
185197
};
186198

187199
const showErrorMessage = () => {
188200
document.body.appendChild(errorModal);
189-
190201
errorButton.addEventListener('click', onCloseErrorModal);
191202
changeEventListeners('add');
203+
errorShown = true;
192204
};
193205

194206
form.addEventListener('submit', async (evt) => {
@@ -224,7 +236,6 @@ form.addEventListener('submit', async (evt) => {
224236
});
225237

226238
formCloseBtn.addEventListener('click', onCloseForm);
227-
document.addEventListener('keydown', onHandleEscapeKey);
228239

229240
const cancelButton = document.querySelector('.img-upload__cancel');
230241
if (cancelButton) {

js/picture-from-api.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { photoTemplate, errorTemplate } from './templates.js';
22
import {getData, Method, Route, ErrorText} from './api.js';
33

4-
export const container = document.querySelector('.pictures');
5-
const photosTotal = [];
6-
74
const COMMENTS_ON_SHOW = 5;
85

6+
export const container = document.querySelector('.pictures');
7+
const photosTotal = [];
98
const openBigPicture = document.querySelector('.big-picture');
109
const bigPicture = document.querySelector('.big-picture__img');
1110
const closeBigPicture = document.querySelector('.big-picture__cancel');

0 commit comments

Comments
 (0)