|
| 1 | +import {renderGallery, setGalleryData} from './gallery.js'; |
| 2 | +import {showErrorMessage} from './utils.js'; |
| 3 | +import { onCloseImageEditor } from './upload-picture.js'; |
| 4 | + |
| 5 | +const hashtagPattern = /^#[a-zа-яё0-9]{1,19}$/i; |
| 6 | +const MAX_HASHTAGS = 5; |
| 7 | +const MAX_DESCRIPTION_LEN = 140; |
| 8 | +const imgUploadForm = document.querySelector('.img-upload__form'); |
| 9 | +const imgUploadSubmit = imgUploadForm.querySelector('.img-upload__submit'); |
| 10 | +const descriptionInput = imgUploadForm.querySelector('.text__description'); |
| 11 | +const hashtagsInput = imgUploadForm.querySelector('.text__hashtags'); |
| 12 | + |
| 13 | +const BASE_URL = 'https://31.javascript.htmlacademy.pro/kekstagram'; |
| 14 | +const Route = { |
| 15 | + GET_DATA: '/data', |
| 16 | + SEND_DATA: '/', |
| 17 | +}; |
| 18 | +const Method = { |
| 19 | + GET: 'GET', |
| 20 | + POST: 'POST', |
| 21 | +}; |
| 22 | +const ErrorText = { |
| 23 | + GET_DATA: 'Не удалось загрузить данные. Попробуйте обновить страницу', |
| 24 | + SEND_DATA: 'Не удалось отправить форму. Попробуйте ещё раз', |
| 25 | +}; |
| 26 | +const SubmitButtonText = { |
| 27 | + IDLE: 'Сохранить', |
| 28 | + SENDING: 'Сохраняю...' |
| 29 | +}; |
| 30 | + |
| 31 | + |
| 32 | +const load = (route, errorText, method = Method.GET, body = null) => |
| 33 | + fetch(`${BASE_URL}${route}`, {method, body}) |
| 34 | + .then((response) => { |
| 35 | + if (!response.ok) { |
| 36 | + throw new Error(); |
| 37 | + } |
| 38 | + return response.json(); |
| 39 | + }) |
| 40 | + .catch(() => { |
| 41 | + throw new Error(errorText); |
| 42 | + }); |
| 43 | + |
| 44 | +const getData = () => load(Route.GET_DATA, ErrorText.GET_DATA); |
| 45 | + |
| 46 | +export const sendData = (body) => load(Route.SEND_DATA, ErrorText.SEND_DATA, Method.POST, body); |
| 47 | + |
| 48 | +getData() |
| 49 | + .then((gallery) => { |
| 50 | + renderGallery(gallery); |
| 51 | + setGalleryData(gallery); |
| 52 | + }) |
| 53 | + .catch( |
| 54 | + (err) => { |
| 55 | + showErrorMessage(err.message); |
| 56 | + } |
| 57 | + ); |
| 58 | + |
| 59 | +const toggleSubmitButton = (disabled) => { |
| 60 | + imgUploadSubmit.disabled = disabled; |
| 61 | + imgUploadSubmit.textContent = disabled |
| 62 | + ? SubmitButtonText.SENDING |
| 63 | + : SubmitButtonText.IDLE; |
| 64 | +}; |
| 65 | + |
| 66 | +const pristine = new Pristine(imgUploadForm, { |
| 67 | + classTo: 'img-upload__field-wrapper', |
| 68 | + errorTextParent: 'img-upload__field-wrapper', |
| 69 | + errorTextClass: 'img-upload__field-wrapper--error' |
| 70 | +}); |
| 71 | + |
| 72 | +const isHashtagsValid = (value) => { |
| 73 | + const trimmedValue = value.trim(); |
| 74 | + |
| 75 | + if (trimmedValue === '') { |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + const hashtagsArray = trimmedValue.split(/\s+/); |
| 80 | + |
| 81 | + if (hashtagsArray.length > MAX_HASHTAGS) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + const uniqueHashtags = new Set(hashtagsArray.map((h) => h.toLowerCase())); |
| 86 | + |
| 87 | + if (uniqueHashtags.size !== hashtagsArray.length) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + return hashtagsArray.every((hashtag) => hashtagPattern.test(hashtag)); |
| 92 | +}; |
| 93 | + |
| 94 | +const isDescriptionValid = (value) => |
| 95 | + value.trim() === '' || value.length <= MAX_DESCRIPTION_LEN; |
| 96 | + |
| 97 | +pristine.addValidator(hashtagsInput, isHashtagsValid, 'введён невалидный хэштег'); |
| 98 | +pristine.addValidator(descriptionInput, isDescriptionValid, 'длина комментария больше 140 символов.'); |
| 99 | + |
| 100 | +imgUploadForm.addEventListener('submit', async (evt) => { |
| 101 | + evt.preventDefault(); |
| 102 | + const isValid = pristine.validate(); |
| 103 | + |
| 104 | + if (isValid) { |
| 105 | + try { |
| 106 | + toggleSubmitButton(true); |
| 107 | + |
| 108 | + await sendData(new FormData(evt.target)); |
| 109 | + onCloseImageEditor(); |
| 110 | + } catch (err) { |
| 111 | + showErrorMessage(err.message); |
| 112 | + } finally { |
| 113 | + toggleSubmitButton(false); |
| 114 | + } |
| 115 | + } else { |
| 116 | + hashtagsInput.style.borderColor = 'red'; |
| 117 | + |
| 118 | + hashtagsInput.focus(); |
| 119 | + } |
| 120 | +}); |
| 121 | + |
0 commit comments