1
1
import { isEscapeKey } from './util.js' ;
2
+ import { uploadData } from './api.js' ;
2
3
import { imagePreview , scaleValueField } from './image-scaling.js' ;
3
4
import { sliderElementWrapper } from './image-effect.js' ;
4
5
import { validateAllHashtags , validateHashtagError } from './hashtag-validation.js' ;
5
- import { validateComment } from './comment-validation.js' ;
6
+ import { validateComment , validateCommentError } from './comment-validation.js' ;
6
7
7
8
const imageUploadForm = document . querySelector ( '.img-upload__form' ) ;
8
9
const imageUploadNode = imageUploadForm . querySelector ( '.img-upload__overlay' ) ;
9
10
const imageUploadInput = imageUploadForm . querySelector ( '.img-upload__input' ) ;
11
+ const imageUploadSubmit = imageUploadForm . querySelector ( '.img-upload__submit' ) ;
10
12
const imageUploadCloseButton = imageUploadForm . querySelector ( '.img-upload__cancel' ) ;
11
13
const effectOriginal = imageUploadForm . querySelector ( '#effect-none' ) ;
12
14
const hashtagsInput = imageUploadForm . querySelector ( '.text__hashtags' ) ;
@@ -19,6 +21,14 @@ const pristine = new Pristine(imageUploadForm, {
19
21
errorTextTag : 'div' ,
20
22
} , false ) ;
21
23
24
+ const blockSubmitButton = ( ) => {
25
+ imageUploadSubmit . disabled = true ;
26
+ } ;
27
+
28
+ const unblockSubmitButton = ( ) => {
29
+ imageUploadSubmit . disabled = false ;
30
+ } ;
31
+
22
32
const onDocumentKeydown = ( evt ) => {
23
33
if ( isEscapeKey ( evt ) ) {
24
34
evt . preventDefault ( ) ;
@@ -30,21 +40,24 @@ const onDocumentKeydown = (evt) => {
30
40
} ;
31
41
32
42
function uploadFormClear ( ) {
33
- imageUploadInput . value = '' ;
34
43
hashtagsInput . value = '' ;
35
44
commentInput . value = '' ;
36
45
effectOriginal . checked = true ;
37
46
sliderElementWrapper . classList . add ( 'hidden' ) ;
38
- imagePreview . style . transform = '' ;
39
47
imagePreview . style . filter = '' ;
40
48
scaleValueField . value = '100%' ;
49
+ imagePreview . style . transform = '' ;
41
50
}
42
51
43
- function uploadFormCloseHandler ( ) {
52
+ function uploadFormCloseHandler ( _ , uploadError = false ) {
44
53
imageUploadNode . classList . add ( 'hidden' ) ;
45
54
document . body . classList . remove ( 'modal-open' ) ;
55
+ imageUploadInput . value = '' ;
56
+
57
+ if ( ! uploadError ) {
58
+ uploadFormClear ( ) ;
59
+ }
46
60
47
- uploadFormClear ( ) ;
48
61
document . removeEventListener ( 'keydown' , onDocumentKeydown ) ;
49
62
imageUploadCloseButton . removeEventListener ( 'click' , uploadFormCloseHandler ) ;
50
63
}
@@ -65,12 +78,23 @@ imageUploadInput.addEventListener('change', ()=> {
65
78
} ) ;
66
79
67
80
pristine . addValidator ( imageUploadForm . querySelector ( '.text__hashtags' ) , validateAllHashtags , validateHashtagError ) ;
68
- pristine . addValidator ( imageUploadForm . querySelector ( '.text__description' ) , validateComment , 'Длина комментария не может составлять больше 140 символов' ) ;
81
+ pristine . addValidator ( imageUploadForm . querySelector ( '.text__description' ) , validateComment , validateCommentError ) ;
69
82
70
- imageUploadForm . addEventListener ( 'submit' , ( evt ) => {
71
- evt . preventDefault ( ) ;
72
- pristine . validate ( ) ;
73
83
74
- imageUploadForm . removeEventListener ( 'keydown' , uploadFormNoEscWhenInputActive ) ;
75
- imageUploadCloseButton . removeEventListener ( 'click' , uploadFormCloseHandler ) ;
76
- } ) ;
84
+ const setImageUploadFormSubmit = ( ) => {
85
+ imageUploadForm . addEventListener ( 'submit' , ( evt ) => {
86
+ evt . preventDefault ( ) ;
87
+
88
+ const isValid = pristine . validate ( ) ;
89
+ if ( isValid ) {
90
+ const formData = new FormData ( evt . target ) ;
91
+ blockSubmitButton ( ) ;
92
+ uploadData ( formData ) ;
93
+ }
94
+
95
+ imageUploadForm . removeEventListener ( 'keydown' , uploadFormNoEscWhenInputActive ) ;
96
+ imageUploadCloseButton . removeEventListener ( 'click' , uploadFormCloseHandler ) ;
97
+ } ) ;
98
+ } ;
99
+
100
+ export { setImageUploadFormSubmit , uploadFormCloseHandler , unblockSubmitButton } ;
0 commit comments