Skip to content

Commit

Permalink
Исправляет ошибки
Browse files Browse the repository at this point in the history
  • Loading branch information
elviraSolov committed Feb 13, 2023
1 parent 894aa29 commit 3582a68
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 116 deletions.
30 changes: 15 additions & 15 deletions js/api.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const getData = (onSuccess) => {
fetch('https://25.javascript.pages.academy/kekstagram/data')
.then((response) => response.json())
.then((posts) => {
onSuccess(posts);
});
}
fetch('https://25.javascript.pages.academy/kekstagram/data')
.then((response) => response.json())
.then((posts) => {
onSuccess(posts);
});
};

const sendData = (onSuccess, onFail, body) => {
fetch('https://25.javascript.pages.academy/kekstagram',
Expand All @@ -13,15 +13,15 @@ const sendData = (onSuccess, onFail, body) => {
body,
},
)
.then((response) => {
if(!response.ok) {
throw new Error('Не удалось отправить фото. Попробуйте еще раз');
}
onSuccess();
})
.catch((err) => {
onFail(err.message);
.then((response) => {
if(!response.ok) {
throw new Error('Не удалось отправить фото. Попробуйте еще раз');
}
onSuccess();
})
.catch((err) => {
onFail(err.message);
});
};

export { getData, sendData }
export { getData, sendData };
2 changes: 1 addition & 1 deletion js/big-picture.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const renderPictureDetails = ({ url, likes, description }) => {
const createComment = ({ avatar, message, name }) => {
const comment = document.createElement('li');
comment.innerHTML =
'<img class="social__picture" src="" alt="" width="35" height="35"><p class="social__text"></p>'
'<img class="social__picture" src="" alt="" width="35" height="35"><p class="social__text"></p>';
comment.classList.add('social__comment');

comment.querySelector('.social__picture').src = avatar;
Expand Down
62 changes: 0 additions & 62 deletions js/data.js

This file was deleted.

19 changes: 9 additions & 10 deletions js/form.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { isEscapeKey } from './util.js';
import { sendData } from './api.js';
import { showAlert, successMessage } from './util.js';
//import { renderPictures } from './pictures.js';

const form = document.querySelector('.img-upload__form');
const uploadFile = form.querySelector('#upload-file');
Expand All @@ -17,7 +16,14 @@ const MIN_HASHTAG_LENGTH = 2;
const MAX_HASHTAG_LENGTH = 20;
const UNVALID_SYMBOLS = /[^a-zA-Z0-9а-яА-ЯёЁ]/g;

var pristine = new Pristine(form, {
const onEscKeydown = (evt) => {
if (isEscapeKey(evt) && !isTextFieldFocused()) {
evt.preventDefault();
closeUploadOverlay();
}
};

let pristine = new Pristine(form, {
classTo: 'img-upload__element',
errorTextParent: 'img-upload__element',
errorTextClass: 'img-upload__error'
Expand All @@ -41,13 +47,6 @@ const isTextFieldFocused = () =>
document.activeElement === hashtags ||
document.activeElement === comment;

const onEscKeydown = (evt) => {
if (isEscapeKey(evt) && !isTextFieldFocused()) {
evt.preventDefault();
closeUploadOverlay();
}
};

const startsWithHash = (string) => string[0] === '#';

const hasValidLength = (string) =>
Expand Down Expand Up @@ -91,7 +90,7 @@ const blockSubmitButton = () => {
const unblockSubmitButton = () => {
submitButton.disabled = false;
submitButton.textContent = 'Опубликовать';
}
};

const onSendDataSuccess = () => {
closeUploadOverlay();
Expand Down
24 changes: 12 additions & 12 deletions js/picture-editing.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ noUiSlider.create(slider, {
connect: 'lower',
});

const onFormChange = (evt) => {
if (!evt.target.classList.contains('effects__radio')) {
return;
}
chosenEffect = EFFECTS.find((effect) => effect.name === evt.target.value);
updateSlider(chosenEffect);
};

const isDefault = () => chosenEffect === DEFAULT_EFFECT;

const updateSlider = (chosenEffect) => {
slider.classList.remove('hidden');
slider.noUiSlider.updateOptions({
Expand All @@ -100,13 +90,23 @@ const updateSlider = (chosenEffect) => {
}
};

const onFormChange = (evt) => {
if (!evt.target.classList.contains('effects__radio')) {
return;
}
chosenEffect = EFFECTS.find((effect) => effect.name === evt.target.value);
updateSlider(chosenEffect);
};

const isDefault = () => chosenEffect === DEFAULT_EFFECT;

const onSliderUpdate = () => {
image.style.filter = 'none';
image.className = '';
let effectValue = slider.noUiSlider.get();
const effectValue = slider.noUiSlider.get();
image.classList.add(`effects__preview--${chosenEffect.name}`);
image.style.filter = `${chosenEffect.style}(${effectValue}${chosenEffect.unit})`;
}
};

slider.noUiSlider.on('update', onSliderUpdate);
form.addEventListener('change', onFormChange);
Expand Down
2 changes: 1 addition & 1 deletion js/pictures.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ const renderPictures = (pictures) => {

// добавляет фрагмент на страницу
picturesContainer.append(fragment);
}
};

export { renderPictures };
10 changes: 5 additions & 5 deletions js/sorting.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ const turnFilterOn = (loadedPictures) => {
filtersElement.classList.remove('img-filters--inactive');
pictures = [...loadedPictures];
currentFilter = Filter.DEFAULT;
}
};

const randomSort = () => Math.random() - 0.5;

const discussedSort = (pictureA, pictureB) => {
return pictureB.comments.length - pictureA.comments.length;
}
};

const filterPictures = () => {
switch (currentFilter) {
Expand All @@ -35,7 +35,7 @@ const filterPictures = () => {
default:
return [...pictures];
}
}
};

const debouncedRenderPictures = debounce(renderPictures);

Expand All @@ -56,6 +56,6 @@ filtersElement.addEventListener('click', (evt) => {
clickedButton.classList.add('img-filters__button--active');
currentFilter = clickedButton.id;
debouncedRenderPictures(filterPictures());
})
});

export { turnFilterOn, filterPictures }
export { turnFilterOn, filterPictures };
11 changes: 1 addition & 10 deletions js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ function getRandomPositiveInteger (a, b = 0) {
const getRandomArrayElement = (elements) =>
elements[getRandomPositiveInteger(0, elements.length - 1)];

//const checkStringLength = (string, length) => string.length <= length;

function createIdGenerator() {
let lastGeneratedId = 0;
return function() {
Expand Down Expand Up @@ -56,23 +54,16 @@ const successMessage = () => {

const onSuccessButtonCLick = () => {
document.querySelector('.success').remove();
}
};

successButton.addEventListener('click', onSuccessButtonCLick);

function debounce (callback, timeoutDelay = 500) {
let timeoutId;

return (...rest) => {
// Перед каждым новым вызовом удаляем предыдущий таймаут,
// чтобы они не накапливались
clearTimeout(timeoutId);

// Затем устанавливаем новый таймаут с вызовом колбэка на ту же задержку
timeoutId = setTimeout(() => callback.apply(this, rest), timeoutDelay);

// Таким образом цикл «поставить таймаут - удалить таймаут» будет выполняться,
// пока действие совершается чаще, чем переданная задержка timeoutDelay
};
}

Expand Down

0 comments on commit 3582a68

Please sign in to comment.