Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 62 additions & 10 deletions modules/newComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { renderComments } from './renderComments.js';
import { updateComments } from './commentsData.js';
import { inputName, commentText } from './index.js';

let retryCount = 0;

export const newComment = () => {
const name = inputName.value.trim().replaceAll('<', '').replaceAll('>', '');
const text = commentText.value
Expand All @@ -17,9 +19,6 @@ export const newComment = () => {
const newCommentObj = {
name: name,
text: text,
// isLiked: false,
// likes: 0,
// date: newTime(),
};

document.querySelector('.form-loading').style.display = 'block';
Expand All @@ -29,13 +28,27 @@ export const newComment = () => {
method: 'POST',
body: JSON.stringify(newCommentObj),
})
.then((response) => response.json())
.then((response) => {
if (response.status === 500) {
throw new Error('Ошибка сервера');
}

if (response.status === 400) {
throw new Error('Неверный запрос');
}

if (response.status === 201) {
return response.json();
}
})
.then((data) => {
if (data.result !== 'ok') {
console.error('Ошибка при добавлении комментария:', data);
return;
}

retryCount = 0;

return fetch(
'https://wedev-api.sky.pro/api/v1/mamay-vitaliy/comments',
);
Expand All @@ -46,17 +59,56 @@ export const newComment = () => {
console.error('Нет поля comments в ответе:', data);
return;
}
document.querySelector('.form-loading').style.display = 'none';
document.querySelector('.add-form').style.display = 'flex';

inputName.value = '';
commentText.value = '';
updateComments(data.comments);
renderComments();

document.querySelector('.form-loading').style.display = 'none';
document.querySelector('.add-form').style.display = 'flex';
})
.catch((error) => {
console.error('Ошибка запроса:', error);
});

inputName.value = '';
commentText.value = '';
if (error.message === 'Ошибка сервера' && retryCount < 2) {
retryCount++;
console.log(`Повторная попытка ${retryCount}...`);
setTimeout(newComment, 1000);
return;
}

retryCount = 0;

if (error.message === 'Неверный запрос') {
alert('Имя и комментарий не могут быть короче 3-х символов');
inputName.classList.add(
'add-form--error',
'add-form--error-shake',
);
commentText.classList.add(
'add-form--error',
'add-form--error-shake',
);
setTimeout(() => {
inputName.classList.remove(
'add-form--error',
'add-form--error-shake',
);
commentText.classList.remove(
'add-form--error',
'add-form--error-shake',
);
}, 2000);
} else if (error.message === 'Failed to fetch') {
alert(
'Что-то пошло не так. Проверьте подключение к интернету и попробуйте снова.',
);
} else {
alert('Не удалось отправить комментарий. Попробуйте позже.');
}

renderComments();
document.querySelector('.form-loading').style.display = 'none';
document.querySelector('.add-form').style.display = 'flex';
});
};
25 changes: 25 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,31 @@ body {
resize: none;
}

.add-form-name,
.add-form-text:focus {
border-bottom-color: #4a90e2;
box-shadow: 0 1px 0 0 #4a90e2;
}


.add-form--error {
border-bottom-color: #e74c3c !important;
box-shadow: 0 1px 0 0 #e74c3c !important;
}

.add-form--error-shake {
animation: shake 0.5s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
transform: translateX(0);
}

@keyframes shake {
0%, 100% { transform: translateX(0); }
20% { transform: translateX(-4px); }
40% { transform: translateX(4px); }
60% { transform: translateX(-2px); }
80% { transform: translateX(2px); }
}

.add-form-row {
display: flex;
justify-content: flex-end;
Expand Down