Skip to content

Commit

Permalink
Исправление функции добавления фото
Browse files Browse the repository at this point in the history
  • Loading branch information
shumova committed Apr 11, 2022
1 parent e8fb2db commit f40cd83
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions js/photos.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const FILE_TYPES = ['gif', 'jpg', 'jpeg', 'png'];
const MAX_PHOTO_QUANTITY = 6;
const avatarChooser = document.querySelector('.avatar__input');
const avatarPreview = document.querySelector('.avatar__photo');
const offerPhotosChooser = document.querySelector('.ad-form__input');
const offerPhotosPreview = document.querySelector('.ad-form__photo');
const FILE_TYPES = ['gif', 'jpg', 'jpeg', 'png'];
const MAX_PHOTO_QUANTITY = 6;

let currentQuantity = 0;
const resetPhotos = () => {
Expand All @@ -15,26 +15,33 @@ const resetPhotos = () => {

avatarChooser.addEventListener('change', () => {
const file = avatarChooser.files[0];
const fileName = file.name.toLowerCase();
const matches = FILE_TYPES.some((it) => fileName.endsWith(it));
if (file) {
const fileName = file.name.toLowerCase();
const matches = FILE_TYPES.some((it) => fileName.endsWith(it));

if (matches) {
avatarPreview.src = URL.createObjectURL(file);
if (matches) {
avatarPreview.src = URL.createObjectURL(file);
}
}
});

offerPhotosChooser.addEventListener('change', () => {
const files = Array.from(offerPhotosChooser.files);
files.forEach((file) => {
currentQuantity++;
if (currentQuantity <= MAX_PHOTO_QUANTITY) {
const photoElement = document.createElement('img');
photoElement.src = URL.createObjectURL(file);
photoElement.alt = 'Фотография объекта';
photoElement.width = 70;
photoElement.height = 70;
photoElement.classList.add('ad-form__photo-item');
offerPhotosPreview.insertAdjacentElement('beforeend', photoElement);
const fileName = file.name.toLowerCase();
const matches = FILE_TYPES.some((it) => fileName.endsWith(it));

if(matches) {
if (currentQuantity <= MAX_PHOTO_QUANTITY) {
const photoElement = document.createElement('img');
photoElement.src = URL.createObjectURL(file);
photoElement.alt = 'Фотография объекта';
photoElement.width = 70;
photoElement.height = 70;
photoElement.classList.add('ad-form__photo-item');
offerPhotosPreview.insertAdjacentElement('beforeend', photoElement);
}
}

if (currentQuantity === MAX_PHOTO_QUANTITY) {
Expand All @@ -48,6 +55,7 @@ offerPhotosChooser.addEventListener('change', () => {
}
}
});
offerPhotosChooser.value = '';
});

export {resetPhotos};

0 comments on commit f40cd83

Please sign in to comment.