-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
254 lines (241 loc) · 8.57 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
class comments {
constructor(){
this.Data_app();
}
/*display the exiting comments */
main_comment(data){
return `
<section class="comment">
<div class="score">
<button class="plus operator"><img src="images/icon-plus.svg" alt=""></button>
<button class="number-score">${data.score}</button>
<button class="minus operator"><img src="images/icon-minus.svg" alt=""></button>
</div>
<div class="comments" data-comment="main" data-id="${data.id}">
<div class="thumbnail">
<div class="image">
<img src="${data.user.image.png}" alt="hiiihi">
<span class="name">${data.user.username}</span>
<span class="date">${data.createdAt}</span>
</div>
<div class="replay hundler replay-commment">Replay</div>
</div>
<div class="content">
<p>
${data.content}
</p>
</div>
</div>
</section>
<div class="wrap">
${(data.replies.map(function(item){ return `<section class="replay-comments">
<div class="score">
<button class="plus operator"><img src="images/icon-plus.svg" alt=""></button>
<button class="number-score">${item.score}</button>
<button class="minus operator"><img src="images/icon-minus.svg" alt=""></button>
</div>
<div class="comments" data-comment="replies" data-id="${item.id}">
<div class="thumbnail">
<div class="image">
<img src="${item.user.image.png}" alt="hiiihi">
<span class="name">${item.user.username}</span>
<span class="date">${item.createdAt}</span>
</div>
<div class="replay hundler">Replay</div>
</div>
<div class="content">
<p>
${item.content}
</p>
</div>
</div>
</section>
`}))}
<section class="add-comment replay-comments">
<div class="comments">
<div class="thumbnail">
<div class="image">
<img src="./images/avatars/image-juliusomo.png" alt="hiiihi">
</div>
</div>
</div>
<form class="add-post">
<textarea placeholder="add your comment..."></textarea>
<button type="submit" class="send">SEND</button>
</form>
</section>
</div>
`
}
/**display add comment section*/
/**add comment */
add_comment() {
return `<section class="add-comment comments">
<div class="comments data-comment="main">
<div class="thumbnail">
<div class="image">
<img src="./images/avatars/image-juliusomo.png" alt="hiiihi">
</div>
</div>
</div>
<form class="add-post">
<textarea placeholder="add your comment..."></textarea>
<button type="submit" class="send">SEND</button>
</form>
</section>`
}
current_user_commentRender(user,comment_handler){
let section= document.querySelectorAll("section");
section.forEach((e)=>{
let comment=e.querySelector(".comments");
let replay=e.querySelector(".replay");
let thumbnail=e.querySelector(".thumbnail");
if(e.innerHTML.includes(`<span class="name">${user}</span>`)==true){
comment.classList.toggle("user-comment");
//console.log(comment_handler,replay);
//thumbnail.replaceChild(comment_handler,replay);
replay.classList.toggle("comment-handler");
replay.innerHTML=`<button class="delete">delete</button>
<button class="edit">edit</button>`;
replay.classList.remove("replay");
}
})
}
display_reply(){
let wrap=document.querySelectorAll(".wrap");
console.log(wrap[1]);
let replay=document.querySelectorAll(".replay-commment");
replay.forEach((item, index) => {
console.log("before event"+index);
item.addEventListener("click",(e)=>{
console.log("after event"+index);
wrap[index].style.display="flex";
})
})
}
/**edit comment */
EditComment(){
let editBtn=document.querySelectorAll(".edit");
editBtn.forEach((item, index) =>{
item.addEventListener("click",(e)=>{
let parent=e.target.parentNode.parentNode.parentNode;
let content=parent.querySelector("p");
//alert(content.innerHTML);
const form = document.createElement("form");
form.innerHTML=` <textarea></textarea>
<button type="submit" class="send">Update</button>`;
content.replaceWith(form);
let textarea=form.querySelector("textarea");
textarea.value=content.innerHTML;
form.addEventListener("submit",(e)=>{
e.preventDefault();
let comment=parent.parentNode.querySelector(".comments");
let data_id=comment.getAttribute("data-id");
alert(typeof(data_id));
//return;
fetch('https://my-json-server.typicode.com/ibtihelbs/interactive/comments/'+data_id, {
method: 'PATCH',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
content: textarea.value,
})
}).then((response) => response.json())
.then((json) => console.log(json));
//console.log();
})
})
})
}
/**adding comment */
submit_comment() {
let add_post= document.querySelectorAll(".add-post");
add_post.forEach(element => {
element.addEventListener("submit",(e)=>{
e.preventDefault();
let parent=e.target.parentNode.parentNode;
let data=parent.querySelector(".comments");
//console.log(data);
//return;
let content=element.querySelector("textarea").value;
//console.log(content);
/**?author.name=typicode */
fetch('https://my-json-server.typicode.com/ibtihelbs/interactive/comments', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8'
},
body: JSON.stringify({
content: content,
createdAt: "1 month ago",
score: 0,
user: {
"image": {
"png": "./images/avatars/image-juliusomo.png",
"webp": "./images/avatars/image-juliusomo.webp"
},
"username": "juliusomo"
},
replies: []})
}).then((response) => response.json())
.then((json) => console.log(json));;
})
});
}
/**delete comment */
delete_comment() {
let delete_comment=document.querySelectorAll(".delete");
let delete_modal= document.querySelector(".delete-comment");
delete_comment.forEach(element => {
element.addEventListener("click",(e)=>{
e.preventDefault();
delete_modal.style.display='flex';
delete_modal.addEventListener("click",(f)=>{
let btn=f.target.closest('button');
if(!btn)return;
if(btn.classList.contains('cancel-btn')){
delete_modal.style.display='none';
}else{
let parent=element.parentNode.parentNode.parentNode.parentNode;
let comment=parent.querySelector(".comments");
let data_id=comment.getAttribute("data-id");
alert(data_id);
fetch('https://my-json-server.typicode.com/ibtihelbs/interactive/comments/'+data_id, {
method: 'DELETE'
})
.then(res => res.json())
.then(data => {
console.log(data);
})
.catch(err => console.log(err));
}
})
})
})
}
/**remove comment */
async Data_app() {
const response = await fetch('db.json');
const data = await response.json();
let comment = data.comments;
//console.log(comment[0])
let current_user=data.currentUser.username;
/**for main_comments data should be data.comments */
comment.map((item)=>{
document.querySelector("main").innerHTML += this.main_comment(item);
});
document.querySelector("main").insertAdjacentHTML("beforeend",this.add_comment());
let comment_handler=document.createElement("div");
comment_handler.setAttribute("class", "comment-handler hundler");
comment_handler.innerHTML=`<span class="delete">delete</span>
<span class="edit">edit</span>`;
this.current_user_commentRender(current_user,comment_handler);
this.display_reply();
this.submit_comment();
this.EditComment();
this.delete_comment();
}
}
let comment = new comments();