Skip to content

Commit

Permalink
completed-hw1
Browse files Browse the repository at this point in the history
  • Loading branch information
golubevNikita committed Dec 12, 2024
1 parent 77e9b73 commit ff1b2e9
Show file tree
Hide file tree
Showing 5 changed files with 515 additions and 60 deletions.
273 changes: 213 additions & 60 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,71 +1,224 @@
<!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>
<head>
<title>Проект "Комменты"</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="styles.css" />
</head>

<body>
<div class="container">
<ul data-js-comment-container class="comments">
<!-- <li class="comment">
<div class="comment-header">
<div>Глеб Фокин</div>
<div>12.02.22 12:18</div>
</div>
<div data-js-text-area class="comment-body">
<div class="comment-text">
Это будет первый комментарий на этой странице
</div>
</li>
<li class="comment">
<div class="comment-header">
<div>Варвара Н.</div>
<div>13.02.22 19:22</div>
</div>
<div class="comment-footer">
<div class="likes">
<span data-js-like-counter class="likes-counter">3</span>
<button data-js-like-button data-js-button-index="0" class="like-button"></button>
</div>
<div class="comment-body">
<div class="comment-text">
Мне нравится как оформлена эта страница! ❤
</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 data-js-text-area class="comment-text">
Мне нравится как оформлена эта страница! ❤
</div>
<div class="comment-footer">
<div class="likes">
<span class="likes-counter">75</span>
<button class="like-button -active-like"></button>
</div>
</div>
<div class="comment-footer">
<div class="likes">
<span data-js-like-counter class="likes-counter">75</span>
<button data-js-like-button data-js-button-index="0" class="like-button -active-like"></button>
</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>
</li> -->
</ul>

<div class="add-form">
<input
data-js-name-input
type="text"
class="add-form-name"
placeholder="Введите ваше имя"
/>

<textarea
data-js-comment-input
type="textarea"
class="add-form-text"
placeholder="Введите ваш коментарий"
rows="4"
></textarea>

<div class="add-form-row">
<button data-js-add-button class="add-form-button">Написать</button>
</div>
</div>
</body>
</div>
</body>

<script>

const commentInput = document.querySelector('[data-js-comment-input]');
const nameInput = document.querySelector('[data-js-name-input]');
const addCommentButton = document.querySelector('[data-js-add-button]');

const commentContainer = document.querySelector('[data-js-comment-container]');
const commentText = document.querySelector('[data-js-text-area]');
const commentLikeCounter = document.querySelector('[data-js-like-counter]');

const commentsArray = [
{
commentHeader: {
himself: 'Варвара Н.',
commentDateAndTime: '13.02.22 19:22',
},

commentBody: {
commentText: 'Мне нравится как оформлена эта страница! ❤',
},

commentFooter: {
likes: {
likesCounter: 75,
likeButton: true,
},
},
},

{
commentHeader: {
himself: 'Глеб Фокин',
commentDateAndTime: '12.02.22 12:18',
},

commentBody: {
commentText: 'Это будет первый комментарий на этой странице',
},

commentFooter: {
likes: {
likesCounter: 8,
likeButton: false,
},
},
},
];

function renderCommentEl(itemEl, indexEL) {
return `<li class="comment" data-js-comment-el-index="${indexEL}">
<div class="comment-header">
<div>${itemEl.commentHeader.himself}</div>
<div>${itemEl.commentHeader.commentDateAndTime}</div>
</div>
<div class="comment-body">
<div data-js-text-area class="comment-text">${itemEl.commentBody.commentText}</div>
</div>
<div class="comment-footer">
<div class="likes">
<span data-js-like-counter
class="likes-counter">${itemEl.commentFooter.likes.likesCounter}</span>
<button data-js-like-button data-js-button-index="${indexEL}"
class="like-button${itemEl.commentFooter.likes.likeButton
? " -active-like"
: ""}"></button>
</div>
</div>
</li>`;
};

function canILike() {
const commentLikeButtons = document.querySelectorAll('[data-js-like-button]');
for (const commentLikeButtonEL of commentLikeButtons) {
commentLikeButtonEL.addEventListener("click", () => {
const likeIndex = commentLikeButtonEL.dataset.jsButtonIndex;

if (commentsArray[likeIndex].commentFooter.likes.likeButton) {
commentsArray[likeIndex].commentFooter.likes.likeButton = false;
commentsArray[likeIndex].commentFooter.likes.likesCounter -= 1;
} else {
commentsArray[likeIndex].commentFooter.likes.likeButton = true;
commentsArray[likeIndex].commentFooter.likes.likesCounter += 1;
};

commentRender();
});
};
};

