11import { isHashtagValid , error } from './hashtag-validator' ;
22import { isEscapeKey } from './utils' ;
33import { onEffectChange , resetFilter } from './slider-effect.js' ;
4+ import { ErrorText , sendData , showDataError } from './api.js' ;
45
56const STEP = 25 ;
67const MIN_SCALE = 25 ;
78const MAX_SCALE = 100 ;
89
10+ const SubmitButtonText = {
11+ IDLE : 'Сохранить' ,
12+ SENDING : 'Сохраняю...'
13+ } ;
914
1015const uploadForm = document . querySelector ( '.img-upload__form' ) ;
1116const pageBody = document . body ;
@@ -22,6 +27,9 @@ const increaseScaleButton = uploadForm.querySelector('.scale__control--bigger');
2227const scaleValue = uploadForm . querySelector ( '.scale__control--value' ) ;
2328const uploadImagePreview = uploadForm . querySelector ( '.img-upload__preview img' ) ;
2429const effectsList = uploadForm . querySelector ( '.effects__list' ) ;
30+ const submitButton = uploadForm . querySelector ( '.img-upload__submit' ) ;
31+ const templateSuccess = document . querySelector ( '#success' ) . content ;
32+ const templateError = document . querySelector ( '#error' ) . content ;
2533
2634let currentScaleValue = parseInt ( scaleValue . value . slice ( 0 , - 1 ) , 10 ) ;
2735
@@ -45,6 +53,34 @@ function onDocumentKeyDown(evt) {
4553 }
4654}
4755
56+ function onCloseNotification ( evt ) {
57+ evt . stopPropagation ( ) ;
58+ const existElement = document . querySelector ( '.success' ) || document . querySelector ( '.error' ) ;
59+ const closeButton = existElement . querySelector ( 'button' ) ;
60+ if ( evt . target === existElement || evt . target === closeButton || isEscapeKey ( evt ) ) {
61+ existElement . remove ( ) ;
62+ pageBody . removeEventListener ( 'click' , onCloseNotification ) ;
63+ pageBody . removeEventListener ( 'keydown' , onCloseNotification ) ;
64+ }
65+ }
66+
67+ function appendNotification ( template ) {
68+ const notificationNode = template . cloneNode ( true ) ;
69+ pageBody . append ( notificationNode ) ;
70+ pageBody . addEventListener ( 'click' , onCloseNotification ) ;
71+ pageBody . addEventListener ( 'keydown' , onCloseNotification ) ;
72+ }
73+
74+ const blockSubmitButton = ( ) => {
75+ submitButton . disabled = true ;
76+ submitButton . textContent = SubmitButtonText . SENDING ;
77+ } ;
78+
79+ const unblockSubmitButton = ( ) => {
80+ submitButton . disabled = false ;
81+ submitButton . textContent = SubmitButtonText . IDLE ;
82+ } ;
83+
4884const pristine = new Pristine ( uploadForm , {
4985 classTo : 'img-upload__field-wrapper' ,
5086 errorClass : 'img-upload__field-wrapper--error' ,
@@ -59,7 +95,6 @@ function closePhotoEditorForm(){
5995 pristine . reset ( ) ;
6096 uploadForm . reset ( ) ;
6197 resetFilter ( ) ;
62-
6398}
6499
65100function initUploadModal ( ) {
@@ -78,10 +113,9 @@ function onScaleImage(direction) {
78113 let newValue = currentScaleValue + STEP * direction ;
79114 if ( newValue < MIN_SCALE ) {
80115 newValue = MIN_SCALE ;
81- } else if ( newValue > MAX_SCALE ) {
116+ } else if ( newValue > MAX_SCALE ) {
82117 newValue = MAX_SCALE ;
83118 }
84-
85119 currentScaleValue = newValue ;
86120 scaleValue . value = `${ currentScaleValue } %` ;
87121 uploadImagePreview . style . transform = `scale(${ currentScaleValue / MAX_SCALE } )` ;
@@ -97,7 +131,17 @@ function onFormSubmit(evt) {
97131 evt . preventDefault ( ) ;
98132 if ( pristine . validate ( ) ) {
99133 hashtagInput . value = hashtagInput . value . trim ( ) . replaceAll ( / \s + / g, ' ' ) ;
100- uploadForm . submit ( ) ;
134+ blockSubmitButton ( ) ;
135+ sendData ( new FormData ( evt . target ) )
136+ . then ( ( ) => {
137+ appendNotification ( templateSuccess ) ;
138+ closePhotoEditorForm ( ) ;
139+ } )
140+ . catch ( ( ) => {
141+ showDataError ( ErrorText . SEND_DATA ) ;
142+ appendNotification ( templateError ) ;
143+ } )
144+ . finally ( unblockSubmitButton ) ;
101145 }
102146}
103147
0 commit comments