deneljs 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +57 -2
- package/drizzle/0000_lonely_reptil.sql +27 -0
- package/drizzle/0001_misty_wendigo.sql +74 -0
- package/drizzle/meta/0000_snapshot.json +179 -0
- package/drizzle/meta/0001_snapshot.json +492 -0
- package/drizzle/meta/_journal.json +20 -0
- package/package.json +55 -4
- package/src/admin/auth.ts +851 -0
- package/src/admin/layout.ts +419 -0
- package/src/admin/pages/categories.ts +223 -0
- package/src/admin/pages/dashboard.ts +192 -0
- package/src/admin/pages/media.ts +270 -0
- package/src/admin/pages/posts.ts +804 -0
- package/src/admin/pages/settings.ts +169 -0
- package/src/admin/pages/themes.ts +461 -0
- package/src/admin/pages/users.ts +501 -0
- package/src/admin/routes.ts +455 -0
- package/src/admin/tiptap-bundle.ts +48 -0
- package/src/admin/tiptap-entry.ts +226 -0
- package/src/ai/generator.ts +296 -0
- package/src/ai/index.ts +22 -0
- package/src/ai/schema.ts +13 -0
- package/src/ai/spec.ts +193 -0
- package/src/cli.ts +151 -0
- package/src/db/index.ts +278 -0
- package/src/db/schema.ts +115 -0
- package/src/engine/index.ts +52 -0
- package/src/engine/renderer.ts +533 -0
- package/src/engine/sandbox.ts +108 -0
- package/src/engine/schema.ts +71 -0
- package/src/engine/validator.ts +404 -0
- package/src/routes/admin-api.ts +1035 -0
- package/src/routes/posts.ts +294 -0
- package/src/routes/uploads.ts +119 -0
- package/src/server.ts +672 -0
- package/src/utils/sanitize-html.ts +145 -0
- package/themes/.gitkeep +1 -0
- package/themes/default/assets/style.css +799 -0
- package/themes/default/layouts/base.liquid +31 -0
- package/themes/default/layouts/home.liquid +180 -0
- package/themes/default/layouts/page.liquid +21 -0
- package/themes/default/layouts/post.liquid +55 -0
- package/themes/default/partials/footer.liquid +33 -0
- package/themes/default/partials/navbar.liquid +49 -0
- package/themes/default/theme.json +59 -0
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
import { serializeSafeJson } from "../layout";
|
|
2
|
+
|
|
3
|
+
export interface UserListItem {
|
|
4
|
+
id: number;
|
|
5
|
+
name: string;
|
|
6
|
+
email: string;
|
|
7
|
+
role: "admin" | "editor" | "author";
|
|
8
|
+
avatar_url: string;
|
|
9
|
+
status: "active" | "suspended";
|
|
10
|
+
created_at: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Render the User Manager view (/admin/users).
|
|
15
|
+
*/
|
|
16
|
+
export function renderUsersPage(
|
|
17
|
+
initialUsers: UserListItem[],
|
|
18
|
+
currentUserId: number,
|
|
19
|
+
): string {
|
|
20
|
+
const seed = serializeSafeJson({ users: initialUsers, currentUserId });
|
|
21
|
+
|
|
22
|
+
return `
|
|
23
|
+
<script id="users-seed" type="application/json">${seed}</script>
|
|
24
|
+
<script>
|
|
25
|
+
document.addEventListener("alpine:init", () => {
|
|
26
|
+
Alpine.data("usersManager", () => ({
|
|
27
|
+
users: [],
|
|
28
|
+
currentUserId: null,
|
|
29
|
+
modalOpen: false,
|
|
30
|
+
editingId: null,
|
|
31
|
+
form: { name: "", email: "", password: "", role: "author", status: "active" },
|
|
32
|
+
errors: [],
|
|
33
|
+
saving: false,
|
|
34
|
+
confirmDeleteId: null,
|
|
35
|
+
|
|
36
|
+
detailsOpen: false,
|
|
37
|
+
detailsLoading: false,
|
|
38
|
+
detailsData: null,
|
|
39
|
+
|
|
40
|
+
init() {
|
|
41
|
+
const el = document.getElementById("users-seed");
|
|
42
|
+
const seed = el ? JSON.parse(el.textContent || "{}") : {};
|
|
43
|
+
this.users = Array.isArray(seed.users) ? seed.users : [];
|
|
44
|
+
this.currentUserId = seed.currentUserId || null;
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
formatDate(sec) {
|
|
48
|
+
if (!sec) return "—";
|
|
49
|
+
return new Date(sec * 1000).toLocaleDateString(undefined, {
|
|
50
|
+
year: "numeric",
|
|
51
|
+
month: "short",
|
|
52
|
+
day: "numeric",
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
initials(name) {
|
|
57
|
+
return String(name || "U")
|
|
58
|
+
.trim()
|
|
59
|
+
.split(/\\s+/)
|
|
60
|
+
.slice(0, 2)
|
|
61
|
+
.map((part) => part[0] ? part[0].toUpperCase() : "")
|
|
62
|
+
.join("") || "U";
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
openCreate() {
|
|
66
|
+
this.editingId = null;
|
|
67
|
+
this.errors = [];
|
|
68
|
+
this.form = { name: "", email: "", password: "", role: "author", status: "active" };
|
|
69
|
+
this.modalOpen = true;
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
openEdit(u) {
|
|
73
|
+
this.editingId = u.id;
|
|
74
|
+
this.errors = [];
|
|
75
|
+
this.form = {
|
|
76
|
+
name: u.name,
|
|
77
|
+
email: u.email,
|
|
78
|
+
password: "",
|
|
79
|
+
role: u.role,
|
|
80
|
+
status: u.status,
|
|
81
|
+
};
|
|
82
|
+
this.detailsOpen = false;
|
|
83
|
+
this.modalOpen = true;
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
async openDetails(id) {
|
|
87
|
+
this.detailsOpen = true;
|
|
88
|
+
this.detailsLoading = true;
|
|
89
|
+
this.detailsData = null;
|
|
90
|
+
try {
|
|
91
|
+
const res = await fetch("/api/admin/users/" + id, { cache: "no-store" });
|
|
92
|
+
const data = await res.json();
|
|
93
|
+
if (!res.ok || !data.success) {
|
|
94
|
+
this.detailsOpen = false;
|
|
95
|
+
window.cmsToast("error", (data.errors && data.errors[0]) || "Failed to load user details.");
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
this.detailsData = data;
|
|
99
|
+
} catch {
|
|
100
|
+
this.detailsOpen = false;
|
|
101
|
+
window.cmsToast("error", "Network error loading user details.");
|
|
102
|
+
} finally {
|
|
103
|
+
this.detailsLoading = false;
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
async reload() {
|
|
108
|
+
const res = await fetch("/api/admin/users?t=" + Date.now(), { cache: "no-store" });
|
|
109
|
+
if (res.ok) {
|
|
110
|
+
const data = await res.json();
|
|
111
|
+
this.users = Array.isArray(data.users) ? data.users : [];
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
async save() {
|
|
116
|
+
this.saving = true;
|
|
117
|
+
this.errors = [];
|
|
118
|
+
const isEdit = Boolean(this.editingId);
|
|
119
|
+
const url = isEdit ? "/api/admin/users/" + this.editingId : "/api/admin/users";
|
|
120
|
+
const method = isEdit ? "PUT" : "POST";
|
|
121
|
+
|
|
122
|
+
const payload = {
|
|
123
|
+
name: this.form.name,
|
|
124
|
+
email: this.form.email,
|
|
125
|
+
role: this.form.role,
|
|
126
|
+
status: this.form.status,
|
|
127
|
+
...(this.form.password ? { password: this.form.password } : {}),
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
const res = await fetch(url, {
|
|
132
|
+
method,
|
|
133
|
+
headers: { "content-type": "application/json" },
|
|
134
|
+
body: JSON.stringify(payload),
|
|
135
|
+
});
|
|
136
|
+
const data = await res.json();
|
|
137
|
+
if (!res.ok || !data.success) {
|
|
138
|
+
this.errors = Array.isArray(data.errors) ? data.errors : ["Failed to save user."];
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
await this.reload();
|
|
142
|
+
this.modalOpen = false;
|
|
143
|
+
window.cmsToast("success", isEdit ? "User updated." : "User created.");
|
|
144
|
+
} catch {
|
|
145
|
+
this.errors = ["Network error while saving user."];
|
|
146
|
+
} finally {
|
|
147
|
+
this.saving = false;
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
async deleteUser(id) {
|
|
152
|
+
try {
|
|
153
|
+
const res = await fetch("/api/admin/users/" + id, { method: "DELETE" });
|
|
154
|
+
const data = await res.json();
|
|
155
|
+
if (!res.ok || !data.success) {
|
|
156
|
+
window.cmsToast("error", (data.errors && data.errors[0]) || "Failed to delete user.");
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
this.confirmDeleteId = null;
|
|
160
|
+
await this.reload();
|
|
161
|
+
window.cmsToast("success", "User deleted.");
|
|
162
|
+
} catch {
|
|
163
|
+
window.cmsToast("error", "Network error while deleting user.");
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
}));
|
|
167
|
+
});
|
|
168
|
+
</script>
|
|
169
|
+
|
|
170
|
+
<div x-data="usersManager" class="space-y-5">
|
|
171
|
+
<div class="flex items-center justify-between gap-3">
|
|
172
|
+
<p class="text-sm text-slate-500">Manage CMS users, roles, account details, and statuses.</p>
|
|
173
|
+
<button
|
|
174
|
+
type="button"
|
|
175
|
+
@click="openCreate()"
|
|
176
|
+
class="inline-flex items-center gap-1.5 px-4 py-2 rounded-lg bg-slate-900 text-white text-sm font-medium hover:bg-slate-800 transition"
|
|
177
|
+
>
|
|
178
|
+
<i data-lucide="user-plus" class="w-4 h-4"></i>
|
|
179
|
+
New User
|
|
180
|
+
</button>
|
|
181
|
+
</div>
|
|
182
|
+
|
|
183
|
+
<div class="bg-white rounded-xl border border-slate-200 shadow-sm overflow-hidden">
|
|
184
|
+
<div class="overflow-x-auto">
|
|
185
|
+
<table class="w-full text-left border-collapse">
|
|
186
|
+
<thead>
|
|
187
|
+
<tr class="bg-slate-50 border-b border-slate-200 text-[11px] font-semibold uppercase tracking-wider text-slate-500">
|
|
188
|
+
<th class="px-5 py-3">Name</th>
|
|
189
|
+
<th class="px-5 py-3">Email</th>
|
|
190
|
+
<th class="px-5 py-3">Role</th>
|
|
191
|
+
<th class="px-5 py-3">Status</th>
|
|
192
|
+
<th class="px-5 py-3">Created</th>
|
|
193
|
+
<th class="px-5 py-3 text-right">Actions</th>
|
|
194
|
+
</tr>
|
|
195
|
+
</thead>
|
|
196
|
+
<tbody class="divide-y divide-slate-200">
|
|
197
|
+
<template x-for="u in users" :key="u.id">
|
|
198
|
+
<tr class="hover:bg-slate-50/70">
|
|
199
|
+
<td class="px-5 py-3.5">
|
|
200
|
+
<div class="flex items-center gap-2.5">
|
|
201
|
+
<span class="w-8 h-8 rounded-full bg-slate-900 text-white text-xs font-semibold flex items-center justify-center shrink-0" x-text="initials(u.name)"></span>
|
|
202
|
+
<span class="text-sm font-semibold text-slate-900" x-text="u.name"></span>
|
|
203
|
+
<span x-show="u.id === currentUserId" class="px-1.5 py-0.5 rounded bg-blue-50 text-blue-700 border border-blue-200 text-[10px] font-semibold">You</span>
|
|
204
|
+
</div>
|
|
205
|
+
</td>
|
|
206
|
+
<td class="px-5 py-3.5 text-sm text-slate-600" x-text="u.email"></td>
|
|
207
|
+
<td class="px-5 py-3.5">
|
|
208
|
+
<span
|
|
209
|
+
class="inline-flex px-2 py-0.5 rounded-full text-xs font-medium border uppercase"
|
|
210
|
+
:class="u.role === 'admin' ? 'bg-violet-50 text-violet-700 border-violet-200' : (u.role === 'editor' ? 'bg-blue-50 text-blue-700 border-blue-200' : 'bg-slate-100 text-slate-700 border-slate-200')"
|
|
211
|
+
x-text="u.role"
|
|
212
|
+
></span>
|
|
213
|
+
</td>
|
|
214
|
+
<td class="px-5 py-3.5">
|
|
215
|
+
<span
|
|
216
|
+
class="inline-flex px-2 py-0.5 rounded-full text-xs font-medium border"
|
|
217
|
+
:class="u.status === 'active' ? 'bg-emerald-50 text-emerald-700 border-emerald-200' : 'bg-red-50 text-red-700 border-red-200'"
|
|
218
|
+
x-text="u.status"
|
|
219
|
+
></span>
|
|
220
|
+
</td>
|
|
221
|
+
<td class="px-5 py-3.5 text-xs text-slate-500" x-text="formatDate(u.created_at)"></td>
|
|
222
|
+
<td class="px-5 py-3.5 text-right whitespace-nowrap">
|
|
223
|
+
<div class="inline-flex items-center gap-2">
|
|
224
|
+
<button
|
|
225
|
+
type="button"
|
|
226
|
+
@click="openDetails(u.id)"
|
|
227
|
+
class="px-2.5 py-1 rounded-md border border-slate-200 bg-slate-50 text-xs font-medium text-slate-700 hover:bg-slate-100"
|
|
228
|
+
>
|
|
229
|
+
Details
|
|
230
|
+
</button>
|
|
231
|
+
<button type="button" @click="openEdit(u)" class="text-xs font-medium text-blue-600 hover:underline">Edit</button>
|
|
232
|
+
<span x-show="u.id !== currentUserId" class="inline-flex items-center">
|
|
233
|
+
<button x-show="confirmDeleteId !== u.id" type="button" @click="confirmDeleteId = u.id" class="text-xs font-medium text-red-600 hover:underline">Delete</button>
|
|
234
|
+
<span x-show="confirmDeleteId === u.id" class="inline-flex items-center gap-1">
|
|
235
|
+
<button type="button" @click="deleteUser(u.id)" class="px-2 py-0.5 rounded bg-red-600 text-white text-xs font-medium">Confirm</button>
|
|
236
|
+
<button type="button" @click="confirmDeleteId = null" class="px-2 py-0.5 rounded bg-slate-100 text-slate-600 text-xs font-medium">Cancel</button>
|
|
237
|
+
</span>
|
|
238
|
+
</span>
|
|
239
|
+
</div>
|
|
240
|
+
</td>
|
|
241
|
+
</tr>
|
|
242
|
+
</template>
|
|
243
|
+
</tbody>
|
|
244
|
+
</table>
|
|
245
|
+
</div>
|
|
246
|
+
</div>
|
|
247
|
+
|
|
248
|
+
<!-- User Details Modal -->
|
|
249
|
+
<div x-show="detailsOpen" x-cloak class="fixed inset-0 z-50 flex items-center justify-center p-4 bg-slate-950/60">
|
|
250
|
+
<div @click.outside="detailsOpen = false" class="bg-white rounded-2xl shadow-2xl border border-slate-200 w-full max-w-lg p-6 space-y-5">
|
|
251
|
+
<div class="flex items-center justify-between border-b border-slate-100 pb-3">
|
|
252
|
+
<h3 class="text-base font-semibold text-slate-900">User Details</h3>
|
|
253
|
+
<button type="button" @click="detailsOpen = false" class="p-1 rounded-lg text-slate-500 hover:bg-slate-100">
|
|
254
|
+
<i data-lucide="x" class="w-4 h-4"></i>
|
|
255
|
+
</button>
|
|
256
|
+
</div>
|
|
257
|
+
|
|
258
|
+
<div x-show="detailsLoading" class="py-10 text-center text-sm text-slate-500">Loading user details...</div>
|
|
259
|
+
|
|
260
|
+
<div x-show="!detailsLoading && detailsData" class="space-y-5">
|
|
261
|
+
<div class="flex items-center gap-4">
|
|
262
|
+
<div class="w-14 h-14 rounded-full bg-slate-900 text-white text-lg font-bold flex items-center justify-center shrink-0" x-text="initials(detailsData?.user?.name)"></div>
|
|
263
|
+
<div class="min-w-0 flex-1">
|
|
264
|
+
<div class="flex items-center gap-2 flex-wrap">
|
|
265
|
+
<h4 class="text-base font-bold text-slate-900" x-text="detailsData?.user?.name"></h4>
|
|
266
|
+
<span class="px-2 py-0.5 rounded-full text-[11px] font-semibold uppercase bg-slate-100 text-slate-800" x-text="detailsData?.user?.role"></span>
|
|
267
|
+
<span
|
|
268
|
+
class="px-2 py-0.5 rounded-full text-[11px] font-medium border"
|
|
269
|
+
:class="detailsData?.user?.status === 'active' ? 'bg-emerald-50 text-emerald-700 border-emerald-200' : 'bg-red-50 text-red-700 border-red-200'"
|
|
270
|
+
x-text="detailsData?.user?.status"
|
|
271
|
+
></span>
|
|
272
|
+
</div>
|
|
273
|
+
<p class="text-sm text-slate-600 truncate" x-text="detailsData?.user?.email"></p>
|
|
274
|
+
<p class="text-xs text-slate-400 mt-0.5">
|
|
275
|
+
User #<span x-text="detailsData?.user?.id"></span> · Joined <span x-text="formatDate(detailsData?.user?.created_at)"></span>
|
|
276
|
+
</p>
|
|
277
|
+
</div>
|
|
278
|
+
</div>
|
|
279
|
+
|
|
280
|
+
<div class="grid grid-cols-4 gap-2.5 text-center">
|
|
281
|
+
<div class="rounded-xl bg-slate-50 border border-slate-200 p-3">
|
|
282
|
+
<p class="text-lg font-bold text-slate-900" x-text="detailsData?.stats?.postsCount ?? 0"></p>
|
|
283
|
+
<p class="text-[11px] text-slate-500">Total Posts</p>
|
|
284
|
+
</div>
|
|
285
|
+
<div class="rounded-xl bg-emerald-50/70 border border-emerald-200 p-3">
|
|
286
|
+
<p class="text-lg font-bold text-emerald-800" x-text="detailsData?.stats?.publishedCount ?? 0"></p>
|
|
287
|
+
<p class="text-[11px] text-emerald-700">Published</p>
|
|
288
|
+
</div>
|
|
289
|
+
<div class="rounded-xl bg-amber-50/70 border border-amber-200 p-3">
|
|
290
|
+
<p class="text-lg font-bold text-amber-800" x-text="detailsData?.stats?.draftCount ?? 0"></p>
|
|
291
|
+
<p class="text-[11px] text-amber-700">Drafts</p>
|
|
292
|
+
</div>
|
|
293
|
+
<div class="rounded-xl bg-blue-50/70 border border-blue-200 p-3">
|
|
294
|
+
<p class="text-lg font-bold text-blue-800" x-text="detailsData?.stats?.mediaCount ?? 0"></p>
|
|
295
|
+
<p class="text-[11px] text-blue-700">Media</p>
|
|
296
|
+
</div>
|
|
297
|
+
</div>
|
|
298
|
+
|
|
299
|
+
<div class="space-y-2">
|
|
300
|
+
<h5 class="text-xs font-semibold uppercase tracking-wide text-slate-500">Recent Authored Posts</h5>
|
|
301
|
+
<div x-show="!detailsData?.recentPosts?.length" class="text-xs text-slate-500 py-2">No posts authored yet.</div>
|
|
302
|
+
<ul x-show="detailsData?.recentPosts?.length" class="divide-y divide-slate-100 rounded-lg border border-slate-200 overflow-hidden">
|
|
303
|
+
<template x-for="p in (detailsData?.recentPosts || [])" :key="p.id">
|
|
304
|
+
<li class="px-3 py-2 flex items-center justify-between gap-2 text-xs bg-white hover:bg-slate-50">
|
|
305
|
+
<a :href="'/admin/posts/' + p.id + '/edit'" class="font-medium text-slate-800 hover:text-blue-600 truncate" x-text="p.title"></a>
|
|
306
|
+
<span class="px-2 py-0.5 rounded-full text-[10px] font-medium bg-slate-100 text-slate-700" x-text="p.status"></span>
|
|
307
|
+
</li>
|
|
308
|
+
</template>
|
|
309
|
+
</ul>
|
|
310
|
+
</div>
|
|
311
|
+
|
|
312
|
+
<div class="pt-2 border-t border-slate-100 flex items-center justify-end gap-2">
|
|
313
|
+
<button type="button" @click="detailsOpen = false" class="px-4 py-2 rounded-lg border border-slate-300 text-xs font-medium text-slate-700 hover:bg-slate-50">Close</button>
|
|
314
|
+
<button type="button" @click="openEdit(detailsData.user)" class="px-4 py-2 rounded-lg bg-slate-900 text-white text-xs font-medium hover:bg-slate-800">Edit User</button>
|
|
315
|
+
</div>
|
|
316
|
+
</div>
|
|
317
|
+
</div>
|
|
318
|
+
</div>
|
|
319
|
+
|
|
320
|
+
<!-- Create / Edit User Modal -->
|
|
321
|
+
<div x-show="modalOpen" x-cloak class="fixed inset-0 z-50 flex items-center justify-center p-4 bg-slate-950/60">
|
|
322
|
+
<div @click.outside="modalOpen = false" class="bg-white rounded-2xl shadow-2xl border border-slate-200 w-full max-w-md p-6 space-y-4">
|
|
323
|
+
<div class="flex items-center justify-between">
|
|
324
|
+
<h3 class="text-base font-semibold text-slate-900" x-text="editingId ? 'Edit User' : 'Create User'"></h3>
|
|
325
|
+
<button type="button" @click="modalOpen = false" class="p-1 rounded-lg text-slate-500 hover:bg-slate-100">
|
|
326
|
+
<i data-lucide="x" class="w-4 h-4"></i>
|
|
327
|
+
</button>
|
|
328
|
+
</div>
|
|
329
|
+
|
|
330
|
+
<div x-show="errors.length > 0" class="rounded-lg bg-red-50 border border-red-200 p-3 text-xs text-red-700 space-y-1">
|
|
331
|
+
<template x-for="(err, idx) in errors" :key="idx">
|
|
332
|
+
<p x-text="err"></p>
|
|
333
|
+
</template>
|
|
334
|
+
</div>
|
|
335
|
+
|
|
336
|
+
<form @submit.prevent="save()" class="space-y-4">
|
|
337
|
+
<div>
|
|
338
|
+
<label for="user-name" class="block text-xs font-medium text-slate-700 mb-1">Name</label>
|
|
339
|
+
<input id="user-name" type="text" required x-model="form.name" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none">
|
|
340
|
+
</div>
|
|
341
|
+
|
|
342
|
+
<div>
|
|
343
|
+
<label for="user-email" class="block text-xs font-medium text-slate-700 mb-1">Email</label>
|
|
344
|
+
<input id="user-email" type="email" required x-model="form.email" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none">
|
|
345
|
+
</div>
|
|
346
|
+
|
|
347
|
+
<div class="grid grid-cols-2 gap-3">
|
|
348
|
+
<div>
|
|
349
|
+
<label for="user-role" class="block text-xs font-medium text-slate-700 mb-1">Role</label>
|
|
350
|
+
<select id="user-role" x-model="form.role" :disabled="editingId === currentUserId" class="w-full rounded-lg border border-slate-300 bg-white px-3 py-2 text-sm disabled:bg-slate-100">
|
|
351
|
+
<option value="admin">admin</option>
|
|
352
|
+
<option value="editor">editor</option>
|
|
353
|
+
<option value="author">author</option>
|
|
354
|
+
</select>
|
|
355
|
+
</div>
|
|
356
|
+
<div>
|
|
357
|
+
<label for="user-status" class="block text-xs font-medium text-slate-700 mb-1">Status</label>
|
|
358
|
+
<select id="user-status" x-model="form.status" :disabled="editingId === currentUserId" class="w-full rounded-lg border border-slate-300 bg-white px-3 py-2 text-sm disabled:bg-slate-100">
|
|
359
|
+
<option value="active">active</option>
|
|
360
|
+
<option value="suspended">suspended</option>
|
|
361
|
+
</select>
|
|
362
|
+
</div>
|
|
363
|
+
</div>
|
|
364
|
+
|
|
365
|
+
<div>
|
|
366
|
+
<label for="user-password" class="block text-xs font-medium text-slate-700 mb-1" x-text="editingId ? 'Reset Password (optional)' : 'Password (min 6 chars)'"></label>
|
|
367
|
+
<input
|
|
368
|
+
id="user-password"
|
|
369
|
+
type="password"
|
|
370
|
+
:required="!editingId"
|
|
371
|
+
x-model="form.password"
|
|
372
|
+
placeholder="••••••••"
|
|
373
|
+
class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none"
|
|
374
|
+
>
|
|
375
|
+
</div>
|
|
376
|
+
|
|
377
|
+
<div class="pt-2 flex items-center justify-end gap-2">
|
|
378
|
+
<button type="button" @click="modalOpen = false" class="px-4 py-2 rounded-lg border border-slate-300 text-sm text-slate-700 hover:bg-slate-50">Cancel</button>
|
|
379
|
+
<button type="submit" :disabled="saving" class="px-4 py-2 rounded-lg bg-slate-900 text-white text-sm font-medium hover:bg-slate-800 disabled:opacity-50" x-text="saving ? 'Saving...' : 'Save User'"></button>
|
|
380
|
+
</div>
|
|
381
|
+
</form>
|
|
382
|
+
</div>
|
|
383
|
+
</div>
|
|
384
|
+
</div>
|
|
385
|
+
`;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Render the current user's Profile & Account Details page (/admin/profile).
|
|
390
|
+
*/
|
|
391
|
+
export function renderProfilePage(user: {
|
|
392
|
+
id: number;
|
|
393
|
+
name: string;
|
|
394
|
+
email: string;
|
|
395
|
+
role: string;
|
|
396
|
+
status: string;
|
|
397
|
+
}, stats: { postsCount: number; publishedCount: number; mediaCount: number }): string {
|
|
398
|
+
const seed = serializeSafeJson({ user, stats });
|
|
399
|
+
|
|
400
|
+
return `
|
|
401
|
+
<script id="profile-seed" type="application/json">${seed}</script>
|
|
402
|
+
<script>
|
|
403
|
+
document.addEventListener("alpine:init", () => {
|
|
404
|
+
Alpine.data("profileManager", () => ({
|
|
405
|
+
user: {},
|
|
406
|
+
stats: {},
|
|
407
|
+
form: { name: "", current_password: "", new_password: "" },
|
|
408
|
+
errors: [],
|
|
409
|
+
saving: false,
|
|
410
|
+
|
|
411
|
+
init() {
|
|
412
|
+
const el = document.getElementById("profile-seed");
|
|
413
|
+
const seed = el ? JSON.parse(el.textContent || "{}") : {};
|
|
414
|
+
this.user = seed.user || {};
|
|
415
|
+
this.stats = seed.stats || {};
|
|
416
|
+
this.form.name = this.user.name || "";
|
|
417
|
+
},
|
|
418
|
+
|
|
419
|
+
async save() {
|
|
420
|
+
this.saving = true;
|
|
421
|
+
this.errors = [];
|
|
422
|
+
try {
|
|
423
|
+
const res = await fetch("/api/auth/profile", {
|
|
424
|
+
method: "POST",
|
|
425
|
+
headers: { "content-type": "application/json" },
|
|
426
|
+
body: JSON.stringify(this.form)
|
|
427
|
+
});
|
|
428
|
+
const data = await res.json();
|
|
429
|
+
if (!res.ok || !data.success) {
|
|
430
|
+
this.errors = Array.isArray(data.errors) ? data.errors : ["Failed to update profile."];
|
|
431
|
+
window.cmsToast("error", this.errors[0]);
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
this.user = data.user;
|
|
435
|
+
this.form.current_password = "";
|
|
436
|
+
this.form.new_password = "";
|
|
437
|
+
window.cmsToast("success", "Account details updated.");
|
|
438
|
+
} catch {
|
|
439
|
+
this.errors = ["Network error while updating profile."];
|
|
440
|
+
} finally {
|
|
441
|
+
this.saving = false;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}));
|
|
445
|
+
});
|
|
446
|
+
</script>
|
|
447
|
+
|
|
448
|
+
<div x-data="profileManager" class="max-w-2xl space-y-6">
|
|
449
|
+
<div class="bg-white rounded-xl border border-slate-200 shadow-sm p-6 flex flex-wrap items-center justify-between gap-4">
|
|
450
|
+
<div>
|
|
451
|
+
<h2 class="text-lg font-bold text-slate-900" x-text="user.name"></h2>
|
|
452
|
+
<p class="text-sm text-slate-600" x-text="user.email"></p>
|
|
453
|
+
<div class="mt-2 flex items-center gap-2">
|
|
454
|
+
<span class="px-2 py-0.5 rounded-full text-xs font-semibold uppercase bg-slate-900 text-white" x-text="user.role"></span>
|
|
455
|
+
<span class="px-2 py-0.5 rounded-full text-xs font-medium bg-emerald-50 text-emerald-700 border border-emerald-200" x-text="user.status"></span>
|
|
456
|
+
</div>
|
|
457
|
+
</div>
|
|
458
|
+
<div class="flex items-center gap-3 text-center">
|
|
459
|
+
<div class="px-4 py-2 rounded-lg bg-slate-50 border border-slate-200">
|
|
460
|
+
<p class="text-base font-bold text-slate-900" x-text="stats.postsCount ?? 0"></p>
|
|
461
|
+
<p class="text-[11px] text-slate-500">Posts</p>
|
|
462
|
+
</div>
|
|
463
|
+
<div class="px-4 py-2 rounded-lg bg-slate-50 border border-slate-200">
|
|
464
|
+
<p class="text-base font-bold text-slate-900" x-text="stats.mediaCount ?? 0"></p>
|
|
465
|
+
<p class="text-[11px] text-slate-500">Uploads</p>
|
|
466
|
+
</div>
|
|
467
|
+
</div>
|
|
468
|
+
</div>
|
|
469
|
+
|
|
470
|
+
<form @submit.prevent="save()" class="bg-white rounded-xl border border-slate-200 shadow-sm p-6 space-y-4">
|
|
471
|
+
<h3 class="text-base font-semibold text-slate-900">Update Account Details</h3>
|
|
472
|
+
|
|
473
|
+
<div x-show="errors.length > 0" x-cloak class="rounded-lg bg-red-50 border border-red-200 p-3 text-xs text-red-700 space-y-1">
|
|
474
|
+
<template x-for="(err, idx) in errors" :key="idx">
|
|
475
|
+
<p x-text="err"></p>
|
|
476
|
+
</template>
|
|
477
|
+
</div>
|
|
478
|
+
|
|
479
|
+
<div>
|
|
480
|
+
<label for="prof-name" class="block text-xs font-semibold text-slate-700 mb-1">Display Name</label>
|
|
481
|
+
<input id="prof-name" type="text" required x-model="form.name" class="w-full rounded-lg border border-slate-300 px-3.5 py-2 text-sm focus:border-blue-500 focus:outline-none">
|
|
482
|
+
</div>
|
|
483
|
+
|
|
484
|
+
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
485
|
+
<div>
|
|
486
|
+
<label for="prof-cur-pw" class="block text-xs font-semibold text-slate-700 mb-1">Current Password</label>
|
|
487
|
+
<input id="prof-cur-pw" type="password" x-model="form.current_password" placeholder="Required to change password" class="w-full rounded-lg border border-slate-300 px-3.5 py-2 text-sm focus:border-blue-500 focus:outline-none">
|
|
488
|
+
</div>
|
|
489
|
+
<div>
|
|
490
|
+
<label for="prof-new-pw" class="block text-xs font-semibold text-slate-700 mb-1">New Password (min 6 chars)</label>
|
|
491
|
+
<input id="prof-new-pw" type="password" x-model="form.new_password" placeholder="Leave blank to keep current" class="w-full rounded-lg border border-slate-300 px-3.5 py-2 text-sm focus:border-blue-500 focus:outline-none">
|
|
492
|
+
</div>
|
|
493
|
+
</div>
|
|
494
|
+
|
|
495
|
+
<div class="pt-2 flex justify-end">
|
|
496
|
+
<button type="submit" :disabled="saving" class="px-4 py-2 rounded-lg bg-slate-900 text-white text-sm font-medium hover:bg-slate-800 disabled:opacity-50" x-text="saving ? 'Saving...' : 'Save Account Details'"></button>
|
|
497
|
+
</div>
|
|
498
|
+
</form>
|
|
499
|
+
</div>
|
|
500
|
+
`;
|
|
501
|
+
}
|