-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.handlebars
More file actions
73 lines (65 loc) · 2.51 KB
/
post.handlebars
File metadata and controls
73 lines (65 loc) · 2.51 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
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
{{!-- Partial for a single post --}}
<link rel="stylesheet" href="/css/homeStyle.css">
<div class="content-container">
<div class="post-form">
<div class="post-avatar">
<img src="/avatar/{{username}}" alt="Avatar for {{username}}" class="avatar-image">
</div>
<div class="post-content preserve-newlines">
<h3>{{ title }}</h3>
<p>{{ content }}</p>
<div class="post-status-bar">
<!-- Render like button -->
<button class="like-button" onclick="handleLikeClick(event)" data-id="{{ id }}">
<i class="far fa-heart"></i> Like
</button>
<!-- Render delete button -->
<button class="delete-button" onclick="handleDeleteClick(event)" data-id="{{ id }}">
<i class="fas fa-trash-alt"></i> Delete
</button>
</div>
</div>
</div>
</div>
<script>
function handleLikeClick(event) {
const postId = event.target.getAttribute('data-id');
// Make a POST request to the server to update the likes count for this post
fetch(`/like/${postId}`, {
method: 'POST',
})
.then(response => {
if (!response.ok) {
throw new Error('Failed to like post');
}
// If the request is successful, update the UI to reflect the new like count
const likeButton = event.target;
const likeCount = likeButton.nextElementSibling;
likeCount.textContent = parseInt(likeCount.textContent) + 1;
})
.catch(error => {
console.error('Error liking post:', error);
// Handle error
});
}
// Delete Function called
function handleDeleteClick(event) {
const postId = event.target.getAttribute('data-id');
// Make a POST request to the server to delete the post
fetch(`/delete/${postId}`, {
method: 'POST',
})
.then(response => {
if (!response.ok) {
throw new Error('Failed to delete post');
}
// If the request is successful, remove the post from the UI
const postContainer = event.target.closest('.content-container');
postContainer.remove();
})
.catch(error => {
console.error('Error deleting post:', error);
// Handle error
});
}
</script>