Skip to content
Closed

Task4 #1341

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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
3 changes: 3 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tabWidth: 4
semi: false
singleQuote: true
12 changes: 12 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import config from "eslint-config-prettier";
import plugin from "eslint-plugin-prettier/recommended";

/** @type {import('eslint').Linter.Config[]} */
export default [
{ languageOptions: { globals: globals.browser } },
pluginJs.configs.recommended,
config,
plugin,
];
45 changes: 3 additions & 42 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html>
<head>
<title>Проект "Комменты"</title>
Expand All @@ -8,42 +8,7 @@

<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>
<ul class="comments"></ul>
<div class="add-form">
<input
type="text"
Expand All @@ -63,9 +28,5 @@
</div>
</body>

<script>
"use strict";
// Код писать здесь
console.log("It works!");
</script>
<script type="module" src="index.js"></script>
</html>
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { initAddCommentListener } from "./modules/initListeners.js";
import { renderComments } from "./modules/renderComments.js";

("use strict");

renderComments();
initAddCommentListener();
16 changes: 16 additions & 0 deletions modules/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export let comments = [
{
name: "Глеб Фокин",
text: "Это будет первый комментарий на этой странице",
time: "12.02.22 12:18",
isLiked: false,
likes: 3,
},
{
name: "Варвара Н.",
text: "Мне нравится как оформлена эта страница! ❤",
time: "13.02.22 19:22",
isLiked: true,
likes: 75,
},
];
62 changes: 62 additions & 0 deletions modules/initListeners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { renderComments } from "./renderComments.js";
import { sanitizeHTML } from "./sanitizeHTML.js";
import { comments } from "./comments.js";

const nameField = document.querySelector(".add-form-name");
const commField = document.querySelector(".add-form-text");

export const initAddCommentListener = () => {
const button = document.querySelector(".add-form-button");
button.addEventListener("click", () => {
const date = new Date();
const month = date.getMonth();
const year = date.getFullYear() % 100;
const day = date.getDate();
const hour = date.getHours();
const minutes = date.getMinutes();
const name = sanitizeHTML(nameField.value);
const commText = sanitizeHTML(commField.value);
if ((name == "") | (commText == "")) {
alert("Вы не ввели имя или ваш комментарий пуст");
return;
}
const dateStr = `${(day < 10 ? "0" : "") + day}.${(month < 10 ? "0" : "") + month}.${(year < 10 ? "0" : "") + year} ${(hour < 10 ? "0" : "") + hour}:${(minutes < 10 ? "0" : "") + minutes}`;
const newComm = {
name: name,
text: commText,
time: dateStr,
isLiked: false,
likes: 0,
};
comments.push(newComm);
nameField.value = "";
commField.value = "";
renderComments();
});
};

export const initCommentElementListener = () => {
const commentsElements = document.querySelectorAll(".comment");
for (const commentElement of commentsElements) {
commentElement.addEventListener("click", () => {
const comm = comments[commentElement.dataset.index];
commField.value += `\n${comm.name}: ${comm.text}\n`;
});
}
};

export const initLikeListener = () => {
const likeButtons = document.querySelectorAll(".like-button");
likeButtons.forEach((likeButton) => {
likeButton.addEventListener("click", () => {
event.stopPropagation();
const index = likeButton.dataset.index;
const comm = comments[index];

comm.likes = comm.isLiked ? comm.likes - 1 : comm.likes + 1;
comm.isLiked = !comm.isLiked;

renderComments();
});
});
};
34 changes: 34 additions & 0 deletions modules/renderComments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { comments } from "./comments.js";
import {
initLikeListener,
initCommentElementListener,
} from "./initListeners.js";

export const renderComments = () => {
const list = document.querySelector(".comments");
list.innerHTML = comments
.map((comment, index) => {
console.log("It works!");
return `<li class="comment" data-index="${index}"">
<div class="comment-header">
<div>${comment.name}</div>
<div>${comment.time}</div>
</div>
<div class="comment-body">
<div class="comment-text">
${comment.text}
</div>
</div>
<div class="comment-footer">
<div class="likes">
<span class="likes-counter">${comment.likes}</span>
<button data-index="${index}" class="like-button ${comment.isLiked ? "-active-like" : ""}"></button>
</div>
</div>
</li>`;
})
.join("");

initLikeListener();
initCommentElementListener();
};
3 changes: 3 additions & 0 deletions modules/sanitizeHTML.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const sanitizeHTML = (value) => {
return value.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
};
1 change: 1 addition & 0 deletions node_modules/.bin/acorn

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/eslint

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/eslint-config-prettier

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/js-yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/loose-envify

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/node-which

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/prettier

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/resolve

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/tsc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/tsserver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading