-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
494 lines (449 loc) · 15.7 KB
/
index.html
File metadata and controls
494 lines (449 loc) · 15.7 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SkillConnect | Home</title>
<link rel="stylesheet" href="style.css" />
<style>
/* small page-specific adjustments (keeps your main style.css intact) */
.hero {
padding: 48px 0;
background: linear-gradient(135deg, #081b4b, #3a5bb3);
color: #fff;
}
.hero-text h1 {
margin: 0 0 8px;
font-size: 1.6rem;
}
.search-bar {
display: flex;
gap: 12px;
justify-content: center;
margin: 20px auto;
max-width: 820px;
padding: 12px;
background: #fff;
border-radius: 999px;
box-shadow: 0 6px 18px rgba(12, 18, 50, 0.06);
}
.search-bar input,
.search-bar select {
border: 0;
background: transparent;
padding: 10px 14px;
outline: none;
width: 280px;
}
.posts-grid {
display: flex;
flex-direction: column;
gap: 18px;
max-width: 900px;
margin: 0 auto;
padding: 18px 12px 60px;
}
.post-card {
background: #fff;
border-radius: 14px;
overflow: hidden;
box-shadow: 0 6px 18px rgba(12, 18, 50, 0.06);
transition: transform 0.2s;
}
.post-card:hover {
transform: translateY(-6px);
}
.post-header {
display: flex;
gap: 12px;
align-items: center;
padding: 12px;
}
.user-thumb {
width: 48px;
height: 48px;
border-radius: 50%;
object-fit: cover;
border: 2px solid #29b6f6;
cursor: pointer;
}
.post-media {
width: 100%;
max-height: 420px;
object-fit: cover;
display: block;
}
.post-body {
padding: 12px;
color: #081b4b;
line-height: 1.4;
}
.post-actions {
display: flex;
gap: 12px;
padding: 10px 12px;
border-top: 1px solid #f0f3f8;
}
.post-actions button {
background: transparent;
border: 0;
cursor: pointer;
padding: 6px 10px;
border-radius: 8px;
}
.post-actions button:hover {
background: rgba(10, 27, 75, 0.04);
}
@media (max-width: 900px) {
.search-bar input {
width: 100%;
}
.hero-text h1 {
font-size: 1.2rem;
}
}
</style>
</head>
<body>
<header class="site-header">
<div class="container header-inner">
<div class="brand">SkillConnect</div>
<nav class="nav" id="navMenu">
<a href="index.html" class="nav-link">Home</a>
<a href="profile.html" class="nav-link">Profile</a>
<a href="chat.html" class="nav-link">Chats</a>
<a href="login.html" class="nav-link" id="logoutBtn">Logout</a>
</nav>
<!-- Mobile Toggle -->
<button class="hamburger" id="menu-toggle">☰</button>
</div>
</header>
<!-- Hero -->
<section class="hero">
<div class="container hero-inner">
<div class="hero-text">
<h1>Welcome to SkillConnect</h1>
<p>
Connect with skilled professionals, showcase your work, and grow
your network.
</p>
<div class="hero-cta">
<a href="register.html" class="btn primary">Get Started</a>
<a href="login.html" class="btn ghost">Login</a>
</div>
</div>
</div>
</section>
<!-- Search -->
<div class="search-bar">
<input
type="text"
id="searchInput"
placeholder="Search for skills, names or descriptions..."
/>
<select id="categorySelect">
<option value="">All Categories</option>
<option value="Tech">Tech</option>
<option value="Fashion">Fashion</option>
<option value="Catering">Catering</option>
<option value="Art">Art</option>
<option value="Photography">Photography</option>
<option value="Others">Others</option>
</select>
</div>
<!-- Posts Feed -->
<section class="container feed-section">
<h2 class="section-title">Latest Works</h2>
<div id="postsContainer" class="posts-grid"></div>
</section>
<footer class="site-footer">
<div class="container">
<p>© 2025 SkillConnect. All rights reserved.</p>
</div>
</footer>
<script>
/* --------------- Helper utilities --------------- */
const currentUser = JSON.parse(localStorage.getItem("currentUser")) || {};
const POSTS_PREFIX = "posts_"; // per-user posts key prefix
const GLOBAL_KEY = "global_posts"; // global feed (optional)
const ALLPOSTS_KEY = "allPosts"; // cached merged feed
function readJSON(key) {
try {
return JSON.parse(localStorage.getItem(key)) || [];
} catch (e) {
return [];
}
}
function writeJSON(key, val) {
localStorage.setItem(key, JSON.stringify(val));
}
/* --------------- Build merged feed --------------- */
function getAllPostsMerged() {
// gather posts_<username> entries
let merged = [];
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (!key) continue;
if (key.startsWith(POSTS_PREFIX)) {
const arr = readJSON(key);
// ensure each post has an id (use date fallback)
arr.forEach((p) => {
if (!p.id)
p.id = p.date || "id_" + Math.random().toString(36).slice(2, 9);
});
merged = merged.concat(arr);
}
}
// include global_posts if present (legacy/other)
const global = readJSON(GLOBAL_KEY);
global.forEach((p) => {
if (!p.id)
p.id = p.date || "id_" + Math.random().toString(36).slice(2, 9);
});
merged = merged.concat(global);
// dedupe by id (keep newest instance)
const byId = {};
merged.forEach((p) => {
byId[p.id] = p;
});
// convert back to array and sort newest first (by date or id fallback)
const result = Object.values(byId).sort((a, b) => {
const da = a.date ? new Date(a.date).getTime() : 0;
const db = b.date ? new Date(b.date).getTime() : 0;
return db - da;
});
// cache it
writeJSON(ALLPOSTS_KEY, result);
return result;
}
/* --------------- Render feed --------------- */
function renderFeed() {
const container = document.getElementById("postsContainer");
if (!container) return;
const posts = getAllPostsMerged();
container.innerHTML = "";
if (!posts || posts.length === 0) {
container.innerHTML =
'<p class="muted">No posts yet. Be the first to share your work!</p>';
return;
}
// get current filter/search
const search = (
document.getElementById("searchInput")?.value || ""
).toLowerCase();
const cat = (
document.getElementById("categorySelect")?.value || ""
).toLowerCase();
posts.forEach((post, idx) => {
// filtering
const matchesSearch =
!search ||
(post.text && post.text.toLowerCase().includes(search)) ||
(post.name && post.name.toLowerCase().includes(search));
const matchesCat =
!cat || (post.category && post.category.toLowerCase() === cat);
if (!matchesSearch || !matchesCat) return;
const card = document.createElement("article");
card.className = "post-card";
card.dataset.id = post.id;
// ensure avatar & required props
const avatar =
post.avatar ||
"https://cdn-icons-png.flaticon.com/512/149/149071.png";
const displayName = post.name || post.username || "Unknown";
const caption = post.text || post.desc || "";
card.innerHTML = `
<div class="post-header">
<img src="${escapeHtml(
avatar
)}" class="user-thumb" alt="avatar" onclick="openProfile('${encodeURIComponent(
post.username || ""
)}')"/>
<div>
<a href="profile.html?user=${encodeURIComponent(
post.username || ""
)}" class="name-link">${escapeHtml(displayName)}</a>
<div class="post-meta">@${escapeHtml(post.username || "")}${
post.category ? " • " + escapeHtml(post.category) : ""
}</div>
</div>
</div>
<img src="${escapeHtml(
post.image
)}" class="post-media" alt="post media" />
<div class="post-body">${escapeHtml(caption)}</div>
<div class="post-actions">
<button class="love-btn" data-id="${
post.id
}">❤️ Love <span class="count">${post.likes || 0}</span></button>
<button class="comment-btn" data-id="${
post.id
}">💬 Comment <span class="count">${
(post.comments || []).length
}</span></button>
<button class="share-btn" data-id="${post.id}">🔗 Share</button>
</div>
`;
container.appendChild(card);
});
// Attach delegated listeners
attachFeedListeners();
}
/* --------------- Event handlers (delegated) --------------- */
function attachFeedListeners() {
// love
document.querySelectorAll(".love-btn").forEach((btn) => {
btn.onclick = (e) => {
const id = btn.dataset.id;
if (!id) return;
handleLike(id, btn);
};
});
// comment
document.querySelectorAll(".comment-btn").forEach((btn) => {
btn.onclick = (e) => {
const id = btn.dataset.id;
handleComment(id);
};
});
// share
document.querySelectorAll(".share-btn").forEach((btn) => {
btn.onclick = (e) => {
const id = btn.dataset.id;
handleShare(id);
};
});
}
/* --------------- Like logic (no duplication) --------------- */
function handleLike(postId, btnElement) {
// update cached merged array
const all = readJSON(ALLPOSTS_KEY);
const idx = all.findIndex((p) => p.id === postId);
if (idx === -1) {
// fallback: rebuild merged list then find
getAllPostsMerged();
const fresh = readJSON(ALLPOSTS_KEY);
const fidx = fresh.findIndex((p) => p.id === postId);
if (fidx === -1) return;
all.splice(0, all.length, ...fresh);
}
// increment like in merged list
const post = all.find((p) => p.id === postId);
post.likes = (post.likes || 0) + 1;
// persist merged/global
writeJSON(ALLPOSTS_KEY, all);
writeJSON(GLOBAL_KEY, all);
// also update the original user's posts_<username> array if present
const username = post.username;
if (username) {
const key = POSTS_PREFIX + username;
const userPosts = readJSON(key);
const upIndex = userPosts.findIndex(
(p) => p.id === postId || p.date === post.date
);
if (upIndex !== -1) {
userPosts[upIndex].likes = post.likes;
writeJSON(key, userPosts);
} else {
// if it's not there, try to replace by matching image/date
const byDate = userPosts.findIndex(
(p) => p.date && post.date && p.date === post.date
);
if (byDate !== -1) {
userPosts[byDate].likes = post.likes;
writeJSON(key, userPosts);
}
}
}
// update UI count without re-rendering entire feed (prevents duplicates)
if (btnElement) {
const span = btnElement.querySelector(".count");
if (span) span.textContent = post.likes;
} else {
renderFeed();
}
}
/* --------------- Comment & Share --------------- */
function handleComment(postId) {
if (!currentUser.username) {
alert("Please login to comment.");
return;
}
const text = prompt("Write your comment:");
if (!text) return;
// update merged/global
const all = readJSON(ALLPOSTS_KEY);
const post = all.find((p) => p.id === postId);
if (!post) {
renderFeed();
return;
}
post.comments = post.comments || [];
post.comments.push({
user: currentUser.username,
text,
date: new Date().toISOString(),
});
writeJSON(ALLPOSTS_KEY, all);
writeJSON(GLOBAL_KEY, all);
// also update per-user list if possible
const username = post.username;
if (username) {
const key = POSTS_PREFIX + username;
const userPosts = readJSON(key);
const upIndex = userPosts.findIndex(
(p) => p.id === postId || (p.date && p.date === post.date)
);
if (upIndex !== -1) {
userPosts[upIndex] = post;
writeJSON(key, userPosts);
}
}
// re-render to update comment counts
renderFeed();
}
function handleShare(postId) {
const url = location.origin + location.pathname + `?post=${postId}`;
navigator.clipboard?.writeText(url).then(
() => alert("Post link copied to clipboard!"),
() => alert("Copy not supported")
);
}
/* --------------- Navigation helpers --------------- */
function openProfile(username) {
const u = decodeURIComponent(username || "");
if (!u) return;
window.location.href = `profile.html?user=${encodeURIComponent(u)}`;
}
/* --------------- Utility --------------- */
function escapeHtml(s) {
if (!s && s !== 0) return "";
return String(s)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """);
}
/* --------------- Search & filter wiring --------------- */
document
.getElementById("searchInput")
?.addEventListener("input", renderFeed);
document
.getElementById("categorySelect")
?.addEventListener("change", renderFeed);
/* --------------- Mobile menu toggle --------------- */
const menuToggle = document.getElementById("menu-toggle");
const nav = document.querySelector(".nav");
menuToggle?.addEventListener("click", () => nav.classList.toggle("show"));
/* --------------- Logout wiring (keeps behavior consistent) --------------- */
document.getElementById("logoutBtn")?.addEventListener("click", (e) => {
e.preventDefault();
localStorage.removeItem("currentUser");
window.location.href = "login.html";
});
/* --------------- Initialize --------------- */
// recompute and render feed
renderFeed();
</script>
</body>
</html>