-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
197 lines (181 loc) · 6.86 KB
/
app.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const commentsContainer = document.querySelector('.comments-container')
const addCommentContainer = document.querySelector('.add-new-comment')
const commentTemplate = document.querySelector('#new-comment-template').content
const commentReplyTemplate = document.querySelector('#new-comment-reply-template').content
const modalContainer = document.querySelector('.modal-container')
const comment = document.createDocumentFragment()
let dataComments = {}
onload = async () => {
let localData = localStorage.getItem('comments')
if (localData) {
let data = JSON.parse(localData)
dataComments.comments = data.comments
dataComments.currentUser = data.currentUser
createComments(dataComments.comments)
} else {
const response = await fetch('./data.json')
const data = await response.json()
localStorage.setItem('comments', JSON.stringify(data))
location.reload()
}
}
const createComments = (data) => {
data.forEach((item) => {
const {replies} = item
if (item.user.username !== dataComments.currentUser.username) {
getTemplateComment(commentTemplate, item, false,false)
if (replies.length > 0){
const {replies} = item;
createReplies(replies)
}
} else {
getTemplateComment(commentTemplate, item, true, false)
}
})
commentsContainer.appendChild(comment)
}
const createReplies = (replies) =>{
replies.forEach(reply => {
if (reply.user.username !== dataComments.currentUser.username) {
getTemplateComment(commentReplyTemplate, reply, false, true)
} else {
getTemplateComment(commentReplyTemplate, reply, true, true)
}
})
}
const getTemplateComment = (template, data, viewButtons, reply) => {
template.querySelector('.new-comment').setAttribute('id', data.id)
template.querySelector('.image').src = data.user.image.png
template.querySelector('.user-name').textContent = data.user.username
template.querySelector('.created-date').textContent = data.createdAt
if (reply){
template.querySelector('.sentence-comment').textContent = `@${data.replyingTo} ${data.content}`
}else {
template.querySelector('.sentence-comment').textContent = data.content
}
if (viewButtons) {
template.querySelector('.button--reply').style.display = 'none'
template.querySelector('.button--edit').style.display = 'flex'
template.querySelector('.button--delete').style.display = 'flex'
} else {
template.querySelector('.button--reply').style.display = 'flex'
template.querySelector('.button--edit').style.display = 'none'
template.querySelector('.button--delete').style.display = 'none'
}
let cloneTemplate = template.cloneNode(true)
comment.appendChild(cloneTemplate)
}
const addComment = () => {
const newCommentContainer = document.querySelector('.add-new-comment')
let textAreaElement = newCommentContainer.querySelector('.textarea')
let contentTextArea = textAreaElement.value
let idLastComment = dataComments.comments.at(-1).id
if (contentTextArea) {
let contextOwnComment = {
id: idLastComment + 1,
content: contentTextArea,
createdAt: '1 month ago',
score: 0,
user: {
image: {
png: dataComments.currentUser.image.png,
webp: dataComments.currentUser.image.webp,
},
username: dataComments.currentUser.username,
},
}
dataComments.comments.push(contextOwnComment)
localStorage.setItem('comments', JSON.stringify(dataComments))
getTemplateComment(commentTemplate, contextOwnComment, true)
commentsContainer.appendChild(comment)
textAreaElement.value = ''
} else {
const snackbarInfo = document.querySelector('#snackbar-info')
snackbarInfo.textContent = 'Add comment content'
snackbarInfo.className = 'show'
setTimeout(() => {
snackbarInfo.className = snackbarInfo.className.replace('show', '')
}, 3000)
}
}
const deleteComment = (e) => {
modalContainer.classList.add('display-modal')
let elementToDelete = e.parentNode.parentNode
let idCommentToDelete = elementToDelete.getAttribute('id')
let deleteCommentButton = modalContainer.querySelector('.modal-confirm-button')
deleteCommentButton.addEventListener('click', () => {
elementToDelete.remove()
dataComments.comments.splice(idCommentToDelete - 1)
localStorage.setItem('comments', JSON.stringify(dataComments))
modalContainer.classList.remove('display-modal')
})
}
const cancelDeleteComment = () => {
modalContainer.classList.remove('display-modal')
}
const editComment = (e) => {
const parentEditButton = e.parentNode.parentNode
const contentComment = parentEditButton.querySelector('.sentence-comment')
const contentCommentText = contentComment.textContent
const textareaEdit = parentEditButton.querySelector('.edit-textarea-comment')
const updateButton = parentEditButton.querySelector('.button--update')
textareaEdit.value = contentCommentText
contentComment.classList.add('hide-element')
textareaEdit.classList.remove('hide-element')
updateButton.classList.remove('hide-element')
}
const updateComment = (e) => {
const parentEditButton = e.parentNode
const textareaEdit = parentEditButton.querySelector('.edit-textarea-comment')
const updateButton = parentEditButton.querySelector('.button--update')
const contentComment = parentEditButton.querySelector('.sentence-comment')
const textareaValue = textareaEdit.value
if (textareaValue) {
contentComment.textContent = textareaValue
contentComment.classList.remove('hide-element')
textareaEdit.classList.add('hide-element')
updateButton.classList.add('hide-element')
} else {
const snackbarInfo = document.querySelector('#snackbar-info')
snackbarInfo.textContent = 'The edit field must not be empty'
snackbarInfo.className = 'show'
setTimeout(() => {
snackbarInfo.className = snackbarInfo.className.replace('show', '')
}, 3000)
}
}
const replyComment = (e) =>{
const parentReplyButton = e.parentNode.parentNode
const idMainComment = parentReplyButton.getAttribute('id')
const replyClone = addCommentContainer.cloneNode(true)
replyClone.classList.add('reply')
const replyButton = replyClone.querySelector('.button--send')
const textArea = replyClone.querySelector('.textarea')
const userReplied = parentReplyButton.querySelector('.user-name').textContent
replyButton.textContent = 'Reply'
replyButton.removeAttribute('onclick')
parentReplyButton.after(replyClone)
replyButton.addEventListener('click', ()=>{
const text = textArea.value
let idLastComment = dataComments.comments.at(-1).id
let contextOwnComment = {
id: idLastComment + 1,
content: text,
createdAt: '1 month ago',
score: 0,
replyingTo: userReplied,
user: {
image: {
png: dataComments.currentUser.image.png,
webp: dataComments.currentUser.image.webp,
},
username: dataComments.currentUser.username,
},
}
dataComments.comments[idMainComment-1].replies.push(contextOwnComment)
localStorage.setItem('comments', JSON.stringify(dataComments))
getTemplateComment(commentReplyTemplate, contextOwnComment,true,true)
parentReplyButton.after(comment)
replyClone.classList.add('hide-element')
})
}