- 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
158 lines
5.5 KiB
JavaScript
158 lines
5.5 KiB
JavaScript
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
import { $, api, appData, bindPasswordForm, busy, clear, confirmDialog, flash, formatTime, h, handleError, icon, option, setupDialogs, toast, userPath, zoneLink, zonePath } from "./common.js";
|
|
|
|
const { targetUser, username: self } = appData();
|
|
const base = userPath(targetUser);
|
|
const isSelf = targetUser === self;
|
|
const state = { user: null };
|
|
|
|
function summaryRow(dl, term, value) {
|
|
dl.append(h("dt", { text: term }), h("dd", { text: value }));
|
|
}
|
|
|
|
function renderSummary(user) {
|
|
const dl = $("#user-summary");
|
|
clear(dl);
|
|
summaryRow(dl, "Role", user.role);
|
|
summaryRow(dl, "Created (UTC)", formatTime(user.created_at));
|
|
summaryRow(dl, "Last login (UTC)", formatTime(user.last_login_at));
|
|
summaryRow(dl, "State", user.deleted_at ? "deleted " + formatTime(user.deleted_at) : "active");
|
|
$("#user-status").textContent = "";
|
|
const badge = $("#user-state");
|
|
badge.hidden = !user.deleted_at;
|
|
badge.textContent = "deleted";
|
|
}
|
|
|
|
function zoneItem(entry, removable) {
|
|
const item = h("li", null, entry.exists ? zoneLink(entry.zone) : h("span", { class: "mono", text: entry.zone }));
|
|
if (!entry.exists) item.append(" ", h("span", { class: "badge badge-warning", text: "zone not found" }));
|
|
if (removable) {
|
|
item.append(" ", h("button", { type: "button", class: "btn btn-small btn-danger", "aria-label": "Remove zone " + entry.zone, onclick: () => removeZone(entry.zone) }, icon("trash"), " Remove"));
|
|
}
|
|
return item;
|
|
}
|
|
|
|
async function renderZones(user) {
|
|
const readonly = Boolean(user.deleted_at);
|
|
const zones = user.zones || [];
|
|
if (readonly) {
|
|
const list = $("#user-zones-readonly");
|
|
clear(list);
|
|
for (const entry of zones) list.append(zoneItem(entry, false));
|
|
return;
|
|
}
|
|
const list = $("#user-zones");
|
|
clear(list);
|
|
for (const entry of zones) list.append(zoneItem(entry, true));
|
|
$("#user-zones-status").textContent = zones.length === 0 ? "No zone is delegated to this user." : "";
|
|
const form = $("#zone-add-form");
|
|
const pick = $("#user-zone-pick");
|
|
clear(pick);
|
|
if (user.role === "admin") {
|
|
form.hidden = true;
|
|
$("#user-zones-status").textContent = zones.length === 0 ? "Administrators see every zone and need no delegation." : "Administrators need no delegation; these delegations apply again if the user is demoted.";
|
|
return;
|
|
}
|
|
form.hidden = false;
|
|
try {
|
|
const { data } = await api("GET", "/zones");
|
|
const have = new Set(zones.map((entry) => entry.zone));
|
|
const candidates = (data.zones || []).filter((zone) => !have.has(zone.name));
|
|
for (const zone of candidates) pick.append(option(zone.name, zone.name, false));
|
|
if (candidates.length === 0) pick.append(option("", "No zone available", true));
|
|
pick.disabled = candidates.length === 0;
|
|
form.querySelector("button").disabled = candidates.length === 0;
|
|
} catch (err) {
|
|
handleError(err);
|
|
}
|
|
}
|
|
|
|
async function load() {
|
|
try {
|
|
const { data } = await api("GET", base);
|
|
state.user = data;
|
|
} catch (err) {
|
|
$("#user-status").textContent = "The user could not be loaded.";
|
|
handleError(err);
|
|
return;
|
|
}
|
|
const user = state.user;
|
|
renderSummary(user);
|
|
if (isSelf) return;
|
|
const deleted = Boolean(user.deleted_at);
|
|
$("#user-controls").hidden = deleted;
|
|
$("#user-readonly").hidden = !deleted;
|
|
$("#user-role").value = user.role;
|
|
await renderZones(user);
|
|
}
|
|
|
|
async function removeZone(zone) {
|
|
const ok = await confirmDialog({ title: "Remove zone", message: "Remove the delegation of " + zone + " from " + targetUser + "?", confirm: "Remove" });
|
|
if (!ok) return;
|
|
try {
|
|
await api("DELETE", zonePath(zone) + "/users/" + encodeURIComponent(targetUser));
|
|
toast("Delegation removed.");
|
|
await load();
|
|
} catch (err) {
|
|
handleError(err, { reload: load });
|
|
}
|
|
}
|
|
|
|
function setup() {
|
|
$("#role-form").addEventListener("submit", async (event) => {
|
|
event.preventDefault();
|
|
await busy(event.target.querySelector("button"), async () => {
|
|
try {
|
|
await api("PUT", base + "/role", { body: { role: $("#user-role").value } });
|
|
toast("Role saved.");
|
|
await load();
|
|
} catch (err) {
|
|
handleError(err);
|
|
await load();
|
|
}
|
|
});
|
|
});
|
|
bindPasswordForm($("#password-form"), async (form) => {
|
|
await api("PUT", base + "/password", { body: { password: form.elements.password.value } });
|
|
toast("Password set. The sessions of " + targetUser + " ended.");
|
|
});
|
|
$("#zone-add-form").addEventListener("submit", async (event) => {
|
|
event.preventDefault();
|
|
const zone = $("#user-zone-pick").value;
|
|
if (!zone) return;
|
|
await busy(event.target.querySelector("button"), async () => {
|
|
try {
|
|
await api("PUT", zonePath(zone) + "/users/" + encodeURIComponent(targetUser));
|
|
toast("Zone delegated.");
|
|
await load();
|
|
} catch (err) {
|
|
handleError(err);
|
|
}
|
|
});
|
|
});
|
|
const confirmInput = $("#user-delete-confirm");
|
|
const deleteButton = $("#user-delete");
|
|
confirmInput.addEventListener("input", () => {
|
|
deleteButton.disabled = confirmInput.value.trim() !== targetUser;
|
|
});
|
|
$("#user-delete-form").addEventListener("submit", async (event) => {
|
|
event.preventDefault();
|
|
if (deleteButton.disabled) return;
|
|
await busy(deleteButton, async () => {
|
|
try {
|
|
await api("DELETE", base, { body: { confirm: confirmInput.value.trim() } });
|
|
flash("User " + targetUser + " deleted.");
|
|
location.assign("/users");
|
|
} catch (err) {
|
|
handleError(err);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
setupDialogs();
|
|
if (!isSelf) setup();
|
|
load();
|