11import { isEscKey } from './utils.js' ;
2- import { initFormValidation , resetValidation } from './form-validation.js' ;
2+ import { renderSendInfoModal } from './info-modal-send.js' ;
3+ import { isFormValid , resetValidation } from './form-validation.js' ;
34import { initScalePhoto , resetScaleValue } from './scale-photo.js' ;
45import { initPhotoFilters , resetPhotoFilters } from './photo-filters.js' ;
6+ import { sendData } from './api.js' ;
7+
8+ const SubmitButtonText = {
9+ IDLE : 'Отправить' ,
10+ SENDING : 'Отправляю...'
11+ } ;
512
613const bodyElement = document . querySelector ( 'body' ) ;
714const formElement = bodyElement . querySelector ( '.img-upload__form' ) ;
815const modalFormElement = formElement . querySelector ( '.img-upload__overlay' ) ;
916const uploadControlElement = formElement . querySelector ( '.img-upload__input' ) ;
1017const modalFormCloseElement = modalFormElement . querySelector ( '.img-upload__cancel' ) ;
1118const sliderWrapperElement = modalFormElement . querySelector ( '.img-upload__effect-level' ) ;
19+ const submitButtonElement = modalFormElement . querySelector ( '.img-upload__submit' ) ;
1220
1321const onDocumentKeydown = ( evt ) => {
1422 if ( isEscKey ( evt ) ) {
@@ -21,6 +29,16 @@ const onDocumentKeydown = (evt) => {
2129 }
2230} ;
2331
32+ const disableSubmitButton = ( ) => {
33+ submitButtonElement . disabled = true ;
34+ submitButtonElement . textContent = SubmitButtonText . SENDING ;
35+ } ;
36+
37+ const enableSubmitButton = ( ) => {
38+ submitButtonElement . disabled = false ;
39+ submitButtonElement . textContent = SubmitButtonText . IDLE ;
40+ } ;
41+
2442function openModalForm ( ) {
2543 modalFormElement . classList . remove ( 'hidden' ) ;
2644 bodyElement . classList . add ( 'modal-open' ) ;
@@ -43,6 +61,23 @@ const initModalForm = () => {
4361 return ;
4462 }
4563
64+ formElement . addEventListener ( 'submit' , ( evt ) => {
65+ evt . preventDefault ( ) ;
66+
67+ if ( isFormValid ( ) ) {
68+ const formData = new FormData ( evt . target ) ;
69+ disableSubmitButton ( ) ;
70+ sendData ( formData ) . then ( ( ) => {
71+ closeModalForm ( ) ;
72+ renderSendInfoModal ( 'success' ) ;
73+ } ) . catch ( ( ) => {
74+ renderSendInfoModal ( 'error' ) ;
75+ } ) . finally ( ( ) => {
76+ enableSubmitButton ( ) ;
77+ } ) ;
78+ }
79+ } ) ;
80+
4681 uploadControlElement . addEventListener ( 'change' , ( ) => {
4782 openModalForm ( ) ;
4883 } ) ;
@@ -51,7 +86,6 @@ const initModalForm = () => {
5186 closeModalForm ( ) ;
5287 } ) ;
5388
54- initFormValidation ( ) ;
5589 initScalePhoto ( ) ;
5690 initPhotoFilters ( ) ;
5791} ;
0 commit comments