@@ -3,18 +3,21 @@ import { initScale, resetScale } from './image-scale';
33import { initValidation , validateForm } from './validation' ;
44import { initEffect , resetEffect } from './slider-effect' ;
55import { sendData } from '../load-data' ;
6- import { showSuccessMessage , showErrorMessage , errorMessageContainer } from '../messages' ;
6+ import { generateErrorMessage , showSuccessMessage , showErrorMessage , hasErrorMessage } from '../messages' ;
7+
8+ const FILE_TYPES = [ '.jpg' , '.jpeg' , '.png' , '.gif' ] ;
79
810const uploadForm = document . querySelector ( '.img-upload__form' ) ;
9- const uploadInput = uploadForm . querySelector ( '.img-upload__input' ) ;
1011const formOverlay = uploadForm . querySelector ( '.img-upload__overlay' ) ;
1112const buttonCloseUpload = uploadForm . querySelector ( '.img-upload__cancel' ) ;
1213const hashtagsInput = uploadForm . querySelector ( '.text__hashtags' ) ;
1314const descriptionInput = uploadForm . querySelector ( '.text__description' ) ;
14-
15+ const fileChooser = document . querySelector ( '.img-upload__wrapper input[type=file]' ) ;
16+ const preview = document . querySelector ( '.img-upload__preview img' ) ;
17+ const uploadFormEffects = uploadForm . querySelectorAll ( '.effects__preview' ) ;
1518
1619const onEscKeyDown = ( evt ) => {
17- if ( isEscapeKey ( evt ) && ( document . activeElement !== hashtagsInput || document . activeElement !== descriptionInput ) && ! errorMessageContainer ) {
20+ if ( isEscapeKey ( evt ) && ( document . activeElement !== hashtagsInput || document . activeElement !== descriptionInput ) && ! hasErrorMessage ) {
1821 closeUploadModal ( ) ;
1922 }
2023} ;
@@ -32,6 +35,8 @@ function closeUploadModal () {
3235 resetEffect ( ) ;
3336 resetScale ( ) ;
3437 uploadForm . reset ( ) ;
38+ fileChooser . reset ( ) ;
39+ // При повторной попытке отправить неверный формата файла ошибка не появляется. Подскажи как реализовать.
3540}
3641
3742const openUploadModal = ( ) => {
@@ -45,12 +50,33 @@ const openUploadModal = () => {
4550 initValidation ( ) ;
4651} ;
4752
48- const sendFormData = async ( formElement ) => {
53+ function onFileChooserChange ( ) {
54+ const file = fileChooser . files [ 0 ] ;
55+ const fileName = file . name . toLowerCase ( ) ;
56+
57+ const matches = FILE_TYPES . some ( ( item ) => fileName . endsWith ( item ) ) ;
58+
59+ if ( matches ) {
60+ preview . src = URL . createObjectURL ( file ) ;
61+ uploadFormEffects . forEach ( ( item ) => {
62+ item . style . backgroundImage = `url(${ URL . createObjectURL ( file ) } )` ;
63+ } ) ;
64+ } else {
65+ generateErrorMessage ( ) ;
66+ return ;
67+ }
68+
69+ openUploadModal ( ) ;
70+ }
71+
72+ const onFormSubmit = async ( evt ) => {
73+ evt . preventDefault ( ) ;
74+
4975 const isValid = validateForm ( ) ;
5076
5177 if ( isValid ) {
5278 try {
53- await sendData ( new FormData ( formElement ) ) ;
79+ await sendData ( new FormData ( evt . target ) ) ;
5480 closeUploadModal ( ) ;
5581 showSuccessMessage ( ) ;
5682 } catch ( error ) {
@@ -59,17 +85,9 @@ const sendFormData = async (formElement) => {
5985 }
6086} ;
6187
62- const onFormSubmit = ( evt ) => {
63- evt . preventDefault ( ) ;
64- sendFormData ( evt . target ) ;
65- } ;
66-
6788const initUploadModal = ( ) => {
6889 uploadForm . addEventListener ( 'submit' , onFormSubmit ) ;
69-
70- uploadInput . addEventListener ( 'change' , ( ) => {
71- openUploadModal ( ) ;
72- } ) ;
90+ fileChooser . addEventListener ( 'change' , onFileChooserChange ) ;
7391} ;
7492
7593export { initUploadModal } ;
0 commit comments