Skip to content

Commit

Permalink
Надо подкачаться
Browse files Browse the repository at this point in the history
  • Loading branch information
MaksimRozov committed Mar 25, 2024
1 parent 5a19985 commit 5e58131
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 22 deletions.
File renamed without changes.
14 changes: 10 additions & 4 deletions js/create-thumbnails/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import {photoItems} from '../generate-data';
// import {dataUserPhoto} from '../generate-data';
import {onOpenBigPicture } from '../show-large-picture';

const pictureTemplate = document.querySelector('#picture').content.querySelector('.picture');
const pictureList = document.querySelector('.pictures');
const createPhotoUsers = photoItems();
// const createPhotoUsers = dataUserPhoto;

const addPhotoThumbnailsUsers = () => {
// const getPhoto = (photoUsers) => photoUsers;

// console.log(getPhoto(photoUsers));
let createPhotoUsers = '';
const addPhotoThumbnailsUsers = (photoUsers) => {
const listFragmentPhoto = document.createDocumentFragment();
createPhotoUsers.forEach((element) => {
createPhotoUsers = photoUsers;
photoUsers.forEach((element) => {
const pictureItem = pictureTemplate.cloneNode(true);
const pictureImage = pictureItem.querySelector('.picture__img');
pictureImage.dataset.id = element.id;
Expand All @@ -17,6 +22,7 @@ const addPhotoThumbnailsUsers = () => {
pictureItem.querySelector('.picture__comments').textContent = element.comments.length;
listFragmentPhoto.appendChild(pictureItem);
});

return pictureList.appendChild(listFragmentPhoto);
};

Expand Down
105 changes: 105 additions & 0 deletions js/get-data/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {onCloseChangePhotoEsc} from '../upload-photo';

const BASE_URL = 'https://31.javascript.htmlacademy.pro/kekstagram';
const Route = {
GET_DATA: '/data',
SEND_DATA: '/',
};

const dataErrorTemplate = document.querySelector('#data-error').content.querySelector('.data-error');
const sendErrorTemplate = document.querySelector('#error').content.querySelector('.error');
const sendFormErrorTemplate = document.querySelector('#success').content.querySelector('.success');
const SHOW_ERROR_TIME = 5000;

const sendSuccessClose = () => {
const successMessage = document.querySelector('.success');
successMessage.remove();
document.removeEventListener('click', clickOutModalSuccess);
};

const sendErrorMessageClose = () => {
const errorMessage = document.querySelector('.error');
errorMessage.remove();
document.removeEventListener('click', clickOutModalError);
document.addEventListener('keydown', onCloseChangePhotoEsc);
document.removeEventListener('keydown', sendErrorMessageCloseEsc);
};

function clickOutModalSuccess (evt) {
if(evt.target.closest('.success__inner')){
return;
}
sendSuccessClose();
}

function clickOutModalError (evt) {
if(evt.target.closest('.error__inner')){
return;
}
sendErrorMessageClose();
}

function sendErrorMessageCloseEsc (evt) {
if (evt.key === 'Escape') {
sendErrorMessageClose();
}
}

function sendSuccessMessageCloseEsc (evt) {
if (evt.key === 'Escape') {
sendSuccessClose();
}
}

const sendErrorMessage = () => {
const errorFragmentSend = document.createDocumentFragment();
const errorItemSend = sendErrorTemplate.cloneNode(true);
const sendButttonClose = errorItemSend.querySelector('.error__button');
sendButttonClose.addEventListener('click', sendErrorMessageClose);
errorFragmentSend.append(errorItemSend);
document.body.append(errorFragmentSend);
document.addEventListener('keydown', sendErrorMessageCloseEsc);
document.addEventListener('click', clickOutModalError);
document.removeEventListener('keydown', onCloseChangePhotoEsc);

};

const showErrorGetData = () => {
const errorFragment = document.createDocumentFragment();
const errorItem = dataErrorTemplate.cloneNode(true);
errorFragment.append(errorItem);
document.body.append(errorFragment);
const dataError = document.querySelector('.data-error');

setTimeout(() => {
dataError.remove();
}, SHOW_ERROR_TIME);
};

const sendSuccessMessage = () => {
const errorFragmentSend = document.createDocumentFragment();
const errorItemSend = sendFormErrorTemplate.cloneNode(true);
const closeButton = errorItemSend.querySelector('.success__button');
errorFragmentSend.append(errorItemSend);
document.body.append(errorFragmentSend);
closeButton.addEventListener('click', sendSuccessClose);
document.addEventListener('click', clickOutModalSuccess);
document.addEventListener('keydown', sendSuccessMessageCloseEsc);
};

const getData = () => fetch(
`${BASE_URL}${Route.GET_DATA}`)
.then((response) => {
if (!response.ok) {
throw new Error();
}
return response.json();
})
.catch(() => {
throw new Error(
showErrorGetData()
);
});


export {getData, sendSuccessMessage, sendErrorMessage};
12 changes: 9 additions & 3 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import {addPhotoThumbnailsUsers} from './create-thumbnails';
import {onOpenChangePhotoListener} from './upload-photo';
import {validateListener} from './validation-form';
import {scaleListener} from './add-effects';
import {scaleListener} from './add-effects-scale';
import {effectCheckedListener} from './slider-effects';
import {getData} from './get-data';
import {addPhotoThumbnailsUsers} from './create-thumbnails';

effectCheckedListener();
scaleListener();
validateListener();
onOpenChangePhotoListener();
addPhotoThumbnailsUsers();


getData()
.then((photos) => {
addPhotoThumbnailsUsers(photos);
});
12 changes: 6 additions & 6 deletions js/slider-effects/effect-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const EFFECTS = [
max: 1,
step: 0.1,
start: 1,
unit: '',
unit: ''
},
{
name: 'sepia',
Expand All @@ -19,7 +19,7 @@ export const EFFECTS = [
max: 1,
step: 0.1,
start: 1,
unit: '',
unit: ''
},
{
name: 'marvin',
Expand All @@ -28,7 +28,7 @@ export const EFFECTS = [
max: 100,
step: 1,
start: 100,
unit: '%',
unit: '%'
},
{
name: 'phobos',
Expand All @@ -37,7 +37,7 @@ export const EFFECTS = [
max: 3,
step: 0.1,
start: 3,
unit: 'px',
unit: 'px'
},
{
name: 'heat',
Expand All @@ -46,7 +46,7 @@ export const EFFECTS = [
max: 3,
step: 0.1,
start: 3,
unit: '',
},
unit: ''
}
];

10 changes: 4 additions & 6 deletions js/upload-photo/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import {toggleClass} from '../util';
import {imgUploadInput, imgUpoadOverlay, imgUploadancel} from './uploadPhotoVariables';
import {inputTextHashtag, commentForm} from '../validation-form';
const effectsPreview = document.querySelectorAll('.effects__preview');
import {inputTextHashtag, commentForm, imgUploadForm} from '../validation-form';

const effectsPreview = document.querySelectorAll('.effects__preview');
const effectLevelSliderParrent = document.querySelector('.img-upload__effect-level');
const uploaPreviewImage = document.querySelector('.img-upload__preview img');

const onCloseChangePhoto = () => {
toggleClass(imgUpoadOverlay, false);
imgUploadInput.value = '';
imgUploadForm.reset();
uploaPreviewImage.style.removeProperty('filter');
uploaPreviewImage.style.removeProperty('transform');
document.removeEventListener('keydown', onCloseChangePhotoEsc);
imgUploadancel.removeEventListener('click', onCloseChangePhoto);

};

function onCloseChangePhotoEsc(evt){
Expand Down Expand Up @@ -54,11 +53,10 @@ const onOpenChangePhoto = (evt) => {
effectLevelSliderParrent.classList.add('hidden');
document.addEventListener('keydown', onCloseChangePhotoEsc);
imgUploadancel.addEventListener('click', onCloseChangePhoto);

};

const onOpenChangePhotoListener = () => {
imgUploadInput.addEventListener('change', onOpenChangePhoto);
};

export {onOpenChangePhotoListener};
export {onOpenChangePhotoListener, onCloseChangePhoto, onCloseChangePhotoEsc};
31 changes: 28 additions & 3 deletions js/validation-form/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const imgUploadForm = document.querySelector('.img-upload__form');
import {sendSuccessMessage, sendErrorMessage} from '../get-data';
import {onCloseChangePhoto} from '../upload-photo';

export const imgUploadForm = document.querySelector('.img-upload__form');
export const inputTextHashtag = imgUploadForm.querySelector('.text__hashtags');
export const commentForm = imgUploadForm.querySelector('.text__description');

Expand Down Expand Up @@ -40,12 +43,34 @@ pristine.addValidator(inputTextHashtag, onHashtagLimitLength, 'Превышен
pristine.addValidator(commentForm, onValidateCommentForm, 'Длина комментария больше 140 символов');

const validateListener = () => {

imgUploadForm.addEventListener('submit', (evt) => {
evt.preventDefault();
if(pristine.validate()) {
imgUploadForm.submit();
fetch(
'https://31.javascript.htmlacademy.pro/kekstagram',
{
method: 'POST',
body: new FormData(evt.target),
})
.then((response) => {
if (response.ok) {
sendSuccessMessage();
onCloseChangePhoto();
}else{
throw new Error ();
}
})
.catch(() => {
throw new Error(
sendErrorMessage()
);
}
);
}
evt.preventDefault();
});
};

export {validateListener};


0 comments on commit 5e58131

Please sign in to comment.