Skip to content

Commit 6e26207

Browse files
authored
Merge pull request #17 from khepriKreator/module12-task2
2 parents eb7fd04 + f67c542 commit 6e26207

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

js/upload-image.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const FILE_TYPES = ['jpg', 'jpeg', 'png'];
2+
3+
const imagePreviewWrapper = document.querySelector('.img-upload__preview');
4+
const imagePreview = imagePreviewWrapper.querySelector('img');
5+
6+
const handleUpload = (target) => {
7+
const file = target.files[0];
8+
const fileName = file.name.toLowerCase();
9+
10+
const matches = FILE_TYPES.some((it) => fileName.endsWith(it));
11+
12+
if(matches) {
13+
imagePreview.src = URL.createObjectURL(file);
14+
}
15+
};
16+
17+
export {
18+
handleUpload,
19+
};

js/upload-modal.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { checkValidaty } from './form-validation.js';
55
import { resetEffects } from './effects.js';
66
import { postPhoto } from './api.js';
77
import { showPostSuccessMessage } from './messages.js';
8+
import {handleUpload} from './upload-image.js';
89

910
const modalWindow = document.querySelector('.img-upload__overlay');
1011
const uploadForm = document.querySelector('.img-upload__form');
@@ -13,11 +14,15 @@ const modalCloseButton = document.querySelector('.img-upload__cancel');
1314
const hashTagsInput = document.querySelector('.text__hashtags');
1415
const descriptionInput = document.querySelector('.text__description');
1516

17+
1618
uploadForm.method = 'post';
1719
uploadForm.action = 'https://31.javascript.htmlacademy.pro/kekstagram';
1820
uploadForm.enctype = 'multipart/form-data';
1921

20-
const onUploadClick = () => handleOpen();
22+
const onUploadClick = ({target}) => {
23+
handleUpload(target);
24+
handleOpen();
25+
};
2126
const onEscapeKeydown = (evt) => {
2227
if (document.activeElement === hashTagsInput
2328
||

0 commit comments

Comments
 (0)