forked from GlebkaF/webdev-dom-homework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw-2.10.js
147 lines (130 loc) · 3.84 KB
/
hw-2.10.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const { log } = console;
"use strict";
const commentsList = document.querySelectorAll(".commentsList");
const cardElement = document.getElementById("comments");
const buttonElement = document.getElementById("add-buttonId");
const inputName = document.getElementById("nameTextId");
const inputText = document.getElementById("commentTextId");
let time = {
hour: 'numeric',
minute: 'numeric',
};
let year = {
year: 'numeric',
month: 'numeric',
day: 'numeric',
};
const users = [
{
name: "Глеб Фокин",
time: "12.02.22 12:18",
comment: "Это будет первый комментарий на этой странице",
likes: 3,
likeStatus: false,
},
{
name: "Варвара Н.",
time: "13.02.22 19:22",
comment: "Мне нравится как оформлена эта страница! ❤",
likes: 75,
likeStatus: true,
},
];
//Блокировка кнопки "Написать"
function checkInputForm() {
if (inputName.value.trim() === "" || inputText.value.trim() === "") {
//Блокировка и серый
buttonElement.disabled = true;
buttonElement.classList.add("active-input");
} else {
//Разблокировка и не серый
buttonElement.disabled = false;
buttonElement.classList.remove("active-input");
}
};
inputName.addEventListener("input", checkInputForm);
inputText.addEventListener("input", checkInputForm);
checkInputForm();
const addLikeClickButton = () => {
const clickLikes = document.querySelectorAll('.like-button');
for (const clickLike of clickLikes) {
clickLike.addEventListener("click", () => {
const index = clickLike.dataset.index;
if (users[index].likeStatus === false) {
users[index].likes++;
users[index].likeStatus = true;
} else if (users[index].likeStatus === true) {
users[index].likes--;
users[index].likeStatus = false;
}
renderCommentList();
});
};
};
renderCommentList = () => {
const userHtml = users.map((user, index) => {
return `<li class="comment">
<div class="comment-header">
<div>${user.name}</div>
<div>${user.time}</div>
</div>
<div class="comment-body">
<div class="comment-text">
${user.comment}
</div>
</div>
<div class="comment-footer">
<div class="likes">
<span class="likes-counter">${user.likes}</span>
<button data-index="${index}" class="like-button ${user.likeStatus === true ? '-active-like' : ''}"></button>
</div>
</div>
</li>`
}).join('');
cardElement.innerHTML = userHtml;
addLikeClickButton();
checkInputForm();
};
renderCommentList();
//Enter
document.addEventListener("keyup", (event) => {
if (event.code === 'Enter') {
document.getElementById("add-buttonId").click();
return;
}
});
buttonElement.addEventListener("click", () => {
const date = new Date();
inputText.classList.remove("error");
inputName.classList.remove("error");
if ((inputText.value.length === 0) || (inputName.value.length === 0)) {
if ((inputText.value.length === 0) && (inputName.value.length === 0)) {
inputName.classList.add("error");
inputText.classList.add("error");
return;
}
else if (inputName.value.length === 0) {
inputName.classList.add("error");
return;
}
else if (inputText.value.length === 0) {
inputText.classList.add("error");
return;
}
};
users.push({
name: inputName.value,
time: date.toLocaleString("ru", year) + " " + date.toLocaleString('ru', time),
comment: inputText.value,
likes: 0,
likeStatus: false,
});
renderCommentList();
addLikeClickButton();
inputName.value = "";
inputText.value = "";
checkInputForm();
});
renderCommentList();
checkInputForm();
console.log("It works!");