Polish the SPA: focus rings, reduced motion, forms that report errors
Test and deploy / test (push) Successful in 1m10s
Test and deploy / deploy (push) Successful in 58s

- Global :focus-visible ring (text fields get a border instead) and a
  prefers-reduced-motion override; the navbar marquee stops for it too.
- Button: disabled state and horizontal padding.
- useCreateForm returns error and submitting, so the create forms show a
  refused save inline and can't double-submit; they, Login, CreatePlace and
  CreateUser are real <form>s so Enter submits from any field.
- Admin: section list wraps on phones, Manage Users is a table with errors,
  Manage Radio confirms before deleting, page delete errors show inline.
- Hard-coded Tailwind colours replaced with tokens; 404 image alt text and
  no forced scrollbars.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-23 20:11:05 +01:00
co-authored by Claude Opus 5.5
parent 3fa456a063
commit 8c535b1cc2
19 changed files with 271 additions and 128 deletions
+38 -2
View File
@@ -6,9 +6,11 @@
* every token is BOTH a utility class in templates
* (text-primary, bg-surface, font-heading) AND a CSS variable
* in component <style> blocks (var(--color-primary)).
* 3. Base default look of plain HTML elements (the dark theme)
* 3. Base default look of plain HTML elements (the dark theme),
* including the keyboard focus ring
* 4. Components reusable classes: gif borders, A4 sheet, halftone background
* 5. Transitions <Transition name="slide"> and <Transition name="fade">
* 5. Transitions <Transition name="slide"> and <Transition name="fade">,
* and the reduced-motion override
* 6. Print
*
* The CV pages are a light theme: src/layouts/CVLayout.vue re-points the colour
@@ -150,6 +152,26 @@ textarea::placeholder {
opacity: 0.5;
}
/* Keyboard focus. Nothing on the site had a visible focus ring, so tabbing
through it was blind. Text fields get the border treatment the login form
had; everything else an outline. The selectors are wrapped in :where() to
keep their specificity just above the bare `input` rule above, so any
component with its own focus or outline style (BoxFrame, the hotels and
automata forms) still wins. Not in an @layer: unlayered rules such as
`input` above would then beat it whatever its specificity. */
:where(*):focus-visible {
outline: 2px solid var(--color-tertiary);
outline-offset: 2px;
}
:where(
input:not([type="checkbox"], [type="radio"], [type="range"], [type="file"]),
textarea
):focus-visible {
outline: none;
border-color: var(--color-tertiary);
box-shadow: 0 0 0 2px var(--color-quaternary);
}
table {
color: var(--color-primary);
border: 1px solid var(--color-primary);
@@ -255,6 +277,20 @@ th {
opacity: 0;
}
/* Visitors who ask the OS for less motion get instant route changes and
crossfades. The JS animation loops check the same query themselves
(Headline.vue for the navbar marquee). */
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
/* ------------------------------------------------------------------ */
/* 6. Print */
/* ------------------------------------------------------------------ */
+3 -1
View File
@@ -1,12 +1,14 @@
<!--
The site's standard button. Wraps a native <button>, so all attributes and
listeners (including @click) fall through to it via Vue's attribute inheritance.
A disabled button is dimmed and stops reacting to hover and press, so a form
that is busy saving looks it.
-->
<script setup></script>
<template>
<button
class="text-primary bg-link-bg text-center border cursor-pointer transition-colors duration-150 ease-in-out hover:bg-surface active:scale-95"
class="px-2 text-primary bg-link-bg text-center border cursor-pointer transition-colors duration-150 ease-in-out hover:bg-surface active:scale-95 disabled:cursor-not-allowed disabled:opacity-50 disabled:hover:bg-link-bg disabled:active:scale-100"
>
<slot />
</button>
+5
View File
@@ -51,6 +51,11 @@ function animate() {
let resizeObserver;
onMounted(() => {
// A visitor who asked the OS for less motion gets the text standing still.
// The CSS reduced-motion override in styles.css can't reach this: the
// movement is a transform written every frame, not a transition.
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
measureWidth();
rafId = requestAnimationFrame(animate);
+20 -4
View File
@@ -1,8 +1,11 @@
// The shared body of the admin "create X" forms (CreatePost, CreateFavorite,
// CreateActivity, CreateBookmark): a ref per field, a GraphQL mutation taking a
// single `input`, clear the fields on success, then emit "done" so the host
// modal can close. Failures are logged and leave the fields alone, exactly as
// the hand-written versions did.
// modal can close. A failure leaves the fields alone and its message on
// `error` for the form to show — it used to go only to the console, so a
// refused save looked exactly like a button that did nothing. `submitting`
// disables the button while the request is out, and a second submit in that
// window is ignored, so a double click cannot create the row twice.
import { ref } from "vue";
import { gql } from "@/graphql";
@@ -15,7 +18,12 @@ import { gql } from "@/graphql";
* @param {string[]} [options.optional] fields sent as undefined when blank,
* so the backend sees "not set" rather than an empty string
* @param {string} [options.resultKey] mutation field to log on success
* @returns {{ values: Record<string, import("vue").Ref<string>>, submit: () => Promise<void> }}
* @returns {{
* values: Record<string, import("vue").Ref<string>>,
* submit: () => Promise<void>,
* error: import("vue").Ref<string>,
* submitting: import("vue").Ref<boolean>,
* }}
*/
export function useCreateForm({
mutation,
@@ -25,8 +33,13 @@ export function useCreateForm({
resultKey,
}) {
const values = Object.fromEntries(fields.map((name) => [name, ref("")]));
const error = ref("");
const submitting = ref(false);
async function submit() {
if (submitting.value) return;
submitting.value = true;
error.value = "";
try {
const input = {};
for (const name of fields) {
@@ -39,8 +52,11 @@ export function useCreateForm({
emit("done");
} catch (err) {
console.error(err);
error.value = err?.message || "Failed to save";
} finally {
submitting.value = false;
}
}
return { values, submit };
return { values, submit, error, submitting };
}
+3 -3
View File
@@ -4,11 +4,11 @@
<template>
<main class="flex flex-col items-center">
<div
class="page-a4 items-center bdr-1 flex flex-col relative overflow-scroll"
class="page-a4 items-center bdr-1 flex flex-col relative overflow-auto"
>
<h1>404</h1>
<RouterLink to="/" class="bdr-2">
<img src="/img/memes/epic.jpeg" loading="lazy" />
<RouterLink to="/" class="bdr-2" aria-label="Back to the home page">
<img src="/img/memes/epic.jpeg" alt="" class="block max-w-full" />
</RouterLink>
<h1>Click her, she will take you home</h1>
</div>
+15 -2
View File
@@ -85,12 +85,14 @@ async function handleLogout() {
</header>
<div class="admin-body">
<nav class="admin-nav">
<nav class="admin-nav" aria-label="Admin sections">
<button
v-for="section in sections"
:key="section.id"
type="button"
class="admin-nav-link"
:class="{ 'is-active': active === section.id }"
:aria-current="active === section.id ? 'page' : undefined"
@click="selectSection(section.id)"
>
{{ section.label }}
@@ -114,7 +116,9 @@ async function handleLogout() {
@done="onDone"
@cancel="onCancel"
/>
<small v-if="notice" class="admin-notice">{{ notice }}</small>
<small v-if="notice" class="admin-notice" role="status">
{{ notice }}
</small>
</section>
</div>
</div>
@@ -195,9 +199,18 @@ async function handleLogout() {
color: var(--color-secondary);
}
/* On a phone the section list would be eleven full-width rows above the
form; as wrapping chips it takes three or four lines. */
@media (max-width: 700px) {
.admin-body {
grid-template-columns: 1fr;
}
.admin-nav {
flex-direction: row;
flex-wrap: wrap;
}
.admin-nav-link {
padding: 4px 8px;
}
}
</style>
+11 -8
View File
@@ -8,7 +8,7 @@ import { useCreateForm } from "@/js/useCreateForm";
const emit = defineEmits(["done", "cancel"]);
const { values, submit: post } = useCreateForm({
const { values, submit: post, error, submitting } = useCreateForm({
mutation: `mutation CreateActivity($input: CreateActivityInput!) { createActivity(input: $input) { id } }`,
fields: ["type", "name", "link"],
optional: ["link"],
@@ -19,12 +19,15 @@ const { type, name, link } = values;
</script>
<template>
<div class="flex flex-col">
<form class="flex flex-col" @submit.prevent="post">
<h1>Create Activity</h1>
<input type="text" v-model="type" placeholder="Type" @keyup.enter="post" />
<input type="text" v-model="name" placeholder="Name" @keyup.enter="post" />
<input type="text" v-model="link" placeholder="Link" @keyup.enter="post" />
<Button @click="post">Upload</Button>
<Button @click="emit('cancel')">Cancel</Button>
</div>
<input type="text" v-model="type" placeholder="Type" />
<input type="text" v-model="name" placeholder="Name" />
<input type="text" v-model="link" placeholder="Link" />
<small v-if="error" class="p-1" role="alert">{{ error }}</small>
<Button type="submit" :disabled="submitting">
{{ submitting ? "Uploading…" : "Upload" }}
</Button>
<Button type="button" @click="emit('cancel')">Cancel</Button>
</form>
</template>
+10 -17
View File
@@ -8,7 +8,7 @@ import { useCreateForm } from "@/js/useCreateForm";
const emit = defineEmits(["done", "cancel"]);
const { values, submit } = useCreateForm({
const { values, submit, error, submitting } = useCreateForm({
mutation: `mutation CreateBookmark($input: CreateBookmarkInput!) { createBookmark(input: $input) { id } }`,
fields: ["category", "name", "link"],
emit,
@@ -17,22 +17,15 @@ const { category, name, link } = values;
</script>
<template>
<div class="flex flex-col">
<form class="flex flex-col" @submit.prevent="submit">
<h1>Create Bookmark</h1>
<input type="text" v-model="category" placeholder="Category" />
<input
type="text"
v-model="name"
placeholder="Name"
@keyup.enter="submit"
/>
<input
type="text"
v-model="link"
placeholder="Link"
@keyup.enter="submit"
/>
<Button @click="submit">Upload</Button>
<Button @click="emit('cancel')">Cancel</Button>
</div>
<input type="text" v-model="name" placeholder="Name" />
<input type="text" v-model="link" placeholder="Link" />
<small v-if="error" class="p-1" role="alert">{{ error }}</small>
<Button type="submit" :disabled="submitting">
{{ submitting ? "Uploading…" : "Upload" }}
</Button>
<Button type="button" @click="emit('cancel')">Cancel</Button>
</form>
</template>
+11 -8
View File
@@ -8,7 +8,7 @@ import { useCreateForm } from "@/js/useCreateForm";
const emit = defineEmits(["done", "cancel"]);
const { values, submit: post } = useCreateForm({
const { values, submit: post, error, submitting } = useCreateForm({
mutation: `mutation CreateFavorite($input: CreateFavoriteInput!) { createFavorite(input: $input) { id } }`,
fields: ["type", "name", "link"],
optional: ["link"],
@@ -19,12 +19,15 @@ const { type, name, link } = values;
</script>
<template>
<div class="flex flex-col">
<form class="flex flex-col" @submit.prevent="post">
<h1>Create Favorite</h1>
<input type="text" v-model="type" placeholder="Type" @keyup.enter="post" />
<input type="text" v-model="name" placeholder="Name" @keyup.enter="post" />
<input type="text" v-model="link" placeholder="Link" @keyup.enter="post" />
<Button @click="post">Upload</Button>
<Button @click="emit('cancel')">Cancel</Button>
</div>
<input type="text" v-model="type" placeholder="Type" />
<input type="text" v-model="name" placeholder="Name" />
<input type="text" v-model="link" placeholder="Link" />
<small v-if="error" class="p-1" role="alert">{{ error }}</small>
<Button type="submit" :disabled="submitting">
{{ submitting ? "Uploading…" : "Upload" }}
</Button>
<Button type="button" @click="emit('cancel')">Cancel</Button>
</form>
</template>
+16 -31
View File
@@ -19,13 +19,16 @@ const imageUrl = ref("");
const notes = ref("");
const done = ref(false);
const error = ref("");
const submitting = ref(false);
async function submit() {
if (submitting.value) return;
if (!title.value.trim()) {
error.value = "Title is required";
return;
}
error.value = "";
submitting.value = true;
try {
await places.create({
title: title.value.trim(),
@@ -48,56 +51,38 @@ async function submit() {
emit("done");
} catch (err) {
error.value = err?.message || "Failed to save";
} finally {
submitting.value = false;
}
}
</script>
<template>
<div class="flex flex-col">
<form class="flex flex-col" @submit.prevent="submit">
<h1>Add Place / Thing To Do</h1>
<input
type="text"
v-model="title"
placeholder="Title (required)"
@keyup.enter="submit"
/>
<input
type="text"
v-model="location"
placeholder="Location"
@keyup.enter="submit"
/>
<input type="text" v-model="title" placeholder="Title (required)" />
<input type="text" v-model="location" placeholder="Location" />
<input
type="text"
v-model="category"
placeholder="Category (place, activity, food…)"
@keyup.enter="submit"
/>
<input
type="text"
v-model="cost"
placeholder="Cost (e.g. £20 pp)"
@keyup.enter="submit"
/>
<input type="text" v-model="cost" placeholder="Cost (e.g. £20 pp)" />
<input
type="number"
v-model="priority"
placeholder="Priority (higher = sooner)"
@keyup.enter="submit"
/>
<input
type="text"
v-model="imageUrl"
placeholder="Image URL"
@keyup.enter="submit"
/>
<input type="text" v-model="imageUrl" placeholder="Image URL" />
<textarea class="h-25" v-model="notes" placeholder="Notes"></textarea>
<label class="flex flex-row items-center gap-2 p-2 text-primary">
<input type="checkbox" v-model="done" class="w-auto" />
Already done
</label>
<small v-if="error" class="p-1">{{ error }}</small>
<Button @click="submit">Upload</Button>
<Button @click="emit('cancel')">Cancel</Button>
</div>
<small v-if="error" class="p-1" role="alert">{{ error }}</small>
<Button type="submit" :disabled="submitting">
{{ submitting ? "Uploading…" : "Upload" }}
</Button>
<Button type="button" @click="emit('cancel')">Cancel</Button>
</form>
</template>
+9 -11
View File
@@ -10,7 +10,7 @@ import { useCreateForm } from "@/js/useCreateForm";
const emit = defineEmits(["done", "cancel"]);
const { values, submit: post } = useCreateForm({
const { values, submit: post, error, submitting } = useCreateForm({
mutation: `mutation CreatePost($input: CreatePostInput!) { createPost(input: $input) { id } }`,
fields: ["title", "content"],
resultKey: "createPost",
@@ -20,16 +20,14 @@ const { title, content } = values;
</script>
<template>
<div class="flex flex-col">
<form class="flex flex-col" @submit.prevent="post">
<h1>Create Post</h1>
<input
type="text"
v-model="title"
placeholder="Title"
@keyup.enter="post"
/>
<input type="text" v-model="title" placeholder="Title" />
<textarea class="h-50" v-model="content" placeholder="Content"></textarea>
<Button @click="post">Upload</Button>
<Button @click="emit('cancel')">Cancel</Button>
</div>
<small v-if="error" class="p-1" role="alert">{{ error }}</small>
<Button type="submit" :disabled="submitting">
{{ submitting ? "Uploading…" : "Upload" }}
</Button>
<Button type="button" @click="emit('cancel')">Cancel</Button>
</form>
</template>
+8 -3
View File
@@ -16,6 +16,7 @@ const emit = defineEmits(["done", "cancel"]);
const images = ref([]);
const results = ref([]);
const uploading = ref(false);
function onFileChange(e) {
images.value = Array.from(e.target.files);
@@ -23,7 +24,8 @@ function onFileChange(e) {
}
async function submit() {
if (!images.value.length) return;
if (!images.value.length || uploading.value) return;
uploading.value = true;
results.value = images.value.map((f) => ({
name: f.name,
status: "Uploading...",
@@ -54,6 +56,7 @@ async function submit() {
);
images.value = [];
uploading.value = false;
// Only a clean run counts as done: hosts close or confirm on "done", which
// would hide or contradict the per-file failure rows above.
if (results.value.every((r) => r.ok)) emit("done");
@@ -69,11 +72,13 @@ async function submit() {
multiple
@change="onFileChange"
/>
<Button @click="submit">Upload</Button>
<Button @click="submit" :disabled="uploading || !images.length">
{{ uploading ? "Reading…" : "Upload" }}
</Button>
<Button @click="emit('cancel')">Cancel</Button>
<div v-for="r in results" :key="r.name">
<span class="text-primary">{{ r.name }}: </span>
<span :class="r.ok ? 'text-secondary' : 'text-red-500'">{{
<span :class="r.ok ? 'text-secondary' : 'text-tertiary'">{{
r.status
}}</span>
</div>
+18 -7
View File
@@ -12,10 +12,13 @@ const username = ref("");
const password = ref("");
const message = ref("");
const error = ref("");
const submitting = ref(false);
async function handleCreate() {
if (submitting.value) return;
message.value = "";
error.value = "";
submitting.value = true;
try {
const data = await gql(
`mutation CreateUser($input: CreateUserInput!) { createUser(input: $input) { id username } }`,
@@ -26,29 +29,37 @@ async function handleCreate() {
password.value = "";
} catch (err) {
error.value = err.message || "Failed to create user.";
} finally {
submitting.value = false;
}
}
</script>
<template>
<div v-if="auth.loggedIn && auth.user.admin" class="flex flex-col">
<form
v-if="auth.loggedIn && auth.user.admin"
class="flex flex-col"
@submit.prevent="handleCreate"
>
<h1>Create User</h1>
<p v-if="message" class="text-green-500">{{ message }}</p>
<p v-if="error" class="text-red-500">{{ error }}</p>
<p v-if="message" class="text-secondary" role="status">{{ message }}</p>
<p v-if="error" class="text-tertiary" role="alert">{{ error }}</p>
<input
type="text"
v-model="username"
placeholder="Username"
@keyup.enter="handleCreate"
autocomplete="off"
/>
<input
type="password"
v-model="password"
placeholder="Password"
@keyup.enter="handleCreate"
autocomplete="new-password"
/>
<Button @click="handleCreate">Create Account</Button>
</div>
<Button type="submit" :disabled="submitting">
{{ submitting ? "Creating…" : "Create Account" }}
</Button>
</form>
<div v-else-if="auth.loggedIn" class="flex flex-col">
<p>You do not have permission to create users.</p>
</div>
+10 -6
View File
@@ -86,7 +86,9 @@ function handleLogout() {
</div>
</div>
<div v-else class="login-card bdr-1">
<!-- A real <form>, so Enter submits from either field and password
managers recognise it as a sign-in. -->
<form v-else class="login-card bdr-1" @submit.prevent="handleLogin">
<h1>Login</h1>
<small class="login-hint">
Admin access is required beyond this point.
@@ -99,7 +101,7 @@ function handleLogout() {
v-model="username"
autocomplete="username"
placeholder="Username"
@keyup.enter="handleLogin"
required
/>
</label>
@@ -110,16 +112,18 @@ function handleLogout() {
v-model="password"
autocomplete="current-password"
placeholder="Password"
@keyup.enter="handleLogin"
required
/>
</label>
<small v-if="errorMsg" class="login-error">{{ errorMsg }}</small>
<small v-if="errorMsg" class="login-error" role="alert">
{{ errorMsg }}
</small>
<Button @click="handleLogin" :disabled="submitting">
<Button type="submit" class="mt-1 py-2" :disabled="submitting">
{{ submitting ? "Signing in…" : "Log In" }}
</Button>
</div>
</form>
</main>
</template>
+6 -1
View File
@@ -17,6 +17,7 @@ const pages = usePagesStore();
pages.fetch();
const removeTarget = ref(null);
const confirmationOpen = ref(false);
const removeError = ref("");
async function remove(page) {
removeTarget.value = page;
@@ -27,10 +28,11 @@ async function confirmRemove() {
const page = removeTarget.value;
removeTarget.value = null;
if (!page) return;
removeError.value = "";
try {
await pages.remove(page.id);
} catch (err) {
alert(err?.message || "Failed to delete");
removeError.value = err?.message || "Failed to delete";
}
}
</script>
@@ -43,6 +45,9 @@ async function confirmRemove() {
The home page (/stp) and the landing page's notes are canvases too —
edit those on the pages themselves.
</p>
<p v-if="removeError" class="text-tertiary" role="alert">
{{ removeError }}
</p>
<p v-if="pages.error" class="text-tertiary">Couldn't load the pages.</p>
<table v-else-if="pages.pages.length" class="w-full">
<tr v-for="page in pages.pages" :key="page.id">
+52 -10
View File
@@ -2,8 +2,11 @@
/**
* Admin panel: list and upload tracks for the Icecast radio. REST
* (multipart/form-data) rather than GraphQL because it moves files.
* Deleting removes the file from the radio's library for good, so it asks
* first; a failed list, enable/disable or delete is shown above the list.
*/
import Button from "@/components/input/Button.vue";
import ConfirmDialog from "@/components/util/ConfirmDialog.vue";
import { ref, onMounted } from "vue";
import { api } from "@/api";
@@ -11,13 +14,19 @@ const songs = ref([]);
const files = ref([]);
const results = ref([]);
const loading = ref(false);
const error = ref("");
const deleteTarget = ref(null);
const confirmOpen = ref(false);
async function fetchSongs() {
try {
const res = await api.get("/radio/songs");
const res = await api.get("/radio/songs", {
fallback: "Couldn't load the songs",
});
songs.value = res.songs;
} catch (err) {
console.error(err);
error.value = err.message;
}
}
@@ -57,22 +66,40 @@ async function upload() {
await fetchSongs();
}
async function deleteSong(name) {
function askDelete(song) {
deleteTarget.value = song;
confirmOpen.value = true;
}
async function deleteSong() {
const song = deleteTarget.value;
deleteTarget.value = null;
if (!song) return;
error.value = "";
try {
await api.delete(`/radio/songs/${encodeURIComponent(name)}`);
await api.delete(`/radio/songs/${encodeURIComponent(song.name)}`, {
fallback: "Delete failed",
});
await fetchSongs();
} catch (err) {
console.error(err);
error.value = err.message;
}
}
async function toggleSong(song) {
const action = song.disabled ? "enable" : "disable";
error.value = "";
try {
await api.patch(`/radio/songs/${encodeURIComponent(song.name)}/${action}`);
await api.patch(
`/radio/songs/${encodeURIComponent(song.name)}/${action}`,
undefined,
{ fallback: `Couldn't ${action} the song` },
);
await fetchSongs();
} catch (err) {
console.error(err);
error.value = err.message;
}
}
@@ -94,26 +121,41 @@ onMounted(fetchSongs);
multiple
@change="onFileChange"
/>
<Button @click="upload" :disabled="loading">Upload</Button>
<Button @click="upload" :disabled="loading || !files.length">
{{ loading ? "Uploading…" : "Upload" }}
</Button>
<p v-if="error" class="text-tertiary" role="alert">{{ error }}</p>
<div v-for="r in results" :key="r.name">
<span class="text-primary">{{ r.name }}: </span>
<span :class="r.ok ? 'text-secondary' : 'text-red-500'">{{
<span :class="r.ok ? 'text-secondary' : 'text-tertiary'">{{
r.status
}}</span>
</div>
<div
v-for="song in songs"
:key="song.name"
class="flex flex-row items-center gap-2"
class="flex flex-row flex-wrap items-center gap-2"
:class="{ 'opacity-50': song.disabled }"
>
<span :class="{ 'line-through': song.disabled }">{{ song.name }}</span>
<span class="text-primary" :class="{ 'line-through': song.disabled }">
{{ song.name }}
</span>
<span class="text-secondary text-sm">{{ formatSize(song.size) }}</span>
<span v-if="song.disabled" class="text-red-400 text-xs">disabled</span>
<span v-if="song.disabled" class="text-tertiary text-xs">disabled</span>
<Button @click="toggleSong(song)">{{
song.disabled ? "Enable" : "Disable"
}}</Button>
<Button @click="deleteSong(song.name)">Delete</Button>
<Button @click="askDelete(song)">Delete</Button>
</div>
<ConfirmDialog
v-if="deleteTarget"
v-model="confirmOpen"
title="Delete song"
:message="`Delete “${deleteTarget.name}” from the radio? The file is removed for good.`"
confirm-label="Delete"
@confirm="deleteSong"
@cancel="deleteTarget = null"
/>
</div>
</template>
+33 -13
View File
@@ -2,7 +2,8 @@
/**
* Admin panel: list users and toggle their admin flag. Mutates the local `user`
* object in place from the mutation's response, so the row updates without a
* refetch.
* refetch. A failed load or toggle is shown above the table rather than only
* logged, and a row's button is disabled while its toggle is in flight.
*/
import Button from "@/components/input/Button.vue";
import { ref, onMounted } from "vue";
@@ -11,6 +12,9 @@ import { gql } from "@/graphql";
const auth = useAuthStore();
const users = ref([]);
const loaded = ref(false);
const error = ref("");
const pending = ref(null);
async function fetchUsers() {
try {
@@ -18,15 +22,23 @@ async function fetchUsers() {
users.value = data.users;
} catch (err) {
console.error(err);
error.value = err.message || "Couldn't load the users";
} finally {
loaded.value = true;
}
}
async function toggleAdmin(user) {
error.value = "";
pending.value = user.id;
try {
const data = await auth.setUserAdmin(user.id, !user.admin);
user.admin = data.admin;
} catch (err) {
console.error(err);
error.value = err.message || `Couldn't change ${user.username}`;
} finally {
pending.value = null;
}
}
@@ -34,18 +46,26 @@ onMounted(fetchUsers);
</script>
<template>
<div class="flex flex-col">
<div class="flex flex-col gap-2">
<h1>Manage Users</h1>
<div
v-for="user in users"
:key="user.id"
class="flex flex-row items-center gap-2"
>
<span>{{ user.username }}</span>
<span v-if="user.admin">(admin)</span>
<Button v-if="user.id !== auth.user.id" @click="toggleAdmin(user)">
{{ user.admin ? "Demote" : "Promote" }}
</Button>
</div>
<p v-if="error" class="text-tertiary" role="alert">{{ error }}</p>
<p v-if="!loaded" class="text-muted">Loading…</p>
<table v-else-if="users.length" class="w-full">
<tr v-for="user in users" :key="user.id">
<td class="py-1">{{ user.username }}</td>
<td class="py-1">{{ user.admin ? "admin" : "user" }}</td>
<td class="py-1 text-right">
<span v-if="user.id === auth.user.id" class="text-muted">you</span>
<Button
v-else
:disabled="pending === user.id"
@click="toggleAdmin(user)"
>
{{ user.admin ? "Demote" : "Promote" }}
</Button>
</td>
</tr>
</table>
<p v-else-if="!error" class="text-muted">No users.</p>
</div>
</template>
+1 -1
View File
@@ -42,7 +42,7 @@ function formatHours(minutes) {
Steam
<span
class="inline-block w-2 h-2 rounded-full"
:class="steamStatus.online ? 'bg-green-500' : 'bg-gray-400'"
:class="steamStatus.online ? 'bg-secondary' : 'bg-muted'"
:title="steamStatus.online ? 'Online' : 'Offline'"
/>
</span>