-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathall-posts.js
More file actions
41 lines (39 loc) · 1.62 KB
/
all-posts.js
File metadata and controls
41 lines (39 loc) · 1.62 KB
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
function loadPosts() {
$.get("/api/posts", (posts) => {
for (let p of posts) {
let item = $(`
<div class="col-4">
<div class="card m-2">
<div class="card-body">
<h5 class="card-title">${p.title}</h5>
<h6 class="card-subtitle mb-2 text-muted">${p.user.username}</h6>
<p class="card-text">${p.body.substr(0, 200)}<a href="#">...read more</a></p>
<input type="text" placeholder="add suggestions" class="newComment">
<button class="card-link btnComment">Comment</button>
<ul class="comment"></ul>
</div>
</div>
</div>
`);
let commentBox = item.find(".comment");
for (let comment of p.comments) {
commentBox.append(
$("<li></li>").text(`[${comment.title}] : ${comment.body}`)
);
}
item.find(".btnComment").on("click", () => {
$.post(
"/api/comments", {
post_id: p.id,
comment_body: item.find(".newComment").val(),
user_id: JSON.parse(window.localStorage.user).id,
},
(comment) => {
$("#content").load(`/components/all-posts.html`);
}
)
})
$("#posts-container").append(item);
}
})
}