function commentRender() {
const commentsHtml = commentsArray.map((item, index) => renderCommentEl(item, index)).join("");

commentContainer.innerHTML = commentsHtml;

canILike();
}

commentRender();

addCommentButton.addEventListener("click", (event) => {
commentInput.classList.remove("main__input_empty");
nameInput.classList.remove("main__input_empty");

let newDateObject = new Date();
correctedYear = String(newDateObject.getFullYear()).split("").slice(-2).join("");

const commentDate = `${newDateObject.getDate()}.${newDateObject.getMonth()}.${correctedYear}
${newDateObject.getHours()}:${newDateObject.getMinutes()}`

if (commentInput.value === "" && nameInput.value === "") {
commentInput.classList.add("main__input_empty");
nameInput.classList.add("main__input_empty");
return;
};

if (commentInput.value === "") {
commentInput.classList.add("main__input_empty");
return;
};

if (nameInput.value === "") {
nameInput.classList.add("main__input_empty");
return;
};

const newCommentsArray = {
commentHeader: {
himself: nameInput.value,
commentDateAndTime: commentDate,
},

commentBody: {
commentText: commentInput.value,
},

commentFooter: {
likes: {
likesCounter: 0,
likeButton: false,
},
},
};

commentsArray.push(newCommentsArray);
commentInput.value = "";
nameInput.value = "";

commentRender();
});

<script>
"use strict";
// Код писать здесь
console.log("It works!");
</script>
</script>
</html>
4 changes: 4 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,8 @@ body {

.add-form-button:hover {
opacity: 0.9;
}

.main__input_empty {
background-color: rgb(255,164,148);
}
101 changes: 101 additions & 0 deletions this.beforeGitHubPuplication/JS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@

const commentInput = document.querySelector('[data-js-comment-input]');
const addCommentButton = document.querySelector('[data-js-add-button]');

const commentContainer = document.querySelector('[data-js-comment-container]');
const commentText = document.querySelector('[data-js-text-area]');
const commentLikeCounter = document.querySelector('[data-js-like-counter]');

const commentsArray = [
{text: 'Lorem ipsum dolor sit amet consectetur adipisicing elit',
likeState: {
isLike: false,
likeCount: 10,
}
},

{text: 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Magni, praesentium?',
likeState: {
isLike: true,
likeCount: 3,
}
},

{text: 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Aspernatur quas earum obcaecati molestiae?',
likeState: {
isLike: false,
likeCount: 8,
}
},
];

function renderCommentEl(itemEl, indexEL) {
return `<div class="main__comment" data-js-comment-el-index="${indexEL}">
<p class="main__text-comment" data-js-text-area>${itemEl.text}</p>
<div class="main__like-container">
<input type="checkbox" ${itemEl.likeState.isLike
? "checked='true'"
: ""}
class="main__like-button${itemEl.likeState.isLike === true
? " main__like-button_color-on"
: " main__like-button_color-off"}" data-js-like-button data-js-button-index="${indexEL}">
</input>
<span class="main__like-counter" data-js-like-counter>${itemEl.likeState.likeCount}</span>
</div>
</div>`;
};

function canILike() {
const commentLikeButtons = document.querySelectorAll('[data-js-like-button]');
for (const commentLikeButtonEL of commentLikeButtons) {
commentLikeButtonEL.addEventListener("click", () => {

const likeIndex = commentLikeButtonEL.dataset.jsButtonIndex;
const htmlLikeEL = document.querySelector(`[data-js-button-index="${likeIndex}"]`);

if (htmlLikeEL.getAttribute('checked')) {
htmlLikeEL.removeAttribute('checked');
commentsArray[likeIndex].likeState.isLike = false;
commentsArray[likeIndex].likeState.likeCount -= 1;
} else {
htmlLikeEL.setAttribute('checked', 'true');
commentsArray[likeIndex].likeState.isLike = true;
commentsArray[likeIndex].likeState.likeCount += 1;
};

commentRender();
});
};
};

function commentRender() {
const commentsHtml = commentsArray.map((item, index) => renderCommentEl(item, index)).join("");

commentContainer.innerHTML = commentsHtml;

canILike();
}

commentRender();

addCommentButton.addEventListener("click", () => {
commentInput.classList.remove("main__input_empty");

if (commentInput.value === "") {
commentInput.classList.add("main__input_empty");
return;
};

const newCommentsArray = {
text: commentInput.value,
likeState: {
isLike: false,
likeCount: 0,
},
};

commentsArray.push(newCommentsArray);
commentInput.value = "";

commentRender();
});
Loading

0 comments on commit ff1b2e9

Please sign in to comment.