Skip to content

Commit 6295b75

Browse files
authored
Merge pull request #13 from natalya87324/module12-task2
Фото на память
2 parents 7cd2f88 + e953cc1 commit 6295b75

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

js/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ const sendData = (onSuccess, onFail, body) => {
5252
});
5353
};
5454

55-
export { getData, sendData };
55+
export { getData, sendData, showErrorMessage };

js/upload-photo.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { toggleClass, isEscapeKey } from './util.js';
22
import { updateScale, resetScale, updateEffect, resetSlider } from './update-photo-mode.js';
3-
import { sendData } from './api.js';
3+
import { sendData, showErrorMessage } from './api.js';
44

55
const HASHTAG_MAX_COUNT = 5;
6-
6+
const FILE_TYPES = ['gif', 'jpg', 'jpeg', 'png'];
77
const DESCRIPTION_MAX_LENGTH = 140;
88

99
const HashtagLength = {
@@ -14,6 +14,7 @@ const HashtagLength = {
1414
const photoUploadForm = document.querySelector('.img-upload__form');
1515
const photoUploadInput = photoUploadForm.querySelector('.img-upload__input');
1616
const photoUploadOverlay = photoUploadForm.querySelector('.img-upload__overlay');
17+
const preview = document.querySelector('.img-upload__preview img');
1718
const hashtagInput = photoUploadForm.querySelector('.text__hashtags');
1819
const photoDescriptionField = photoUploadForm.querySelector('.text__description');
1920
const cancelPhotoUploadBtn = photoUploadForm.querySelector('#upload-cancel');
@@ -133,11 +134,12 @@ const onCancelPhotoUpload = (evt) => {
133134

134135
const onPhotoUploadEscKey = (evt) => {
135136
const errorPopup = errorElement.querySelector('.popup');
136-
if (isEscapeKey(evt) && document.activeElement !== hashtagInput && document.activeElement !== photoDescriptionField && errorPopup) {
137+
if (isEscapeKey(evt) && document.activeElement !== hashtagInput && document.activeElement !== photoDescriptionField && !errorPopup) {
137138
evt.preventDefault();
138139
resetForm();
139140
closePhotoUploadForm();
140141
}
142+
document.removeEventListener('keydown', onPhotoUploadEscKey);
141143
};
142144

143145
const onOpenPhotoUploadForm = (evt) => {
@@ -147,6 +149,16 @@ const onOpenPhotoUploadForm = (evt) => {
147149
};
148150

149151
function openPhotoUploadForm() {
152+
const file = photoUploadInput.files[0];
153+
const fileName = file.name.toLowerCase();
154+
const matches = FILE_TYPES.some((type) => fileName.endsWith(type));
155+
156+
if (matches) {
157+
preview.src = URL.createObjectURL(file);
158+
} else {
159+
showErrorMessage();
160+
closePhotoUploadForm();
161+
}
150162
document.addEventListener('keydown', onPhotoUploadEscKey);
151163
toggleModal();
152164
toggleSubmitBtn();
@@ -171,7 +183,6 @@ const onFailUploadEscKey = (evt) => {
171183
if (isEscapeKey(evt)) {
172184
evt.preventDefault();
173185
errorSubmitMessage.remove();
174-
document.removeEventListener('keydown', onFailUploadEscKey);
175186
}
176187
};
177188

0 commit comments

Comments
 (0)