@@ -16,6 +16,11 @@ const errorElement = sendErrorTemplate.content.cloneNode(true);
1616const errorModal = errorElement . querySelector ( '.error' ) ;
1717const errorButton = errorModal . querySelector ( '.error__button' ) ;
1818
19+ let successShown = false ;
20+ let errorShown = false ;
21+ let shouldCloseForm = true ;
22+
23+
1924const blockSubmitButton = ( ) => {
2025 submitButton . disabled = true ;
2126 submitButton . textContent = 'Публикую...' ;
@@ -28,7 +33,7 @@ const unblockSubmitButton = () => {
2833
2934const 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 = () => {
4045const onCloseErrorModal = ( ) => {
4146 errorModal . remove ( ) ;
4247 changeEventListeners ( 'remove' ) ;
48+ shouldCloseForm = true ;
4349} ;
4450
4551const 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+
8193function onOutsideClick ( evt ) {
8294 if ( ! evt . target . closest ( '.success__inner' ) ) {
8395 onCloseSuccessModal ( ) ;
@@ -112,7 +124,6 @@ function resetForm() {
112124 }
113125}
114126
115-
116127formOpener . 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
187199const showErrorMessage = ( ) => {
188200 document . body . appendChild ( errorModal ) ;
189-
190201 errorButton . addEventListener ( 'click' , onCloseErrorModal ) ;
191202 changeEventListeners ( 'add' ) ;
203+ errorShown = true ;
192204} ;
193205
194206form . addEventListener ( 'submit' , async ( evt ) => {
@@ -224,7 +236,6 @@ form.addEventListener('submit', async (evt) => {
224236} ) ;
225237
226238formCloseBtn . addEventListener ( 'click' , onCloseForm ) ;
227- document . addEventListener ( 'keydown' , onHandleEscapeKey ) ;
228239
229240const cancelButton = document . querySelector ( '.img-upload__cancel' ) ;
230241if ( cancelButton ) {
0 commit comments