feat(admin): list page with tabs, settings and deletion
- Add list header, tab navigation driven by the URL and held-queue badge - Add settings form that sends only changed fields - Add confirmed list deletion with error handling in the dialog - Add labelled select field wrapper for shadcn Select
This commit is contained in:
parent
bbb5f3f9ce
commit
f9e5b142c2
8 changed files with 439 additions and 0 deletions
|
|
@ -1,8 +1,13 @@
|
|||
import { Route, Routes } from 'react-router'
|
||||
import { Shell } from '@/components/Shell'
|
||||
import { ListPage } from '@/pages/ListPage'
|
||||
import { ListsPage } from '@/pages/ListsPage'
|
||||
import { LoginPage } from '@/pages/LoginPage'
|
||||
import { NotFoundPage } from '@/pages/NotFoundPage'
|
||||
import { ArchiveTab } from '@/tabs/ArchiveTab'
|
||||
import { HeldTab } from '@/tabs/HeldTab'
|
||||
import { MembersTab } from '@/tabs/MembersTab'
|
||||
import { SettingsTab } from '@/tabs/SettingsTab'
|
||||
import { Toaster } from '@/components/ui/sonner'
|
||||
|
||||
/** Routes only: providers (query client, router, auth) are set up by the caller. */
|
||||
|
|
@ -13,6 +18,12 @@ export function App() {
|
|||
<Route path="/login" element={<LoginPage />} />
|
||||
<Route element={<Shell />}>
|
||||
<Route index element={<ListsPage />} />
|
||||
<Route path="lists/:id" element={<ListPage />}>
|
||||
<Route index element={<SettingsTab />} />
|
||||
<Route path="members" element={<MembersTab />} />
|
||||
<Route path="held" element={<HeldTab />} />
|
||||
<Route path="archive" element={<ArchiveTab />} />
|
||||
</Route>
|
||||
<Route path="*" element={<NotFoundPage />} />
|
||||
</Route>
|
||||
</Routes>
|
||||
|
|
|
|||
40
admin/src/components/SelectField.tsx
Normal file
40
admin/src/components/SelectField.tsx
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { Label } from '@/components/ui/label'
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
||||
|
||||
export interface Option<T extends string> {
|
||||
value: T
|
||||
label: string
|
||||
}
|
||||
|
||||
/** A labelled select. The label is also the accessible name of the control. */
|
||||
export function SelectField<T extends string>({
|
||||
id,
|
||||
label,
|
||||
value,
|
||||
options,
|
||||
onChange,
|
||||
}: {
|
||||
id: string
|
||||
label: string
|
||||
value: T
|
||||
options: Option<T>[]
|
||||
onChange: (value: T) => void
|
||||
}) {
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor={id}>{label}</Label>
|
||||
<Select value={value} onValueChange={(v) => v && onChange(v as T)} items={options}>
|
||||
<SelectTrigger id={id} className="w-full">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{options.map((o) => (
|
||||
<SelectItem key={o.value} value={o.value}>
|
||||
{o.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
161
admin/src/pages/ListPage.test.tsx
Normal file
161
admin/src/pages/ListPage.test.tsx
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
import { screen, waitFor, within } from '@testing-library/react'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { aList, installFakeApi, newState } from '@/test/fakeApi'
|
||||
import { renderApp } from '@/test/render'
|
||||
|
||||
const dev = () => aList({ id: 1, name: 'dev', title: 'Developers' })
|
||||
|
||||
describe('list page', () => {
|
||||
it('shows the list header and the tabs', async () => {
|
||||
installFakeApi(newState({ lists: [dev()] }))
|
||||
renderApp('/lists/1', { token: 'good' })
|
||||
expect(await screen.findByRole('heading', { name: 'dev@example.org' })).toBeInTheDocument()
|
||||
expect(screen.getByText('Developers')).toBeInTheDocument()
|
||||
for (const name of ['Settings', 'Members', 'Held', 'Archive']) {
|
||||
expect(screen.getByRole('tab', { name: new RegExp(`^${name}`) })).toBeInTheDocument()
|
||||
}
|
||||
expect(screen.getByRole('link', { name: /all lists/i })).toHaveAttribute('href', '/')
|
||||
})
|
||||
|
||||
it('says so when the list does not exist', async () => {
|
||||
installFakeApi(newState())
|
||||
renderApp('/lists/42', { token: 'good' })
|
||||
expect(await screen.findByText(/list not found/i)).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('treats a non-numeric id as not found without calling the API', async () => {
|
||||
const state = installFakeApi(newState())
|
||||
renderApp('/lists/abc', { token: 'good' })
|
||||
expect(await screen.findByText(/list not found/i)).toBeInTheDocument()
|
||||
expect(state.calls).toEqual([])
|
||||
})
|
||||
|
||||
it('moves between tabs', async () => {
|
||||
installFakeApi(newState({ lists: [dev()] }))
|
||||
const { user } = renderApp('/lists/1', { token: 'good' })
|
||||
await user.click(await screen.findByRole('tab', { name: /^members/i }))
|
||||
expect(await screen.findByRole('heading', { name: /members/i, level: 2 })).toBeInTheDocument()
|
||||
expect(screen.getByRole('tab', { name: /^members/i })).toHaveAttribute('aria-selected', 'true')
|
||||
await user.click(screen.getByRole('tab', { name: /^settings/i }))
|
||||
expect(await screen.findByLabelText(/^title$/i)).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('opens the tab named in the URL', async () => {
|
||||
installFakeApi(newState({ lists: [dev()] }))
|
||||
renderApp('/lists/1/archive', { token: 'good' })
|
||||
expect(await screen.findByRole('tab', { name: /^archive/i })).toHaveAttribute('aria-selected', 'true')
|
||||
})
|
||||
|
||||
it('shows how many messages wait in the held queue', async () => {
|
||||
installFakeApi(
|
||||
newState({
|
||||
lists: [dev()],
|
||||
held: { 1: [{ id: 5, sender: 'a@x.com', subject: 's', size: 10, created_at: '2030-01-01T00:00:00Z' }] },
|
||||
}),
|
||||
)
|
||||
renderApp('/lists/1', { token: 'good' })
|
||||
expect(await screen.findByRole('tab', { name: /^held\s*1$/i })).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
describe('settings tab', () => {
|
||||
async function setup(list = dev()) {
|
||||
const state = installFakeApi(newState({ lists: [list] }))
|
||||
const ctx = renderApp('/lists/1', { token: 'good' })
|
||||
await screen.findByLabelText(/^title$/i)
|
||||
return { ...ctx, state }
|
||||
}
|
||||
|
||||
it('shows the current settings', async () => {
|
||||
await setup(aList({ id: 1, title: 'Developers', post_policy: 'moderated', join_policy: 'closed', archive_policy: 'public' }))
|
||||
expect(screen.getByLabelText(/^title$/i)).toHaveValue('Developers')
|
||||
expect(screen.getByRole('combobox', { name: /posting/i })).toHaveTextContent(/moderated/i)
|
||||
expect(screen.getByRole('combobox', { name: /joining/i })).toHaveTextContent(/closed/i)
|
||||
expect(screen.getByRole('combobox', { name: /archive/i })).toHaveTextContent(/public/i)
|
||||
})
|
||||
|
||||
it('cannot be saved until something changes', async () => {
|
||||
await setup()
|
||||
expect(screen.getByRole('button', { name: /save changes/i })).toBeDisabled()
|
||||
})
|
||||
|
||||
it('saves only the fields that changed', async () => {
|
||||
const { user, state } = await setup()
|
||||
await user.clear(screen.getByLabelText(/^title$/i))
|
||||
await user.type(screen.getByLabelText(/^title$/i), 'Core developers')
|
||||
await user.click(screen.getByRole('combobox', { name: /posting/i }))
|
||||
await user.click(await screen.findByRole('option', { name: /moderated/i }))
|
||||
await user.click(screen.getByRole('button', { name: /save changes/i }))
|
||||
|
||||
await waitFor(() => expect(state.lists[0].title).toBe('Core developers'))
|
||||
expect(state.lists[0].post_policy).toBe('moderated')
|
||||
expect(state.lists[0].join_policy).toBe('open')
|
||||
expect(state.calls).toContain('PATCH /lists/1')
|
||||
expect(await screen.findByText(/settings saved/i)).toBeInTheDocument()
|
||||
await waitFor(() => expect(screen.getByRole('button', { name: /save changes/i })).toBeDisabled())
|
||||
})
|
||||
|
||||
it('changes the archive policy', async () => {
|
||||
const { user, state } = await setup()
|
||||
await user.click(screen.getByRole('combobox', { name: /archive/i }))
|
||||
await user.click(await screen.findByRole('option', { name: /public/i }))
|
||||
await user.click(screen.getByRole('button', { name: /save changes/i }))
|
||||
await waitFor(() => expect(state.lists[0].archive_policy).toBe('public'))
|
||||
expect(state.lists[0].title).toBe('Developers')
|
||||
})
|
||||
|
||||
it('refuses an empty title', async () => {
|
||||
const { user, state } = await setup()
|
||||
await user.clear(screen.getByLabelText(/^title$/i))
|
||||
await user.click(screen.getByRole('button', { name: /save changes/i }))
|
||||
expect(await screen.findByText(/title is required/i)).toBeInTheDocument()
|
||||
expect(state.calls).not.toContain('PATCH /lists/1')
|
||||
})
|
||||
|
||||
it('shows a server error and keeps the edits', async () => {
|
||||
const { user, state } = await setup()
|
||||
state.fail['PATCH /lists/1'] = { status: 500, error: 'disk full' }
|
||||
await user.type(screen.getByLabelText(/^title$/i), '!')
|
||||
await user.click(screen.getByRole('button', { name: /save changes/i }))
|
||||
expect(await screen.findByRole('alert')).toHaveTextContent('disk full')
|
||||
expect(screen.getByLabelText(/^title$/i)).toHaveValue('Developers!')
|
||||
})
|
||||
})
|
||||
|
||||
describe('deleting a list', () => {
|
||||
async function setup() {
|
||||
const state = installFakeApi(newState({ lists: [dev(), aList({ id: 2, name: 'news' })] }))
|
||||
const ctx = renderApp('/lists/1', { token: 'good' })
|
||||
await ctx.user.click(await screen.findByRole('button', { name: /delete list/i }))
|
||||
const dialog = await screen.findByRole('alertdialog')
|
||||
return { ...ctx, state, dialog }
|
||||
}
|
||||
|
||||
it('asks for confirmation naming the list, and cancelling deletes nothing', async () => {
|
||||
const { user, state, dialog } = await setup()
|
||||
expect(dialog).toHaveTextContent('dev@example.org')
|
||||
await user.click(within(dialog).getByRole('button', { name: /cancel/i }))
|
||||
await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument())
|
||||
expect(state.calls).not.toContain('DELETE /lists/1')
|
||||
expect(state.lists).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('deletes the list and goes back to the overview', async () => {
|
||||
const { user, state, dialog } = await setup()
|
||||
await user.click(within(dialog).getByRole('button', { name: /^delete$/i }))
|
||||
expect(await screen.findByRole('heading', { name: /^lists$/i })).toBeInTheDocument()
|
||||
expect(state.lists.map((l) => l.id)).toEqual([2])
|
||||
expect(screen.queryByRole('link', { name: 'dev@example.org' })).not.toBeInTheDocument()
|
||||
expect(screen.getByRole('link', { name: 'news@example.org' })).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('shows a server error and stays on the page', async () => {
|
||||
const { user, state, dialog } = await setup()
|
||||
state.fail['DELETE /lists/1'] = { status: 500, error: 'locked' }
|
||||
await user.click(within(dialog).getByRole('button', { name: /^delete$/i }))
|
||||
expect(await within(dialog).findByRole('alert')).toHaveTextContent('locked')
|
||||
expect(screen.getByRole('alertdialog')).toBeInTheDocument() // still open: the user can retry
|
||||
expect(screen.getByRole('heading', { name: 'dev@example.org', hidden: true })).toBeInTheDocument()
|
||||
expect(state.lists).toHaveLength(2)
|
||||
})
|
||||
})
|
||||
61
admin/src/pages/ListPage.tsx
Normal file
61
admin/src/pages/ListPage.tsx
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import { useQuery } from '@tanstack/react-query'
|
||||
import { Link, Outlet, useLocation, useNavigate, useParams } from 'react-router'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'
|
||||
import { api } from '@/lib/api'
|
||||
|
||||
const tabs = ['settings', 'members', 'held', 'archive'] as const
|
||||
|
||||
export function ListPage() {
|
||||
const params = useParams()
|
||||
const id = /^\d+$/.test(params.id ?? '') ? Number(params.id) : null
|
||||
const navigate = useNavigate()
|
||||
const { pathname } = useLocation()
|
||||
|
||||
const list = useQuery({ queryKey: ['list', id], queryFn: () => api.lists.get(id!), enabled: id !== null })
|
||||
const held = useQuery({ queryKey: ['held', id], queryFn: () => api.held.list(id!), enabled: id !== null })
|
||||
|
||||
if (id === null || (list.isError && list.error.message === 'not found')) {
|
||||
return <NotFound />
|
||||
}
|
||||
if (list.isPending) return <p className="text-muted-foreground">Loading…</p>
|
||||
if (list.isError) return <p role="alert" className="text-destructive">{list.error.message}</p>
|
||||
|
||||
const current = tabs.find((t) => pathname.endsWith(`/${t}`)) ?? 'settings'
|
||||
const heldCount = held.data?.length ?? 0
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<Link to="/" className="text-sm text-primary underline">
|
||||
All lists
|
||||
</Link>
|
||||
<h1 className="mt-2 text-2xl font-semibold">{list.data.address}</h1>
|
||||
<p className="text-muted-foreground">{list.data.title}</p>
|
||||
</div>
|
||||
<Tabs value={current} onValueChange={(t) => navigate(t === 'settings' ? `/lists/${id}` : `/lists/${id}/${t}`)}>
|
||||
<TabsList>
|
||||
<TabsTrigger value="settings">Settings</TabsTrigger>
|
||||
<TabsTrigger value="members">Members</TabsTrigger>
|
||||
<TabsTrigger value="held">
|
||||
Held
|
||||
{heldCount > 0 && <Badge className="ml-1">{heldCount}</Badge>}
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="archive">Archive</TabsTrigger>
|
||||
</TabsList>
|
||||
</Tabs>
|
||||
<Outlet context={list.data} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function NotFound() {
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<h1 className="text-2xl font-semibold">List not found</h1>
|
||||
<Link to="/" className="text-primary underline">
|
||||
Back to lists
|
||||
</Link>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
3
admin/src/tabs/ArchiveTab.tsx
Normal file
3
admin/src/tabs/ArchiveTab.tsx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export function ArchiveTab() {
|
||||
return <h2 className="text-lg font-semibold">Archive</h2>
|
||||
}
|
||||
3
admin/src/tabs/HeldTab.tsx
Normal file
3
admin/src/tabs/HeldTab.tsx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export function HeldTab() {
|
||||
return <h2 className="text-lg font-semibold">Held</h2>
|
||||
}
|
||||
3
admin/src/tabs/MembersTab.tsx
Normal file
3
admin/src/tabs/MembersTab.tsx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export function MembersTab() {
|
||||
return <h2 className="text-lg font-semibold">Members</h2>
|
||||
}
|
||||
157
admin/src/tabs/SettingsTab.tsx
Normal file
157
admin/src/tabs/SettingsTab.tsx
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { useState, type FormEvent } from 'react'
|
||||
import { useNavigate, useOutletContext } from 'react-router'
|
||||
import { toast } from 'sonner'
|
||||
import { SelectField, type Option } from '@/components/SelectField'
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@/components/ui/alert-dialog'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Separator } from '@/components/ui/separator'
|
||||
import { api, type ArchivePolicy, type JoinPolicy, type List, type PostPolicy } from '@/lib/api'
|
||||
|
||||
const postOptions: Option<PostPolicy>[] = [
|
||||
{ value: 'members', label: 'Members only' },
|
||||
{ value: 'moderated', label: 'Moderated (hold every post)' },
|
||||
]
|
||||
const joinOptions: Option<JoinPolicy>[] = [
|
||||
{ value: 'open', label: 'Open (confirmed by email)' },
|
||||
{ value: 'closed', label: 'Closed (admin adds members)' },
|
||||
]
|
||||
const archiveOptions: Option<ArchivePolicy>[] = [
|
||||
{ value: 'private', label: 'Private (API only)' },
|
||||
{ value: 'public', label: 'Public (on the web)' },
|
||||
{ value: 'off', label: 'Off (not stored)' },
|
||||
]
|
||||
|
||||
export function SettingsTab() {
|
||||
const list = useOutletContext<List>()
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
<SettingsForm list={list} />
|
||||
<Separator />
|
||||
<DangerZone list={list} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function SettingsForm({ list }: { list: List }) {
|
||||
const queryClient = useQueryClient()
|
||||
const [title, setTitle] = useState(list.title)
|
||||
const [post, setPost] = useState(list.post_policy)
|
||||
const [join, setJoin] = useState(list.join_policy)
|
||||
const [archive, setArchive] = useState(list.archive_policy)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
// Send only what changed.
|
||||
const patch: Parameters<typeof api.lists.update>[1] = {}
|
||||
if (title.trim() !== list.title) patch.title = title.trim()
|
||||
if (post !== list.post_policy) patch.post_policy = post
|
||||
if (join !== list.join_policy) patch.join_policy = join
|
||||
if (archive !== list.archive_policy) patch.archive_policy = archive
|
||||
const dirty = Object.keys(patch).length > 0
|
||||
|
||||
const save = useMutation({
|
||||
mutationFn: () => api.lists.update(list.id, patch),
|
||||
onSuccess: async (updated) => {
|
||||
queryClient.setQueryData(['list', list.id], updated)
|
||||
await queryClient.invalidateQueries({ queryKey: ['lists'] })
|
||||
toast.success('Settings saved')
|
||||
setError(null)
|
||||
},
|
||||
onError: (e: Error) => setError(e.message),
|
||||
})
|
||||
|
||||
function submit(e: FormEvent) {
|
||||
e.preventDefault()
|
||||
if (title.trim() === '') {
|
||||
setError('Title is required')
|
||||
return
|
||||
}
|
||||
setError(null)
|
||||
save.mutate()
|
||||
}
|
||||
|
||||
return (
|
||||
<form onSubmit={submit} className="max-w-md space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="title">Title</Label>
|
||||
<Input id="title" value={title} onChange={(e) => setTitle(e.target.value)} />
|
||||
</div>
|
||||
<SelectField id="post-policy" label="Posting" value={post} options={postOptions} onChange={setPost} />
|
||||
<SelectField id="join-policy" label="Joining" value={join} options={joinOptions} onChange={setJoin} />
|
||||
<SelectField id="archive-policy" label="Archive" value={archive} options={archiveOptions} onChange={setArchive} />
|
||||
{error && (
|
||||
<p role="alert" className="text-sm text-destructive">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
<Button type="submit" disabled={!dirty || save.isPending}>
|
||||
Save changes
|
||||
</Button>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
|
||||
function DangerZone({ list }: { list: List }) {
|
||||
const queryClient = useQueryClient()
|
||||
const navigate = useNavigate()
|
||||
const [open, setOpen] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
const remove = useMutation({
|
||||
mutationFn: () => api.lists.remove(list.id),
|
||||
onSuccess: async () => {
|
||||
queryClient.removeQueries({ queryKey: ['list', list.id] })
|
||||
await queryClient.invalidateQueries({ queryKey: ['lists'] })
|
||||
toast.success(`Deleted ${list.address}`)
|
||||
navigate('/')
|
||||
},
|
||||
onError: (e: Error) => setError(e.message),
|
||||
})
|
||||
|
||||
return (
|
||||
<section className="max-w-md space-y-3">
|
||||
<h2 className="text-lg font-semibold text-destructive">Danger zone</h2>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Deleting a list also deletes its members, held messages and archive. This cannot be undone.
|
||||
</p>
|
||||
<Button
|
||||
variant="destructive"
|
||||
onClick={() => {
|
||||
setError(null)
|
||||
setOpen(true)
|
||||
}}
|
||||
>
|
||||
Delete list
|
||||
</Button>
|
||||
<AlertDialog open={open} onOpenChange={setOpen}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Delete {list.address}?</AlertDialogTitle>
|
||||
<AlertDialogDescription>All members, held messages and archived messages are lost.</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
{error && (
|
||||
<p role="alert" className="text-sm text-destructive">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<Button variant="destructive" disabled={remove.isPending} onClick={() => remove.mutate()}>
|
||||
Delete
|
||||
</Button>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
Loading…
Reference in a new issue