-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdashboard.html.example
More file actions
192 lines (163 loc) · 6.09 KB
/
dashboard.html.example
File metadata and controls
192 lines (163 loc) · 6.09 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
<!--
IMPORTANT WARNING:
1. Copy "dashboard.html.example" to "dashboard.html" before making any modifications.
2. NEVER edit the "dashboard.html.example" file directly.
3. DO NOT push, publish, or deploy the "dashboard.html" file to any repository or server.
This is to protect sensitive data or configurations that should remain private.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Comments System</title>
<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2.0.5"></script>
<link href="https://cdn.jsdelivr.net/npm/daisyui@4.12.14/dist/full.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="p-5 bg-base-100">
<h1 class="text-2xl font-bold mb-8">Comments</h1>
<div class="container mx-auto overflow-x-auto">
<table class="table w-full">
<thead>
<tr>
<th>ID</th>
<th>Created At</th>
<th>Name</th>
<th>Email</th>
<th>Description</th>
<th>Slug</th>
<th>Hidden</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="commentsTable"></tbody>
</table>
</div>
<div id="pagination" class="mt-4 flex gap-2"></div>
<!-- Modal -->
<dialog id="showConfirmModal" class="modal">
<div class="modal-box">
<h3 class="text-lg font-bold">Are you sure?</h3>
<p class="py-4">Do you really want to delete this comment? This action cannot be undone.</p>
<div class="modal-action">
<button id="modalYes" class="btn btn-error">Yes</button>
<button id="modalNo" class="btn">No</button>
</div>
</div>
</dialog>
<script>
const config = {
urlProject: "https://yoursupabaseurl.supabase.co",
secretKey: "your-secret-key",
domain: "https://yourdomain.com",
// domain must be without / at the end of link
};
const db = supabase.createClient(config.urlProject, config.secretKey);
const rowsPerPage = 20;
let currentPage = 1;
let totalComments = 0;
async function fetchComments(page = 1) {
try {
const offset = (page - 1) * rowsPerPage;
const { data, error, count } = await db
.from("comments")
.select("*", { count: "exact" })
.order("created_at", { ascending: false })
.range(offset, offset + rowsPerPage - 1);
if (error) throw error;
totalComments = count || 0;
renderTable(data || []);
renderPagination();
} catch (error) {
alert(`Error fetching comments: ${error.message}`);
}
}
function renderTable(data) {
const tableBody = document.getElementById("commentsTable");
tableBody.innerHTML = "";
data.forEach((comment) => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${comment.id}</td>
<td>${new Date(comment.created_at).toLocaleString()}</td>
<td>${comment.name}</td>
<td><a href="mailto:${comment.email}" target="_blank" class="hover:underline text-primary">${truncate(comment.email, 8)}</a></td>
<td>${truncate(comment.description, 160)}</td>
<td><a target="_blank" class="hover:underline text-primary" href="${config.domain}${comment.slug}">${comment.slug}</a></td>
<td>
<select class="select select-bordered select-sm" onchange="updateHidden(${comment.id}, this.value)">
<option value="false" ${!comment.hidden ? "selected" : ""}>FALSE</option>
<option value="true" ${comment.hidden ? "selected" : ""}>TRUE</option>
</select>
</td>
<td>
<button class="btn btn-error btn-sm" onclick="showConfirmModal(${comment.id})">Delete</button>
</td>
`;
tableBody.appendChild(row);
});
}
function truncate(text, length) {
return text && text.length > length ? text.slice(0, length) + "..." : text;
}
function showConfirmModal(commentId) {
const modal = document.getElementById("showConfirmModal");
const modalYes = document.getElementById("modalYes");
const modalNo = document.getElementById("modalNo");
if (!modal || !modalYes || !modalNo) {
alert("Error: Modal elements not found!");
return;
}
modal.showModal();
const confirmDelete = async () => {
try {
const { error } = await db
.from("comments")
.delete()
.eq("id", commentId);
if (error) throw error;
alert(`Comment ID ${commentId} successfully deleted.`);
modal.close();
fetchComments(currentPage);
} catch (error) {
alert(`Error deleting comment: ${error.message}`);
}
};
modalYes.onclick = confirmDelete;
modalNo.onclick = () => modal.close();
}
async function updateHidden(commentId, newValue) {
try {
const hiddenValue = newValue === "true";
const { error } = await db
.from("comments")
.update({ hidden: hiddenValue })
.eq("id", commentId);
if (error) throw error;
alert(`Comment ID ${commentId} hidden status updated to ${hiddenValue}.`);
} catch (error) {
alert(`Error updating hidden status: ${error.message}`);
}
}
function renderPagination() {
const paginationContainer = document.getElementById("pagination");
paginationContainer.innerHTML = "";
const totalPages = Math.ceil(totalComments / rowsPerPage);
for (let i = 1; i <= totalPages; i++) {
const button = document.createElement("button");
button.textContent = i;
button.className = `btn btn-sm ${i === currentPage ? "btn-primary" : "btn-secondary"}`;
button.addEventListener("click", () => {
currentPage = i;
fetchComments(currentPage);
});
paginationContainer.appendChild(button);
}
}
document.addEventListener("DOMContentLoaded", () => {
fetchComments(currentPage);
});
</script>
</body>
</html>