-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
93 lines (79 loc) · 3.9 KB
/
script.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
document.addEventListener('DOMContentLoaded', function() {
const postForm = document.getElementById('postForm');
const postsContainer = document.querySelector('.posts');
const profileForm = document.getElementById('profileForm');
postForm.addEventListener('submit', function(event) {
event.preventDefault();
const postContent = document.getElementById('postContent').value;
if (postContent) {
const newPostId = `commentSection${postsContainer.children.length + 1}`;
const newPost = document.createElement('div');
newPost.className = 'post';
newPost.innerHTML = `
<h5>User's Post</h5>
<p>${postContent}</p>
<button class="btn btn-light btn-sm like-btn">Like <span class="badge badge-light">0</span></button>
<button class="btn btn-light btn-sm" data-toggle="collapse" data-target="#${newPostId}">Comment</button>
<button class="btn btn-light btn-sm edit-btn">Edit</button>
<button class="btn btn-light btn-sm delete-btn">Delete</button>
<div id="${newPostId}" class="collapse">
<div class="comments">
<input type="text" class="form-control comment-input" placeholder="Write a comment...">
<div class="comment-list"></div>
</div>
</div>
`;
postsContainer.insertBefore(newPost, postsContainer.firstChild);
postForm.reset();
$('#createPostModal').modal('hide');
}
});
postsContainer.addEventListener('click', function(event) {
if (event.target.classList.contains('like-btn')) {
const likeBadge = event.target.querySelector('.badge');
likeBadge.textContent = parseInt(likeBadge.textContent) + 1;
}
if (event.target.classList.contains('edit-btn')) {
const post = event.target.closest('.post');
const postContent = post.querySelector('p');
const newContent = prompt('Edit your post:', postContent.textContent);
if (newContent) {
postContent.textContent = newContent;
}
}
if (event.target.classList.contains('delete-btn')) {
if (confirm('Are you sure you want to delete this post?')) {
event.target.closest('.post').remove();
}
}
if (event.target.classList.contains('comment-input')) {
if (event.key === 'Enter' && event.target.value.trim()) {
const commentList = event.target.nextElementSibling;
const newComment = document.createElement('div');
newComment.textContent = event.target.value;
commentList.appendChild(newComment);
event.target.value = '';
}
}
});
profileForm.addEventListener('submit', function(event) {
event.preventDefault();
const username = document.getElementById('profileUsername').value;
const email = document.getElementById('profileEmail').value;
document.querySelector('.profile-info strong').textContent = username;
document.querySelector('.profile-info p').textContent = email;
$('#editProfileModal').modal('hide');
});
const friendRequests = document.querySelectorAll('.accept-btn, .decline-btn');
friendRequests.forEach(button => {
button.addEventListener('click', function(event) {
const request = event.target.closest('.list-group-item');
if (event.target.classList.contains('accept-btn')) {
alert('Friend request accepted.');
} else {
alert('Friend request declined.');
}
request.remove();
});
});
});