-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.html
More file actions
856 lines (750 loc) · 37.7 KB
/
Index.html
File metadata and controls
856 lines (750 loc) · 37.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SOS: Sanctuary of Solace - Anonymous Support Community</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Playfair+Display:wght@700&display=swap');
body {
font-family: 'Inter', sans-serif;
}
.brand-title {
font-family: 'Playfair Display', serif;
}
.dark {
background-color: #0f172a;
color: #e2e8f0;
}
.dark .bg-white {
background-color: #1e293b;
}
.dark .bg-gray-50 {
background-color: #1e293b;
}
.dark .bg-gray-100 {
background-color: #334155;
}
.dark .text-gray-600 {
color: #94a3b8;
}
.dark .text-gray-700 {
color: #cbd5e1;
}
.dark .text-gray-800 {
color: #e2e8f0;
}
.dark .border-gray-200 {
border-color: #334155;
}
.dark .border-gray-300 {
border-color: #475569;
}
.dark .footer-bg {
background-color: #1e293b;
}
.support-reaction {
transition: all 0.3s ease;
}
.support-reaction:hover {
transform: scale(1.1);
}
.support-reaction.active {
animation: pulse 0.5s ease-in-out;
}
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.2); }
100% { transform: scale(1); }
}
.content-warning {
backdrop-filter: blur(4px);
}
.fade-in {
animation: fadeIn 0.5s ease-in;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.post-card {
transition: all 0.3s ease;
}
.post-card:hover {
transform: translateY(-2px);
box-shadow: 0 10px 25px rgba(0,0,0,0.1);
}
.dark .post-card:hover {
box-shadow: 0 10px 25px rgba(0,0,0,0.3);
}
.hero-gradient {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.sanctuary-icon {
animation: float 3s ease-in-out infinite;
}
@keyframes float {
0% { transform: translateY(0px); }
50% { transform: translateY(-10px); }
100% { transform: translateY(0px); }
}
.comment-section {
max-height: 0;
overflow: hidden;
transition: max-height 0.5s ease;
}
.comment-section.expanded {
max-height: 1000px;
}
.red-heart {
color: #ef4444;
}
</style>
</head>
<body class="bg-gray-50 transition-colors duration-300">
<!-- Header -->
<header class="bg-white shadow-sm sticky top-0 z-50 transition-colors duration-300">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between items-center h-16">
<div class="flex items-center">
<div class="relative">
<i class="fas fa-home text-purple-600 text-2xl mr-3 sanctuary-icon"></i>
<i class="fas fa-heart text-pink-500 text-sm absolute top-0 right-2"></i>
</div>
<div>
<h1 class="text-xl brand-title text-gray-800">SOS</h1>
<p class="text-xs text-gray-600 -mt-1">Sanctuary of Solace</p>
</div>
</div>
<nav class="flex items-center space-x-4">
<button onclick="showGuidelines()" class="text-gray-600 hover:text-gray-800 px-3 py-2 rounded-md text-sm font-medium">
<i class="fas fa-shield-alt mr-2"></i>Guidelines
</button>
<button onclick="showResources()" class="text-gray-600 hover:text-gray-800 px-3 py-2 rounded-md text-sm font-medium">
<i class="fas fa-life-ring mr-2"></i>Resources
</button>
<button onclick="toggleDarkMode()" class="text-gray-600 hover:text-gray-800 p-2 rounded-md">
<i class="fas fa-moon" id="darkModeIcon"></i>
</button>
</nav>
</div>
</div>
</header>
<!-- Hero Section -->
<section class="hero-gradient text-white py-12 mb-8">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 text-center">
<h2 class="text-4xl font-bold mb-4 brand-title">Welcome to Your Sanctuary</h2>
<p class="text-xl opacity-90 max-w-2xl mx-auto">A safe harbor where stories are heard, burdens are shared, and healing begins. You are not alone.</p>
</div>
</section>
<!-- Main Content -->
<main class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<!-- Share Your Story Section -->
<section class="mb-8 fade-in">
<div class="bg-white rounded-lg shadow-md p-6 transition-colors duration-300">
<h2 class="text-2xl font-semibold text-gray-800 mb-4">
<i class="fas fa-feather-alt text-purple-600 mr-2"></i>
Share Your Story in Sanctuary
</h2>
<p class="text-gray-600 mb-4">This is your sacred space. Speak your truth without fear. You are among friends who understand.</p>
<form onsubmit="submitPost(event)" class="space-y-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">What brings you here today?</label>
<select id="postTopic" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-600 focus:border-transparent">
<option value="regret">Regret</option>
<option value="trauma">Trauma</option>
<option value="gbv">Gender-Based Violence</option>
<option value="background">Background</option>
<option value="confessions">Confessions</option>
<option value="other">Other</option>
</select>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Your Story</label>
<textarea id="postContent" rows="4" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-600 focus:border-transparent" placeholder="In this sanctuary, you can speak freely. Share what weighs on your heart..."></textarea>
</div>
<div class="flex items-center justify-between">
<label class="flex items-center">
<input type="checkbox" id="contentWarning" class="mr-2">
<span class="text-sm text-gray-600">This content may be sensitive</span>
</label>
<button type="submit" class="bg-purple-600 text-white px-6 py-2 rounded-lg hover:bg-purple-700 transition-colors">
<i class="fas fa-dove mr-2"></i>Share in Sanctuary
</button>
</div>
</form>
</div>
</section>
<!-- Filter Section -->
<section class="mb-6">
<div class="flex flex-wrap gap-2">
<button onclick="filterPosts('all')" class="filter-btn px-4 py-2 rounded-full bg-purple-600 text-white text-sm font-medium">
All Stories
</button>
<button onclick="filterPosts('regret')" class="filter-btn px-4 py-2 rounded-full bg-gray-200 text-gray-700 text-sm font-medium hover:bg-gray-300">
Regrets
</button>
<button onclick="filterPosts('trauma')" class="filter-btn px-4 py-2 rounded-full bg-gray-200 text-gray-700 text-sm font-medium hover:bg-gray-300">
Trauma
</button>
<button onclick="filterPosts('gbv')" class="filter-btn px-4 py-2 rounded-full bg-gray-200 text-gray-700 text-sm font-medium hover:bg-gray-300">
GBV
</button>
<button onclick="filterPosts('background')" class="filter-btn px-4 py-2 rounded-full bg-gray-200 text-gray-700 text-sm font-medium hover:bg-gray-300">
Background
</button>
<button onclick="filterPosts('confessions')" class="filter-btn px-4 py-2 rounded-full bg-gray-200 text-gray-700 text-sm font-medium hover:bg-gray-300">
Confessions
</button>
</div>
</section>
<!-- Posts Section -->
<section id="postsContainer" class="space-y-4">
<!-- Posts will be dynamically added here -->
</section>
</main>
<!-- Footer -->
<footer class="bg-gray-100 py-6 mt-16 transition-colors duration-300 footer-bg">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<p class="text-center text-xs text-gray-500">
Developed with <span class="red-heart">❤️</span> by Siyamthanda Amahle Buthelezi
</p>
</div>
</footer>
<!-- Community Guidelines Modal -->
<div id="guidelinesModal" class="fixed inset-0 bg-black bg-opacity-50 hidden z-50 flex items-center justify-center p-4">
<div class="bg-white rounded-lg max-w-2xl w-full max-h-[80vh] overflow-y-auto p-6">
<div class="flex justify-between items-center mb-4">
<h3 class="text-2xl font-semibold text-gray-800">Sanctuary Guidelines</h3>
<button onclick="closeModal('guidelinesModal')" class="text-gray-500 hover:text-gray-700">
<i class="fas fa-times text-xl"></i>
</button>
</div>
<div class="space-y-4 text-gray-600">
<div>
<h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-shield-alt text-purple-600 mr-2"></i>Our Sanctuary Promise</h4>
<p>This is a sacred space of healing. Every voice deserves to be heard with respect and compassion.</p>
</div>
<div>
<h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-user-secret text-purple-600 mr-2"></i>Anonymity is Sacred</h4>
<p>Your identity is protected here. Never ask for personal information. Share only what you're comfortable with.</p>
</div>
<div>
<h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-heart text-purple-600 mr-2"></i>Practice Compassion</h4>
<p>Listen with empathy. Validate feelings. Offer support, not judgment. We're all healing together.</p>
</div>
<div>
<h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-ban text-purple-600 mr-2"></i>Protect the Sanctuary</h4>
<p>No hate speech, threats, or harmful content. Report anything that violates our safe space.</p>
</div>
<div>
<h4 class="font-semibold text-gray-800 mb-2"><i class="fas fa-exclamation-triangle text-purple-600 mr-2"></i>Content Warnings</h4>
<p>Use warnings for sensitive topics to help others prepare their hearts for your story.</p>
</div>
</div>
</div>
</div>
<!-- Resources Modal -->
<div id="resourcesModal" class="fixed inset-0 bg-black bg-opacity-50 hidden z-50 flex items-center justify-center p-4">
<div class="bg-white rounded-lg max-w-2xl w-full max-h-[80vh] overflow-y-auto p-6">
<div class="flex justify-between items-center mb-6">
<h3 class="text-2xl font-semibold text-gray-800">South Africa Support Resources</h3>
<button onclick="closeModal('resourcesModal')" class="text-gray-500 hover:text-gray-700">
<i class="fas fa-times text-xl"></i>
</button>
</div>
<div class="space-y-4">
<div class="bg-red-50 p-4 rounded-lg border-l-4 border-red-500">
<h4 class="font-semibold text-red-800 mb-2">
<i class="fas fa-phone-alt mr-2"></i>Suicide Crisis Line
</h4>
<p class="text-sm text-gray-700 font-medium">Call: <span class="text-lg">0800 567 567</span></p>
<p class="text-xs text-gray-600 mt-1">
<i class="fas fa-clock mr-1"></i>Available 24 hours in all 11 official South African languages
</p>
</div>
<div class="bg-purple-50 p-4 rounded-lg border-l-4 border-purple-500">
<h4 class="font-semibold text-purple-800 mb-2">
<i class="fas fa-female mr-2"></i>GBV Helpline
</h4>
<p class="text-sm text-gray-700 font-medium">Call: <span class="text-lg">0800 150 150</span></p>
<p class="text-xs text-gray-600 mt-1">
<i class="fas fa-phone mr-1"></i>Toll-free • Conversations with counsellors 365 days a year, 24 hours a day
</p>
</div>
<div class="bg-blue-50 p-4 rounded-lg border-l-4 border-blue-500">
<h4 class="font-semibold text-blue-800 mb-2">
<i class="fas fa-brain mr-2"></i>Mental Health Support
</h4>
<p class="text-sm text-gray-700 mb-2">
<strong>LifeLine South Africa</strong>
</p>
<p class="text-sm text-gray-700">
<i class="fas fa-envelope mr-1"></i>Email: <span class="font-medium">safetalking@lifeline.org.za</span>
</p>
<p class="text-sm text-gray-700 mt-1">
<i class="fas fa-phone mr-1"></i>Call: <span class="font-medium">0800 012 322</span> or <span class="font-medium">0861 322 322</span>
</p>
</div>
<div class="bg-green-50 p-4 rounded-lg border-l-4 border-green-500">
<h4 class="font-semibold text-green-800 mb-2">
<i class="fas fa-pills mr-2"></i>Substance Abuse Helpline
</h4>
<p class="text-sm text-gray-700 mb-2">
<strong>Department of Social Development</strong>
</p>
<p class="text-sm text-gray-700">
<i class="fas fa-phone mr-1"></i>Call: <span class="font-medium">0800 121 314</span>
</p>
<p class="text-sm text-gray-700 mt-1">
<i class="fas fa-comment mr-1"></i>SMS: <span class="font-medium">32312</span>
</p>
</div>
<div class="bg-yellow-50 p-4 rounded-lg border-l-4 border-yellow-500 mt-6">
<h4 class="font-semibold text-yellow-800 mb-2">
<i class="fas fa-info-circle mr-2"></i>Remember
</h4>
<p class="text-sm text-gray-700">
You are not alone. These services are free, confidential, and available to support you 24/7.
Reaching out is a sign of strength.
</p>
</div>
</div>
</div>
</div>
<script>
// Suppress Tailwind CSS warning
if (window.console && window.console.warn) {
const originalWarn = window.console.warn;
window.console.warn = function(...args) {
if (typeof args[0] === 'string' && args[0].includes('cdn.tailwindcss.com')) {
return;
}
originalWarn.apply(window.console, args);
};
}
// API endpoints
const API_BASE_URL = '/.netlify/functions';
// Global variables
let currentFilter = 'all';
let allPosts = [];
// Sample fallback data
const samplePosts = [
{
id: 1,
topic: 'regret',
content: 'I regret not saying goodbye to my grandmother before she passed. I was too busy with work and thought I had more time. Now I carry this guilt every day.',
timestamp: new Date(Date.now() - 3600000),
has_warning: false,
reactions: { support: 12, strength: 8, hope: 15 },
comments: [
{
id: 101,
content: "I understand this feeling. I lost my father suddenly and didn't get to say goodbye either. The guilt is overwhelming sometimes.",
timestamp: new Date(Date.now() - 1800000)
},
{
id: 102,
content: "Your grandmother knew you loved her. Sometimes life gets in the way, but that doesn't diminish your love for her.",
timestamp: new Date(Date.now() - 1200000)
}
]
},
{
id: 2,
topic: 'trauma',
content: 'I was in a car accident 5 years ago and still have panic attacks when I hear screeching tires. Therapy has helped, but some days are harder than others.',
timestamp: new Date(Date.now() - 7200000),
has_warning: true,
reactions: { support: 23, strength: 18, hope: 20 },
comments: [
{
id: 201,
content: "Have you tried EMDR therapy? It helped me with my PTSD from a similar experience.",
timestamp: new Date(Date.now() - 5400000)
}
]
},
{
id: 3,
topic: 'background',
content: 'Growing up in poverty shaped who I am today. I learned to be resilient, but sometimes I feel like I missed out on a normal childhood.',
timestamp: new Date(Date.now() - 10800000),
has_warning: false,
reactions: { support: 31, strength: 25, hope: 28 },
comments: []
},
{
id: 4,
topic: 'confessions',
content: 'I confess that I sometimes feel jealous of my friends who had easier lives. I know it\'s not fair to compare, but I can\'t help feeling like I was dealt a harder hand.',
timestamp: new Date(Date.now() - 14400000),
has_warning: false,
reactions: { support: 18, strength: 12, hope: 10 },
comments: [
{
id: 401,
content: "It's okay to feel that way. Your feelings are valid. It doesn't make you a bad person.",
timestamp: new Date(Date.now() - 9000000)
}
]
}
];
// Initialize
document.addEventListener('DOMContentLoaded', () => {
loadPosts();
checkDarkMode();
});
// Dark mode functionality
function toggleDarkMode() {
document.body.classList.toggle('dark');
const icon = document.getElementById('darkModeIcon');
icon.classList.toggle('fa-moon');
icon.classList.toggle('fa-sun');
localStorage.setItem('darkMode', document.body.classList.contains('dark'));
}
function checkDarkMode() {
if (localStorage.getItem('darkMode') === 'true') {
document.body.classList.add('dark');
document.getElementById('darkModeIcon').classList.remove('fa-moon');
document.getElementById('darkModeIcon').classList.add('fa-sun');
}
}
// Load posts from database with fallback
async function loadPosts() {
try {
const response = await fetch(`${API_BASE_URL}/posts`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const posts = await response.json();
// If database is empty, use sample data
if (posts.length === 0) {
console.log('Database is empty, using sample data');
allPosts = samplePosts;
} else {
allPosts = posts;
}
renderPosts();
} catch (error) {
console.error('Error loading posts:', error);
// Use sample data as fallback
console.log('Using sample data due to error');
allPosts = samplePosts;
renderPosts();
showNotification('Using sample data. Database connection may be unavailable.', 'info');
}
}
// Submit post to database
async function submitPost(event) {
event.preventDefault();
const topic = document.getElementById('postTopic').value;
const content = document.getElementById('postContent').value.trim();
const hasWarning = document.getElementById('contentWarning').checked;
if (!content) {
showNotification('Please share your story before submitting.', 'error');
return;
}
try {
const response = await fetch(`${API_BASE_URL}/posts`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ topic, content, hasWarning })
});
if (response.ok) {
// Reset form
document.getElementById('postContent').value = '';
document.getElementById('contentWarning').checked = false;
// Reload posts
loadPosts();
showNotification('Your story has found sanctuary. Thank you for your courage.', 'success');
} else {
throw new Error('Failed to create post');
}
} catch (error) {
console.error('Error submitting post:', error);
showNotification('Failed to share your story. Please try again.', 'error');
}
}
// Render posts
function renderPosts() {
const container = document.getElementById('postsContainer');
const filteredPosts = currentFilter === 'all'
? allPosts
: allPosts.filter(post => post.topic === currentFilter);
container.innerHTML = filteredPosts.map(post => createPostHTML(post)).join('');
}
// Create post HTML
function createPostHTML(post) {
const timeAgo = getTimeAgo(new Date(post.timestamp));
const topicLabels = {
regret: 'Regret',
trauma: 'Trauma',
gbv: 'GBV',
background: 'Background',
confessions: 'Confession',
other: 'Other'
};
return `
<div class="post-card bg-white rounded-lg shadow-md p-6 fade-in transition-colors duration-300">
<div class="flex items-start justify-between mb-4">
<div class="flex items-center space-x-3">
<div class="w-10 h-10 bg-purple-100 rounded-full flex items-center justify-center">
<i class="fas fa-dove text-purple-600"></i>
</div>
<div>
<p class="font-medium text-gray-800">Anonymous Soul</p>
<p class="text-sm text-gray-600">${timeAgo} • ${topicLabels[post.topic]}</p>
</div>
</div>
${post.has_warning ? '<span class="bg-yellow-100 text-yellow-800 text-xs px-2 py-1 rounded-full">Content Warning</span>' : ''}
</div>
<div class="mb-4">
<p class="text-gray-700 leading-relaxed">${post.content}</p>
</div>
<div class="flex items-center justify-between pt-4 border-t border-gray-200">
<div class="flex space-x-4">
<button onclick="addReaction(${post.id}, 'support')" class="support-reaction flex items-center space-x-1 text-gray-600 hover:text-purple-600">
<i class="fas fa-heart"></i>
<span class="text-sm">${post.reactions.support}</span>
</button>
<button onclick="addReaction(${post.id}, 'strength')" class="support-reaction flex items-center space-x-1 text-gray-600 hover:text-blue-600">
<i class="fas fa-fist-raised"></i>
<span class="text-sm">${post.reactions.strength}</span>
</button>
<button onclick="addReaction(${post.id}, 'hope')" class="support-reaction flex items-center space-x-1 text-gray-600 hover:text-green-600">
<i class="fas fa-sun"></i>
<span class="text-sm">${post.reactions.hope}</span>
</button>
</div>
<div class="flex space-x-2">
<button onclick="toggleComments(${post.id})" class="text-gray-400 hover:text-blue-600 text-sm">
<i class="fas fa-comment mr-1"></i>Comments (${post.comments ? post.comments.length : 0})
</button>
<button onclick="reportPost(${post.id})" class="text-gray-400 hover:text-red-600 text-sm">
<i class="fas fa-flag mr-1"></i>Report
</button>
</div>
</div>
<!-- Comments Section -->
<div id="comments-${post.id}" class="comment-section mt-4">
<div class="border-t border-gray-200 pt-4">
<h4 class="font-medium text-gray-800 mb-3">Community Support</h4>
<!-- Comments List -->
<div class="space-y-3 mb-4">
${post.comments && post.comments.length > 0
? post.comments.map(comment => createCommentHTML(comment)).join('')
: '<p class="text-sm text-gray-500">No comments yet. Be the first to offer support.</p>'}
</div>
<!-- Add Comment Form -->
<form onsubmit="addComment(event, ${post.id})" class="flex space-x-2">
<input type="text" id="comment-input-${post.id}" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-600 focus:border-transparent" placeholder="Offer your support...">
<button type="submit" class="bg-purple-600 text-white px-4 py-2 rounded-lg hover:bg-purple-700 transition-colors">
<i class="fas fa-paper-plane"></i>
</button>
</form>
</div>
</div>
</div>
`;
}
// Load comments for a post
async function loadComments(postId) {
try {
const response = await fetch(`${API_BASE_URL}/comments/${postId}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const comments = await response.json();
return comments;
} catch (error) {
console.error('Error loading comments:', error);
// Fallback to sample comments if available
const post = allPosts.find(p => p.id === postId);
return post && post.comments ? post.comments : [];
}
}
// Toggle comments section and load comments
async function toggleComments(postId) {
const commentsSection = document.getElementById(`comments-${postId}`);
const isExpanded = commentsSection.classList.contains('expanded');
if (!isExpanded) {
// Load comments
const comments = await loadComments(postId);
// Update comments in the DOM
const commentsList = commentsSection.querySelector('.space-y-3');
if (comments.length > 0) {
commentsList.innerHTML = comments.map(comment => createCommentHTML(comment)).join('');
} else {
commentsList.innerHTML = '<p class="text-sm text-gray-500">No comments yet. Be the first to offer support.</p>';
}
// Update comment count
const commentButton = commentsSection.parentElement.querySelector('.fa-comment').parentElement;
commentButton.innerHTML = `<i class="fas fa-comment mr-1"></i>Comments (${comments.length})`;
// Expand the section
commentsSection.classList.add('expanded');
} else {
// Collapse the section
commentsSection.classList.remove('expanded');
}
}
// Create comment HTML
function createCommentHTML(comment) {
const timeAgo = getTimeAgo(new Date(comment.timestamp));
return `
<div class="bg-gray-50 rounded-lg p-3">
<div class="flex items-start space-x-2">
<div class="w-6 h-6 bg-blue-100 rounded-full flex items-center justify-center flex-shrink-0">
<i class="fas fa-user text-blue-600 text-xs"></i>
</div>
<div class="flex-1">
<p class="text-sm text-gray-700">${comment.content}</p>
<p class="text-xs text-gray-500 mt-1">${timeAgo}</p>
</div>
</div>
</div>
`;
}
// Add comment to database
async function addComment(event, postId) {
event.preventDefault();
const input = document.getElementById(`comment-input-${postId}`);
const content = input.value.trim();
if (!content) {
return;
}
try {
const response = await fetch(`${API_BASE_URL}/comments/${postId}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ content })
});
if (response.ok) {
// Clear input
input.value = '';
// Reload comments for this post
const comments = await loadComments(postId);
// Update comments in the DOM
const commentsSection = document.getElementById(`comments-${postId}`);
const commentsList = commentsSection.querySelector('.space-y-3');
if (comments.length > 0) {
commentsList.innerHTML = comments.map(comment => createCommentHTML(comment)).join('');
} else {
commentsList.innerHTML = '<p class="text-sm text-gray-500">No comments yet. Be the first to offer support.</p>';
}
// Update comment count
const commentButton = commentsSection.parentElement.querySelector('.fa-comment').parentElement;
commentButton.innerHTML = `<i class="fas fa-comment mr-1"></i>Comments (${comments.length})`;
showNotification('Your support has been shared. Thank you for your kindness.', 'success');
} else {
throw new Error('Failed to add comment');
}
} catch (error) {
console.error('Error adding comment:', error);
showNotification('Failed to post your comment. Please try again.', 'error');
}
}
// Update reaction count in database
async function addReaction(postId, reactionType) {
try {
const response = await fetch(`${API_BASE_URL}/reactions/${postId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ reactionType })
});
if (response.ok) {
const data = await response.json();
// Update the reaction count in the DOM
const postCard = document.getElementById(`comments-${postId}`).parentElement;
const reactionButton = Array.from(postCard.querySelectorAll('.support-reaction')).find(
btn => btn.textContent.includes(reactionType.charAt(0).toUpperCase() + reactionType.slice(1))
);
if (reactionButton) {
reactionButton.innerHTML = `
<i class="fas fa-${reactionType === 'support' ? 'heart' : reactionType === 'strength' ? 'fist-raised' : 'sun'}"></i>
<span class="text-sm">${data.reactions[reactionType]}</span>
`;
}
// Animate the button
reactionButton.classList.add('active');
setTimeout(() => {
reactionButton.classList.remove('active');
}, 500);
} else {
throw new Error('Failed to update reaction');
}
} catch (error) {
console.error('Error updating reaction:', error);
showNotification('Failed to register your reaction. Please try again.', 'error');
}
}
// Filter posts
function filterPosts(topic) {
currentFilter = topic;
// Update button styles
document.querySelectorAll('.filter-btn').forEach(btn => {
btn.classList.remove('bg-purple-600', 'text-white');
btn.classList.add('bg-gray-200', 'text-gray-700');
});
event.target.classList.remove('bg-gray-200', 'text-gray-700');
event.target.classList.add('bg-purple-600', 'text-white');
// Re-render posts with the new filter
renderPosts();
}
// Report post
function reportPost(postId) {
if (confirm('Are you sure you want to report this post? Our sanctuary guardians will review it.')) {
showNotification('Thank you for helping protect our sanctuary. The post has been reported.', 'info');
}
}
// Show guidelines
function showGuidelines() {
document.getElementById('guidelinesModal').classList.remove('hidden');
}
// Show resources
function showResources() {
document.getElementById('resourcesModal').classList.remove('hidden');
}
// Close modal
function closeModal(modalId) {
document.getElementById(modalId).classList.add('hidden');
}
// Show notification
function showNotification(message, type) {
const colors = {
success: 'bg-green-500',
error: 'bg-red-500',
info: 'bg-blue-500'
};
const notification = document.createElement('div');
notification.className = `fixed top-20 right-4 ${colors[type]} text-white px-6 py-3 rounded-lg shadow-lg z-50 fade-in`;
notification.innerHTML = `
<div class="flex items-center">
<i class="fas fa-${type === 'success' ? 'check-circle' : type === 'error' ? 'exclamation-circle' : 'info-circle'} mr-2"></i>
<span>${message}</span>
</div>
`;
document.body.appendChild(notification);
setTimeout(() => {
notification.remove();
}, 3000);
}
// Get time ago
function getTimeAgo(date) {
const seconds = Math.floor((new Date() - date) / 1000);
if (seconds < 60) return 'just now';
if (seconds < 3600) return `${Math.floor(seconds / 60)} minutes ago`;
if (seconds < 86400) return `${Math.floor(seconds / 3600)} hours ago`;
return `${Math.floor(seconds / 86400)} days ago`;
}
// Close modals on outside click
window.onclick = function(event) {
if (event.target.classList.contains('bg-opacity-50')) {
event.target.classList.add('hidden');
}
}
</script>
</body>
</html>