Skip to content

Commit

Permalink
fix: make changes after auto tests, delete extra spaces when validati…
Browse files Browse the repository at this point in the history
…ng hashtags
  • Loading branch information
KseniaTry committed Aug 13, 2024
1 parent 6691845 commit 85734ea
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 35 deletions.
21 changes: 11 additions & 10 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import './upload-form.js';
import { getData } from './api.js';
import { showGetDataError } from './messages';

try {
const picturesData = await getData();
renderThumbnails(picturesData);
showImagesSortingSection();
showDefaultPhotos(picturesData);
showRandomPhotos(picturesData);
showDiscussedPhotos(picturesData);
} catch {
showGetDataError();
}
getData()
.then((picturesData) => {
renderThumbnails(picturesData),

Check failure on line 9 in js/main.js

View workflow job for this annotation

GitHub Actions / Check

Expected an assignment or function call and instead saw an expression
showImagesSortingSection(),
showDefaultPhotos(picturesData),
showRandomPhotos(picturesData),
showDiscussedPhotos(picturesData);
})
.catch(() => {
showGetDataError();
});
4 changes: 4 additions & 0 deletions js/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const closeUploadSuccessMessageByEsc = (evt) => {
if (isEscapeKey(evt)) {
evt.preventDefault();
successMessage.remove();
document.removeEventListener('click', closeSuccessMessageByClickOnDocument);

Check failure on line 26 in js/messages.js

View workflow job for this annotation

GitHub Actions / Check

'closeSuccessMessageByClickOnDocument' was used before it was defined
}
};

Expand All @@ -43,13 +44,15 @@ const showPostSucsessMessage = () => {
successButton.addEventListener('click', () => {
successMessage.remove();
document.removeEventListener('keydown', closeUploadSuccessMessageByEsc);
document.removeEventListener('click', closeSuccessMessageByClickOnDocument);
});

// показ сообщения об ошибке при отправке формы
const closeUploadErrorMessageByEsc = (evt) => {
if (isEscapeKey(evt)) {
evt.preventDefault();
errorMessage.remove();
document.removeEventListener('click', closeErrorMessageByClickOnDocument);

Check failure on line 55 in js/messages.js

View workflow job for this annotation

GitHub Actions / Check

'closeErrorMessageByClickOnDocument' was used before it was defined
}
};

Expand All @@ -71,6 +74,7 @@ const showPostErrorMessage = () => {
errorButton.addEventListener('click', () => {
errorMessage.remove();
document.removeEventListener('keydown', closeUploadSuccessMessageByEsc);
document.removeEventListener('click', closeErrorMessageByClickOnDocument);
});

export { showGetDataError, showPostSucsessMessage, showPostErrorMessage };
16 changes: 6 additions & 10 deletions js/upload-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { sendData } from './api.js';
import { showPostErrorMessage, showPostSucsessMessage } from './messages.js';

const HASHTAGS_REGEXP = /^#[a-zа-яё0-9]{1,19}$/i;
const SPACES_REGEXP = / +/g;
const MAX_COMMENTS_LENGTH = 140;
const MAX_HASHTAGS_COUNT = 5;
const FILE_TYPES = ['jpg', 'jpeg', 'png'];
Expand Down Expand Up @@ -47,16 +48,13 @@ const clearFormData = () => {
imgPreviewStartSettings();
scale.value = `${100}%`;
photoPreview.style.transform = `scale(${scale.value})`;
pristine.reset();

Check failure on line 51 in js/upload-form.js

View workflow job for this annotation

GitHub Actions / Check

'pristine' was used before it was defined
};

const closeUploadModal = () => {
uploadOverlay.classList.add('hidden');
document.body.classList.remove('modal-open');

clearFormData();

pristine.reset();

document.removeEventListener('keydown', onDocumentEscKeyDown);
};

Expand All @@ -76,9 +74,7 @@ const setLoadedPhotoPreview = () => {

if (matches) {
const filePath = URL.createObjectURL(file);

photoPreviewImg.src = filePath;

effectsPreviewIcons.forEach((icon) => {
icon.style.backgroundImage = `url(${ filePath })`;
});
Expand Down Expand Up @@ -107,17 +103,17 @@ const pristine = new Pristine(uploadForm, {

// проверка количества хэштегов:
const checkHashtagsArrayLength = (value) => {
const hashtagsArray = value.trim().split(' ');
const hashtagsArray = value.replace(SPACES_REGEXP, ' ').trim().split(' ');

return hashtagsArray.length === 0 || hashtagsArray.length <= MAX_HASHTAGS_COUNT;
};

// проверка повторяющихся хэштегов:
const checkHashtagsRepeat = (value) => {
const hashtagsArray = value.trim().split(' ');
const hashtagsArray = value.replace(SPACES_REGEXP, ' ').trim().split(' ');

const modifiedHashtagArray = hashtagsArray.map((hashtag) => {
const modifiedHashtag = hashtag.replaceAll(' ', '').toLowerCase();
const modifiedHashtag = hashtag.trim().toLowerCase();
return modifiedHashtag;
});

Expand All @@ -126,7 +122,7 @@ return modifiedHashtagArray.length === new Set(modifiedHashtagArray).size;

// проверка корректности введения символов хэштега:
const checkHashtagsRegister = (value) => {
const hashtagsArray = value.trim().split(' ');
const hashtagsArray = value.replace(SPACES_REGEXP, ' ').trim().split(' ');

if (value === '') {
return true;
Expand Down
16 changes: 1 addition & 15 deletions js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,6 @@ const getRandomIntegerArray = (min, max) => {
return randomIntegerArray;
};

// функция, которая выбирает случайный элемент из массива
const getRandomArrayElement = (elements) => elements[getRandomInteger(0, elements.length - 1)];

// функция генерирует ID с учетом других ID чтобы они не повторялись (замыкание)
const createGenerator = () => {
let numberId = 1;

return () => {
numberId += 1;
return numberId;
};
};

// функция, которая возвращает true / false в зависимости от наличия или отсутствия нажатия клавиши esc
const isEscapeKey = (evt) => evt.key === 'Escape';

Expand All @@ -49,5 +36,4 @@ const debounce = (callback, timeoutDelay) => {
};
};

export { getRandomInteger, getRandomIntegerArray, getRandomArrayElement, createGenerator, isEscapeKey, debounce };

export { getRandomInteger, getRandomIntegerArray, isEscapeKey, debounce };

0 comments on commit 85734ea

Please sign in to comment.