@@ -10,6 +10,9 @@ const uploadCancel = document.querySelector('#upload-cancel');
1010const submitButton = document . querySelector ( '#upload-submit' ) ;
1111const hashtagsInput = document . querySelector ( '.text__hashtags' ) ;
1212const descriptionInput = document . querySelector ( '.text__description' ) ;
13+ const imgUploadPreview = document . querySelector ( '.img-upload__preview img' ) ;
14+
15+ const DEFAULT_PREVIEW_SRC = 'img/upload-default-image.jpg' ;
1316
1417const MAX_HASHTAGS = 5 ;
1518const MAX_HASHTAG_LENGTH = 20 ;
@@ -50,9 +53,41 @@ const validateHashtags = (value) => {
5053 } ) ;
5154} ;
5255
56+ const getHashtagErrorMessage = ( value ) => {
57+ if ( ! value . trim ( ) ) {
58+ return '' ;
59+ }
60+
61+ const hashtags = value . trim ( ) . split ( / \s + / ) ;
62+
63+ if ( hashtags . length > MAX_HASHTAGS ) {
64+ return `Максимум ${ MAX_HASHTAGS } хэш-тегов` ;
65+ }
66+
67+ const lowerHashtags = hashtags . map ( ( tag ) => tag . toLowerCase ( ) ) ;
68+ const uniqueHashtags = new Set ( lowerHashtags ) ;
69+
70+ if ( uniqueHashtags . size !== hashtags . length ) {
71+ return 'Хэш-теги не должны повторяться' ;
72+ }
73+
74+ const invalidTag = hashtags . find ( ( hashtag ) => {
75+ if ( hashtag . length > MAX_HASHTAG_LENGTH ) {
76+ return true ;
77+ }
78+ return ! VALID_HASHTAG_REGEX . test ( hashtag ) ;
79+ } ) ;
80+
81+ if ( invalidTag ) {
82+ return 'Некорректный хэш-тег' ;
83+ }
84+
85+ return '' ;
86+ } ;
87+
5388const validateDescription = ( value ) => value . length <= 140 ;
5489
55- pristine . addValidator ( hashtagsInput , validateHashtags , 'Ошибка в хэш-тегах' ) ;
90+ pristine . addValidator ( hashtagsInput , validateHashtags , getHashtagErrorMessage ) ;
5691pristine . addValidator ( descriptionInput , validateDescription , 'Комментарий не должен быть длинее 140 символов' ) ;
5792
5893const openForm = ( ) => {
@@ -67,9 +102,14 @@ const closeForm = () => {
67102 pristine . reset ( ) ;
68103 resetScale ( ) ;
69104 resetEffect ( ) ;
105+ imgUploadPreview . src = DEFAULT_PREVIEW_SRC ;
70106} ;
71107
72108const onUploadFileInputChange = ( ) => {
109+ const file = uploadFileInput . files [ 0 ] ;
110+ if ( file ) {
111+ imgUploadPreview . src = URL . createObjectURL ( file ) ;
112+ }
73113 openForm ( ) ;
74114} ;
75115
0 commit comments