chore(ui): add module file and main stub

- chore(ui): add module file and main stub
- feat(ui): add layout, pages and partials
- feat(ui): add stylesheet and scripts
- feat(ui): add page renderer and static file handler
- test(ui): cover rendering, escaping and static serving
This commit is contained in:
bryanpedini committed 2026-11-19 15:46:12 +01:00
1 parent 7089a6f408
commit 5ac8f98f10
26 files changed
+3921

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 Bryan Joshua Pedini
import { $, api, busy, clear, clearErrors, formatTime, h, handleError, openDialog, setupDialogs, showFieldErrors, toast, userPath } from "./common.js";
const state = { users: [] };
function render() {
const body = $("#users-body");
clear(body);
for (const user of state.users) {
const deleted = Boolean(user.deleted_at);
body.append(h("tr", { class: deleted ? "row-muted" : null },
h("th", { scope: "row" }, h("a", { href: userPath(user.username), text: user.username })),
h("td", { text: user.role }),
h("td", { class: "num", text: user.zone_count }),
h("td", { class: "mono", text: formatTime(user.created_at) }),
h("td", { class: "mono", text: formatTime(user.last_login_at) }),
h("td", null, deleted ? h("span", { class: "badge badge-muted", text: "deleted " + formatTime(user.deleted_at) }) : h("span", { class: "badge badge-ok", text: "active" }))));
}
$("#users-status").textContent = state.users.length === 0 ? "No users." : state.users.length + (state.users.length === 1 ? " user." : " users.");
}
async function load() {
try {
const { data } = await api("GET", "/users", { query: { deleted: $("#show-deleted").checked ? "true" : "" } });
state.users = data.users || [];
render();
} catch (err) {
$("#users-status").textContent = "The users could not be loaded.";
handleError(err);
}
}
function setupCreate() {
const dialog = $("#user-dialog");
const form = $("#user-form");
$("#new-user").addEventListener("click", () => {
form.reset();
openDialog(dialog);
});
form.addEventListener("submit", async (event) => {
event.preventDefault();
clearErrors(form);
if (form.elements.password.value !== form.elements.repeat.value) {
showFieldErrors(form, { repeat: "The two passwords differ." });
return;
}
await busy(form.querySelector("button[type=submit]"), async () => {
try {
await api("POST", "/users", { body: { username: form.elements.username.value.trim(), role: form.elements.role.value, password: form.elements.password.value } });
} catch (err) {
handleError(err, { form });
return;
}
dialog.close();
form.reset();
toast("User created.");
await load();
});
});
}
setupDialogs();
setupCreate();
$("#show-deleted").addEventListener("change", load);
load();