Skip to content

Commit 4555970

Browse files
authored
Merge pull request #14 from Julyova/module12-task2
2 parents fbac3a9 + 86170bd commit 4555970

1 file changed

Lines changed: 41 additions & 1 deletion

File tree

js/form-upload.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const uploadCancel = document.querySelector('#upload-cancel');
1010
const submitButton = document.querySelector('#upload-submit');
1111
const hashtagsInput = document.querySelector('.text__hashtags');
1212
const descriptionInput = document.querySelector('.text__description');
13+
const imgUploadPreview = document.querySelector('.img-upload__preview img');
14+
15+
const DEFAULT_PREVIEW_SRC = 'img/upload-default-image.jpg';
1316

1417
const MAX_HASHTAGS = 5;
1518
const MAX_HASHTAG_LENGTH = 20;
@@ -50,9 +53,41 @@ const validateHashtags = (value) => {
5053
});
5154
};
5255

56+
const getHashtagErrorMessage = (value) => {
57+
if (!value.trim()) {
58+
return '';
59+
}
60+
61+
const hashtags = value.trim().split(/\s+/);
62+
63+
if (hashtags.length > MAX_HASHTAGS) {
64+
return `Максимум ${MAX_HASHTAGS} хэш-тегов`;
65+
}
66+
67+
const lowerHashtags = hashtags.map((tag) => tag.toLowerCase());
68+
const uniqueHashtags = new Set(lowerHashtags);
69+
70+
if (uniqueHashtags.size !== hashtags.length) {
71+
return 'Хэш-теги не должны повторяться';
72+
}
73+
74+
const invalidTag = hashtags.find((hashtag) => {
75+
if (hashtag.length > MAX_HASHTAG_LENGTH) {
76+
return true;
77+
}
78+
return !VALID_HASHTAG_REGEX.test(hashtag);
79+
});
80+
81+
if (invalidTag) {
82+
return 'Некорректный хэш-тег';
83+
}
84+
85+
return '';
86+
};
87+
5388
const validateDescription = (value) => value.length <= 140;
5489

55-
pristine.addValidator(hashtagsInput, validateHashtags, 'Ошибка в хэш-тегах');
90+
pristine.addValidator(hashtagsInput, validateHashtags, getHashtagErrorMessage);
5691
pristine.addValidator(descriptionInput, validateDescription, 'Комментарий не должен быть длинее 140 символов');
5792

5893
const openForm = () => {
@@ -67,9 +102,14 @@ const closeForm = () => {
67102
pristine.reset();
68103
resetScale();
69104
resetEffect();
105+
imgUploadPreview.src = DEFAULT_PREVIEW_SRC;
70106
};
71107

72108
const onUploadFileInputChange = () => {
109+
const file = uploadFileInput.files[0];
110+
if (file) {
111+
imgUploadPreview.src = URL.createObjectURL(file);
112+
}
73113
openForm();
74114
};
75115

0 commit comments

Comments
 (0)