From f67c542cb52632be7c16ad7cc1813636f86d4349 Mon Sep 17 00:00:00 2001 From: khepri_kreator Date: Sun, 26 Apr 2026 13:57:10 +0300 Subject: [PATCH] created upload photo func Co-authored-by: Copilot --- js/upload-image.js | 19 +++++++++++++++++++ js/upload-modal.js | 7 ++++++- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 js/upload-image.js diff --git a/js/upload-image.js b/js/upload-image.js new file mode 100644 index 0000000..fd44290 --- /dev/null +++ b/js/upload-image.js @@ -0,0 +1,19 @@ +const FILE_TYPES = ['jpg', 'jpeg', 'png']; + +const imagePreviewWrapper = document.querySelector('.img-upload__preview'); +const imagePreview = imagePreviewWrapper.querySelector('img'); + +const handleUpload = (target) => { + const file = target.files[0]; + const fileName = file.name.toLowerCase(); + + const matches = FILE_TYPES.some((it) => fileName.endsWith(it)); + + if(matches) { + imagePreview.src = URL.createObjectURL(file); + } +}; + +export { + handleUpload, +}; diff --git a/js/upload-modal.js b/js/upload-modal.js index eba95fe..d87331c 100644 --- a/js/upload-modal.js +++ b/js/upload-modal.js @@ -5,6 +5,7 @@ import { checkValidaty } from './form-validation.js'; import { resetEffects } from './effects.js'; import { postPhoto } from './api.js'; import { showPostSuccessMessage } from './messages.js'; +import {handleUpload} from './upload-image.js'; const modalWindow = document.querySelector('.img-upload__overlay'); const uploadForm = document.querySelector('.img-upload__form'); @@ -13,11 +14,15 @@ const modalCloseButton = document.querySelector('.img-upload__cancel'); const hashTagsInput = document.querySelector('.text__hashtags'); const descriptionInput = document.querySelector('.text__description'); + uploadForm.method = 'post'; uploadForm.action = 'https://31.javascript.htmlacademy.pro/kekstagram'; uploadForm.enctype = 'multipart/form-data'; -const onUploadClick = () => handleOpen(); +const onUploadClick = ({target}) => { + handleUpload(target); + handleOpen(); +}; const onEscapeKeydown = (evt) => { if (document.activeElement === hashTagsInput ||