From 0f50e12de5147fd465f4068bf22beb3a084021b0 Mon Sep 17 00:00:00 2001 From: OlmerOnline Date: Mon, 26 May 2025 14:30:12 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D0=BE?= =?UTF-8?q?=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D1=83=20=D0=BE=D1=88?= =?UTF-8?q?=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/eventsBtnAddComment.js | 102 +++++++++++++++++++++------------ 1 file changed, 64 insertions(+), 38 deletions(-) diff --git a/modules/eventsBtnAddComment.js b/modules/eventsBtnAddComment.js index 38987c3..ac6a27b 100644 --- a/modules/eventsBtnAddComment.js +++ b/modules/eventsBtnAddComment.js @@ -11,53 +11,79 @@ function removeClassError(input) { input.classList.remove('input-error'); } -export function addClickBtnAddComment(API) { - btnWrite.addEventListener('click', () => { - if (inputName.value === '' || inputText.value === '') { - if (inputName.value === '') { - inputName.classList.add('input-error'); - - setTimeout(() => removeClassError(inputName), 2000); - } - if (inputText.value === '') { - inputText.classList.add('input-error'); +function clickAddComment(API) { + if (inputName.value === '' || inputText.value === '') { + if (inputName.value === '') { + inputName.classList.add('input-error'); - setTimeout(() => removeClassError(inputText), 2000); - } - - return; + setTimeout(() => removeClassError(inputName), 2000); } + if (inputText.value === '') { + inputText.classList.add('input-error'); - if (inputName.value.length < 3 || inputText.value.length < 3) { - inputName.value.length < 3 - ? alert('Имя не должно быть менее 3 символов') - : alert('Комментарий не должен быть менее 3 символов'); - return; + setTimeout(() => removeClassError(inputText), 2000); } - const newComment = { - text: replaceSymbol(inputText.value), - name: replaceSymbol(inputName.value), - }; + return; + } - formAddComment.style.display = 'none'; + const newComment = { + text: replaceSymbol(inputText.value), + name: replaceSymbol(inputName.value), + forceError: true, + }; - const loaderText = document.createElement('p'); - loaderText.textContent = 'Комментарий добавляется'; - container.appendChild(loaderText); + formAddComment.style.display = 'none'; - fetch(API, { - method: 'POST', - body: JSON.stringify(newComment), - }) - .then(() => { - return fetchGetComments(API); - }) - .then(() => { + const loaderText = document.createElement('p'); + loaderText.textContent = 'Комментарий добавляется'; + container.appendChild(loaderText); + + let isCrashServer = false; + + fetch(API, { + method: 'POST', + body: JSON.stringify(newComment), + }) + .then((responce) => { + if (responce.status === 201) { inputName.value = ''; inputText.value = ''; + + return fetchGetComments(API); + } else { + if (responce.status === 400) { + throw new Error('400'); + } + + if (responce.status === 500) { + isCrashServer = true; + clickAddComment(API); + } + } + }) + .catch((error) => { + switch (error.message) { + case '400': + alert('Имя и комментарий должны быть не короче 3 символов'); + break; + case '500': + alert('Сервер сломался, попробуй позже'); + break; + default: + alert('Кажется, у вас сломался интернет, попробуйте позже'); + break; + } + }) + .finally(() => { + container.removeChild(loaderText); + + if (!isCrashServer) { formAddComment.style.display = 'flex'; - container.removeChild(loaderText); - }); - }); + } + }); +} + +export function addClickBtnAddComment(API) { + btnWrite.addEventListener('click', () => clickAddComment(API)); }