Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New branch #921

Closed
wants to merge 5 commits into from
Closed
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
87 changes: 22 additions & 65 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,71 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<title>Проект "Комменты"</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="styles.css" />
</head>

<body>
<div class="container">
<ul class="comments">
<li class="comment">
<div class="comment-header">
<div>Глеб Фокин</div>
<div>12.02.22 12:18</div>
</div>
<div class="comment-body">
<div class="comment-text">
Это будет первый комментарий на этой странице
</div>
</div>
<div class="comment-footer">
<div class="likes">
<span class="likes-counter">3</span>
<button class="like-button"></button>
</div>
</div>
</li>
<li class="comment">
<div class="comment-header">
<div>Варвара Н.</div>
<div>13.02.22 19:22</div>
</div>
<div class="comment-body">
<div class="comment-text">
Мне нравится как оформлена эта страница! ❤
</div>
</div>
<div class="comment-footer">
<div class="likes">
<span class="likes-counter">75</span>
<button class="like-button -active-like"></button>
</div>
</div>
</li>
</ul>
<div class="add-form">
<input
type="text"
class="add-form-name"
placeholder="Введите ваше имя"
/>
<textarea
type="textarea"
class="add-form-text"
placeholder="Введите ваш коментарий"
rows="4"
></textarea>
<div class="add-form-row">
<button class="add-form-button">Написать</button>
</div>
<head>
<title>Проект "Комменты"</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="styles.css" />
</head>

<body>
<div class="container">
<ul id="user-comments" class="comments">
<!-- Список рендерится из JS -->
</ul>
<div id="form" class="add-form">
<input id="user-name" type="text" name="name" class="add-form-name" placeholder="Введите ваше имя" />
<textarea id="user-comment" type="textarea" class="add-form-text" placeholder="Введите ваш коментарий"
rows="4"></textarea>
<div class="add-form-row">
<button id="send" class="add-form-button" disabled>Написать</button>
</div>
</div>
</body>
</div>
</body>

<script type="text/javascript" src="scripts.js"></script>

<script>
"use strict";
// Код писать здесь
console.log("It works!");
</script>
</html>
</html>
144 changes: 144 additions & 0 deletions scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
"use strict";
const userName = document.getElementById('user-name');
const userCommentt = document.getElementById('user-comment');
const send = document.getElementById('send');
const userCommentsList = document.getElementById('user-comments');
const userForm = document.getElementById('form');
const commentsElements = document.querySelectorAll('.comment');
const countUsersLikes = 0;
const userNameComment = [userName, userCommentt];

const userComments = [
{
name: 'Глеб Фокин',
date: getUserCommentDate(),
comment: 'Это будет первый комментарий на этой странице',
countLikes: 3,
like: false,
},
{
name: 'Варвара Н.',
date: getUserCommentDate(),
comment: 'Мне нравится как оформлена эта страница! ❤',
countLikes: 75,
like: true,
},
]

const initButtonsLikes = () => {
const buttonLikesElements = document.querySelectorAll('.like-button');
buttonLikesElements.forEach((buttonElement, index) => {
buttonElement.addEventListener('click', () => {
if (userComments[index].like) {
userComments[index].countLikes = userComments[index].countLikes - 1;
userComments[index].like = false;
}
else {
userComments[index].countLikes = userComments[index].countLikes + 1;
userComments[index].like = true;
}
renderUserComments();
initButtonsLikes();
});
});
};

const renderUserComments = () => {
const userCommentsHtml = userComments.map((userComment) => {
return `<li class="comment">
<div class="comment-header">
<div>${userComment.name}</div>
<div>${userComment.date}</div>
</div>
<div class="comment-body">
<div class="comment-text">
${userComment.comment}
</div>
</div>
<div class="comment-footer">
<div class="likes">
<span class="likes-counter">${userComment.countLikes}</span>
<button class="like-button ${userComment.like ? '-active-like' : ''}"></button>
</div>
</div>
</li>`;
}).join('');

userCommentsList.innerHTML = userCommentsHtml;

};
renderUserComments();
initButtonsLikes();


let check;

for (let i = 0; i < userNameComment.length; i++) {
userNameComment[i].addEventListener('input', () => {
if (userNameComment.every((el) => el.value !== '')) {
send.disabled = false;
}
else {
send.disabled = true;
}
});
}

userForm.addEventListener('keyup', (event) => {
if (event.keyCode === 13) {
if (userName.value.trim() == '' || userCommentt.value.trim() == '') {
alert('Вы не ввели имя и/или комментарий');
userName.value = '';
userName.blur();
userCommentt.value = '';
userCommentt.blur();
}

else {
sendComment();
userName.blur();
userName.value = '';
userCommentt.blur();
userCommentt.value = '';
send.disabled = true;
}
}
});

function getUserCommentDate() {
const date = new Date();
const userDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
const userMonth = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
const userYear = date.getFullYear().toString().substr(-2);
const userHours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
const userMinutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
return `${userDate}.${userMonth}.${userYear} ${userHours}:${userMinutes}`;
}

function sendComment() {
const oldUserCommentsList = userCommentsList.innerHTML;
getUserCommentDate();
userComments.push({
name: userName.value,
date: getUserCommentDate(),
comment: userCommentt.value,
countLikes: countUsersLikes,
like: initButtonsLikes(),
});

renderUserComments();
initButtonsLikes();
}

send.addEventListener('click', () => {
sendComment();
send.disabled = true;
userName.value = '';
userName.blur();
userCommentt.value = '';
userCommentt.blur();
});

//-------------------------LIKES----------------------------

console.log("It works!");
Loading