-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhome.php
More file actions
336 lines (299 loc) · 9.95 KB
/
Copy pathhome.php
File metadata and controls
336 lines (299 loc) · 9.95 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit;
}
$USER_ID = $_SESSION['user_id'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Mini Facebook</title>
<style>
body {
margin: 0;
background: #f0f2f5;
font-family: Arial, sans-serif;
}
.navbar {
background: white;
padding: 10px 20px;
display: flex;
align-items: center;
justify-content: space-between;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
position: sticky;
top: 0;
z-index: 100;
}
.navbar .left {
display: flex;
align-items: center;
gap: 15px;
}
.navbar .left img {
height: 40px;
cursor: pointer;
}
.navbar .left input[type="text"] {
padding: 8px;
border-radius: 20px;
border: 1px solid #ccc;
}
.navbar .center {
display: flex;
gap: 30px;
}
.navbar .center div {
cursor: pointer;
font-weight: bold;
}
.navbar .right img {
height: 36px;
border-radius: 50%;
cursor: pointer;
}
.container {
max-width: 600px;
margin: 20px auto;
}
.post-box {
background: white;
padding: 15px;
border-radius: 10px;
margin-bottom: 20px;
}
.post-box textarea {
width: 100%;
padding: 1px;
resize: none;
}
.post-box input[type="file"] {
margin-top: 10px;
}
.post-box button {
margin-top: 10px;
background: #1877f2;
color: white;
border: none;
padding: 10px 15px;
border-radius: 5px;
cursor: pointer;
}
.feed{
display: flex;
flex-direction: column;
gap: 20px;
}
.post {
background: white;
padding: 15px;
border-radius: 10px;
}
.post video, .post img {
max-width: 100%;
margin-top: 10px;
}
.post .like-comment {
display: flex;
gap: 10px;
margin-top: 10px;
}
.post .like-comment button {
padding: 5px 10px;
background: #e4e6eb;
border: none;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="navbar">
<div class="left">
<img src="https://www.facebook.com/images/fb_icon_325x325.png" width="40" onclick="location.reload()">
<input type="text" placeholder="Search Facebook">
</div>
<div class="center">
<div onclick="showTab('home')">Home</div>
<div onclick="showTab('friends')">Friends</div>
<div onclick="showTab('images')">Images</div>
<div onclick="showTab('videos')">Videos</div>
<div onclick="showTab('myposts')">MyPosts</div>
</div>
<div class="right">
<img src="https://cdn-icons-png.flaticon.com/512/847/847969.png" onclick="toggleUserDropdown()">
</div>
</div>
<!-- User Dropdown -->
<div id="userDropdown" style="position:fixed; top:60px; right:20px; background:#fff; border-radius:10px; padding:15px; box-shadow:0 0 10px rgba(0,0,0,0.1); display:none; z-index:200;">
<p><strong>User Name:</strong> <?php echo $_SESSION['full_name']; ?></p>
<p><strong>Data Of Birth:</strong> <?php echo $_SESSION['dob']; ?></p>
<p><strong>Gender:</strong> <?php echo $_SESSION['gender']; ?></p>
<p><strong>Mail Id:</strong> <?php echo $_SESSION['email']; ?></p>
<button onclick="logout()" style="background:red; color:white; padding:5px 10px; border:none; border-radius:5px;">Logout</button>
</div>
<div class="container" id="home-tab" style="display:block;">
<div class="post-box">
<h3>Create Post</h3>
<form id="postForm">
<textarea name="content" rows="4" placeholder="What's on your mind?"></textarea><br>
<input type="file" name="media" accept="image/*,video/*"><br>
<button type="submit">Post</button>
</form>
</div>
<div class="feed" id="feed_home"></div>
</div>
<div class="container" id="images-tab" style="display:none;">
<div class="feed" id="feed_image"></div>
</div>
<div class="container" id="videos-tab" style="display:none;">
<div class="feed" id="feed_video"></div>
</div>
<div class="container" id="friends-tab" style="display:none;">
<h3>Friends Feature Coming Soon</h3>
</div>
<div class="container" id="myposts-tab" style="display:none;">
<div class="feed" id="feed_myposts"></div>
</div>
<script>
const USER_ID = "<?php echo $_SESSION['user_id']; ?>";
function showTab(tab) {
const tabs = ['home', 'images', 'videos', 'friends','myposts'];
tabs.forEach(t => {
document.getElementById(`${t}-tab`).style.display = (t === tab) ? 'block' : 'none';
});
if (tab === 'home') loadFeed();
if (tab === 'images') loadFilteredFeed('image');
if (tab === 'videos') loadFilteredFeed('video');
if (tab === 'myposts') loadMyPosts('myposts');
}
function renderPost(post, container, tabName) {
const isLiked = post.likes.includes(USER_ID);
const commentHtml = post.comments.map(c => `
<div style="margin-left:10px;">
<b>${c.userId?.name || 'Anonymous'}</b>: ${c.text}
</div>
`).join('');
const el = document.createElement('div');
el.className = 'post';
el.innerHTML = `
<div style="display:flex;align-items:center;gap:10px;">
<img src="https://cdn-icons-png.flaticon.com/512/847/847969.png" style="width:30px;border-radius:50%;">
<strong>${post.userId?.name || 'Unknown'}</strong>
</div>
<p>${post.content}</p>
${post.mediaType === 'image' ? `<img src="${post.mediaUrl}">` : ''}
${post.mediaType === 'video' ? `<video controls src="${post.mediaUrl}"></video>` : ''}
<div class="like-comment">
<button onclick="toggleLike('${post._id}', '${tabName}')">
${isLiked ? '❤️ Liked' : '🤍 Like'} (${post.likes.length})
</button>
<button onclick="toggleCommentBox('${post._id}', '${tabName}')">💬 Comment</button>
</div>
<div id="comments-${tabName}-${post._id}" style="margin-top:10px; display:none;">
<input type="text" id="comment-input-${tabName}-${post._id}" placeholder="Write a comment..." style="width:80%;">
<button onclick="submitComment('${post._id}', '${tabName}')">Post</button>
${commentHtml}
</div>
`;
container.appendChild(el);
}
function loadFeed() {
fetch('http://localhost:5000/api/posts')
.then(res => res.json())
.then(data => {
const feed = document.getElementById('feed_home');
feed.innerHTML = '';
data.forEach(post => renderPost(post, feed, 'home'));
});
}
function loadFilteredFeed(type) {
fetch('http://localhost:5000/api/posts')
.then(res => res.json())
.then(data => {
const filtered = data.filter(post => post.mediaType === type);
const feed = document.getElementById(`feed_${type}`);
feed.innerHTML = '';
const tabName = type === 'image' ? 'images' : 'videos';
filtered.forEach(post => renderPost(post, feed, tabName));
});
}
function loadMyPosts() {
fetch('http://localhost:5000/api/posts')
.then(res => res.json())
.then(data => {
const feed = document.getElementById('feed_myposts');
feed.innerHTML = '';
data.forEach(post => {
if(post.userId.userId === USER_ID) {
renderPost(post, feed, 'myposts');
}
});
});
}
// Handle post submission (image/video/text)
document.getElementById('postForm').addEventListener('submit', function(e) {
e.preventDefault();
const formData = new FormData(this);
formData.append("userId", String(USER_ID));
fetch('http://localhost:5000/api/posts', {
method: 'POST',
body: formData
})
.then(() => {
this.reset();
loadFeed();
});
});
function toggleLike(postId, tab = 'home') {
fetch('http://localhost:5000/api/posts/like-toggle', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ postId, userId: USER_ID })
}).then(() => {
if (tab === 'home') loadFeed();
else if (tab === 'images') loadFilteredFeed('image');
else if (tab === 'videos') loadFilteredFeed('video');
else if (tab === 'myposts') loadMyPosts();
});
}
function toggleCommentBox(postId, tab = 'home') {
const box = document.getElementById(`comments-${tab}-${postId}`);
if (box) {
box.style.display = (box.style.display === 'none' || box.style.display === '') ? 'block' : 'none';
} else {
console.warn(`Comment box not found for postId: ${postId} in tab ${tab}`);
}
}
window.onload = () => loadFeed();
function submitComment(postId, tab = 'home') {
const input = document.getElementById(`comment-input-${tab}-${postId}`);
const text = input.value.trim();
if (!text) return;
fetch('http://localhost:5000/api/posts/comment', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ postId, userId: USER_ID, text })
}).then(() => {
input.value = '';
if (tab === 'home') loadFeed();
else if (tab === 'images') loadFilteredFeed('image');
else if (tab === 'videos') loadFilteredFeed('video');
else if (tab === 'myposts') loadMyPosts();
});
}
</script>
<script>
function toggleUserDropdown() {
const box = document.getElementById('userDropdown');
box.style.display = (box.style.display === 'none' || box.style.display === '') ? 'block' : 'none';
}
function logout() {
fetch('logout.php') // will make this next
.then(() => window.location.href = 'login.php');
}
</script>
</body>
</html>