-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.js
More file actions
162 lines (132 loc) · 4.88 KB
/
post.js
File metadata and controls
162 lines (132 loc) · 4.88 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
// Get alert ID from URL
const urlParams = new URLSearchParams(window.location.search);
const alertId = urlParams.get('alertId');
async function getCurrentUser() {
try {
const { data: { user }, error } = await supabase.auth.getUser();
if (error) throw error;
return user;
} catch (error) {
return null;
}
}
async function loadPostDetails() {
try {
const { data: post, error } = await supabase
.from('alerts')
.select('*')
.eq('id', alertId)
.single();
if (error) throw error;
const postDetails = document.getElementById('post-details');
if (!postDetails) {
return;
}
postDetails.innerHTML = `
<div class="alert-name">${post.name}</div>
<div class="alert-message">${post.message}</div>
<div class="alert-timestamp">${new Date(post.timestamp).toLocaleString()}</div>
<div class="alert-votes">
True: ${post.verified_votes} | Fake: ${post.discard_votes} | Comments: ${post.comments_count}
</div>
`;
} catch (error) {
const postDetails = document.getElementById('post-details');
if (postDetails) {
postDetails.innerHTML = `<p>Error loading post</p>`;
}
}
}
async function loadComments() {
try {
// Fetch comments for the alert
const { data: comments, error } = await supabase
.from('comments')
.select('*')
.eq('alert_id', alertId);
if (error) throw error;
// Extract unique user IDs from comments
const userIds = [...new Set(comments.map(comment => comment.user_id))];
// Fetch user details
const { data: users, error: userError } = await supabase
.from('users')
.select('id, full_name')
.in('id', userIds);
if (userError) throw userError;
// Create a map from user IDs to full names
const userMap = new Map(users.map(user => [user.id, user.full_name]));
const commentsList = document.getElementById('comments-list');
if (!commentsList) {
return;
}
commentsList.innerHTML = '';
comments.forEach(comment => {
const commentElement = document.createElement('div');
commentElement.className = 'comment';
const userName = userMap.get(comment.user_id) || 'Unknown User';
commentElement.innerHTML = `
<p><strong>${userName}</strong>: ${comment.comment_text}</p>
<p>${new Date(comment.timestamp).toLocaleString()}</p>
`;
commentsList.appendChild(commentElement);
});
} catch (error) {
const commentsList = document.getElementById('comments-list');
if (commentsList) {
commentsList.innerHTML = `<p>Error loading comments</p>`;
}
}
}
async function submitComment(event) {
event.preventDefault();
const commentText = document.getElementById('comment-text').value;
if (!commentText) return;
try {
const user = await getCurrentUser();
if (!user) throw new Error('User not authenticated');
// Check if user ID exists in the users table
const { data: userData, error: userError } = await supabase
.from('users')
.select('id')
.eq('id', user.id)
.single();
if (userError || !userData) {
throw new Error('User ID does not exist in the users table');
}
// Insert the comment
const { error } = await supabase
.from('comments')
.insert({
alert_id: alertId,
user_id: user.id,
comment_text: commentText,
timestamp: new Date().toISOString()
});
if (error) throw error;
// Increment comment count in `alerts`
const { data: post, error: postError } = await supabase
.from('alerts')
.select('comments_count')
.eq('id', alertId)
.single();
if (postError) throw postError;
await supabase
.from('alerts')
.update({ comments_count: post.comments_count + 1 })
.eq('id', alertId);
document.getElementById('comment-form').reset();
await loadComments();
} catch (error) {
const commentsList = document.getElementById('comments-list');
if (commentsList) {
commentsList.innerHTML = `<p>Error submitting comment</p>`;
}
}
}
// Initialize the post details and comments on page load
async function initPostPage() {
await loadPostDetails();
await loadComments();
}
document.getElementById('comment-form').addEventListener('submit', submitComment);
window.addEventListener('load', initPostPage);