feat(admin): create lists from a dialog
- Add new-list dialog with client validation and server error display - Reset the form each time the dialog opens
This commit is contained in:
parent
5f67289943
commit
bbb5f3f9ce
3 changed files with 186 additions and 1 deletions
82
admin/src/components/CreateListDialog.tsx
Normal file
82
admin/src/components/CreateListDialog.tsx
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { useState, type FormEvent } from 'react'
|
||||
import { toast } from 'sonner'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { api } from '@/lib/api'
|
||||
|
||||
export function CreateListDialog({ open, onOpenChange }: { open: boolean; onOpenChange: (open: boolean) => void }) {
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>New list</DialogTitle>
|
||||
<DialogDescription>The address will be name@domain.</DialogDescription>
|
||||
</DialogHeader>
|
||||
{/* The form lives inside the popup, so its state is dropped every time the dialog closes. */}
|
||||
<CreateListForm onDone={() => onOpenChange(false)} />
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
|
||||
function CreateListForm({ onDone }: { onDone: () => void }) {
|
||||
const queryClient = useQueryClient()
|
||||
const [name, setName] = useState('')
|
||||
const [domain, setDomain] = useState('')
|
||||
const [title, setTitle] = useState('')
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
const create = useMutation({
|
||||
mutationFn: api.lists.create,
|
||||
onSuccess: async (list) => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['lists'] })
|
||||
toast.success(`Created ${list.address}`)
|
||||
onDone()
|
||||
},
|
||||
onError: (e: Error) => setError(e.message),
|
||||
})
|
||||
|
||||
function submit(e: FormEvent) {
|
||||
e.preventDefault()
|
||||
const n = name.trim()
|
||||
const d = domain.trim()
|
||||
if (!n || !d) {
|
||||
setError('Name and domain are required')
|
||||
return
|
||||
}
|
||||
if (/[@+\s]/.test(n) || /[@+\s]/.test(d)) {
|
||||
setError("Name and domain must not contain '@', '+' or spaces")
|
||||
return
|
||||
}
|
||||
setError(null)
|
||||
create.mutate({ name: n, domain: d, title: title.trim() || undefined })
|
||||
}
|
||||
|
||||
return (
|
||||
<form onSubmit={submit} className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="list-name">Name</Label>
|
||||
<Input id="list-name" value={name} onChange={(e) => setName(e.target.value)} placeholder="dev" autoComplete="off" />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="list-domain">Domain</Label>
|
||||
<Input id="list-domain" value={domain} onChange={(e) => setDomain(e.target.value)} placeholder="example.org" autoComplete="off" />
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="list-title">Title</Label>
|
||||
<Input id="list-title" value={title} onChange={(e) => setTitle(e.target.value)} placeholder="Optional" autoComplete="off" />
|
||||
</div>
|
||||
{error && (
|
||||
<p role="alert" className="text-sm text-destructive">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
<Button type="submit" className="w-full" disabled={create.isPending}>
|
||||
Create list
|
||||
</Button>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
95
admin/src/pages/ListsPage.test.tsx
Normal file
95
admin/src/pages/ListsPage.test.tsx
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
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'
|
||||
|
||||
describe('lists overview', () => {
|
||||
it('shows every list with its policies and links to the detail page', async () => {
|
||||
installFakeApi(
|
||||
newState({
|
||||
lists: [
|
||||
aList({ id: 1, name: 'dev', title: 'Developers', post_policy: 'moderated', archive_policy: 'public' }),
|
||||
aList({ id: 2, name: 'news', title: 'News', join_policy: 'closed' }),
|
||||
],
|
||||
}),
|
||||
)
|
||||
renderApp('/', { token: 'good' })
|
||||
const dev = await screen.findByRole('link', { name: 'dev@example.org' })
|
||||
expect(dev).toHaveAttribute('href', '/lists/1')
|
||||
expect(screen.getByRole('link', { name: 'news@example.org' })).toHaveAttribute('href', '/lists/2')
|
||||
const row = dev.closest('tr')!
|
||||
expect(within(row).getByText('Developers')).toBeInTheDocument()
|
||||
expect(within(row).getByText('moderated')).toBeInTheDocument()
|
||||
expect(within(row).getByText('public')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('has an empty state', async () => {
|
||||
installFakeApi(newState())
|
||||
renderApp('/', { token: 'good' })
|
||||
expect(await screen.findByText(/no lists yet/i)).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('shows a loading error from the server', async () => {
|
||||
installFakeApi(newState({ fail: { 'GET /lists': { status: 500, error: 'database is locked' } } }))
|
||||
renderApp('/', { token: 'good' })
|
||||
expect(await screen.findByRole('alert')).toHaveTextContent('database is locked')
|
||||
})
|
||||
})
|
||||
|
||||
describe('creating a list', () => {
|
||||
async function openDialog() {
|
||||
const state = installFakeApi(newState({ lists: [aList()] }))
|
||||
const ctx = renderApp('/', { token: 'good' })
|
||||
await screen.findByText('dev@example.org')
|
||||
await ctx.user.click(screen.getByRole('button', { name: /new list/i }))
|
||||
const dialog = await screen.findByRole('dialog')
|
||||
return { ...ctx, state, dialog }
|
||||
}
|
||||
|
||||
it('creates a list and shows it in the table', async () => {
|
||||
const { user, state, dialog } = await openDialog()
|
||||
await user.type(within(dialog).getByLabelText(/^name$/i), 'ops')
|
||||
await user.type(within(dialog).getByLabelText(/domain/i), 'example.org')
|
||||
await user.type(within(dialog).getByLabelText(/title/i), 'Operations')
|
||||
await user.click(within(dialog).getByRole('button', { name: /create list/i }))
|
||||
|
||||
expect(await screen.findByRole('link', { name: 'ops@example.org' })).toBeInTheDocument()
|
||||
expect(screen.getByText('Operations')).toBeInTheDocument()
|
||||
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
|
||||
expect(state.calls).toContain('POST /lists')
|
||||
})
|
||||
|
||||
it('needs a name and a domain before calling the API', async () => {
|
||||
const { user, state, dialog } = await openDialog()
|
||||
await user.click(within(dialog).getByRole('button', { name: /create list/i }))
|
||||
expect(await within(dialog).findByText(/name and domain are required/i)).toBeInTheDocument()
|
||||
expect(state.calls).not.toContain('POST /lists')
|
||||
})
|
||||
|
||||
it('rejects names with @ or + before calling the API', async () => {
|
||||
const { user, state, dialog } = await openDialog()
|
||||
await user.type(within(dialog).getByLabelText(/^name$/i), 'a+b')
|
||||
await user.type(within(dialog).getByLabelText(/domain/i), 'example.org')
|
||||
await user.click(within(dialog).getByRole('button', { name: /create list/i }))
|
||||
expect(await within(dialog).findByText(/must not contain/i)).toBeInTheDocument()
|
||||
expect(state.calls).not.toContain('POST /lists')
|
||||
})
|
||||
|
||||
it('shows a duplicate error and keeps the dialog open', async () => {
|
||||
const { user, dialog } = await openDialog()
|
||||
await user.type(within(dialog).getByLabelText(/^name$/i), 'dev')
|
||||
await user.type(within(dialog).getByLabelText(/domain/i), 'example.org')
|
||||
await user.click(within(dialog).getByRole('button', { name: /create list/i }))
|
||||
expect(await within(dialog).findByRole('alert')).toHaveTextContent('already exists')
|
||||
expect(screen.getByRole('dialog')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('starts empty each time it opens', async () => {
|
||||
const { user, dialog } = await openDialog()
|
||||
await user.type(within(dialog).getByLabelText(/^name$/i), 'leftover')
|
||||
await user.keyboard('{Escape}')
|
||||
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
|
||||
await user.click(screen.getByRole('button', { name: /new list/i }))
|
||||
expect(await within(await screen.findByRole('dialog')).findByLabelText(/^name$/i)).toHaveValue('')
|
||||
})
|
||||
})
|
||||
|
|
@ -1,14 +1,22 @@
|
|||
import { useQuery } from '@tanstack/react-query'
|
||||
import { useState } from 'react'
|
||||
import { Link } from 'react-router'
|
||||
import { CreateListDialog } from '@/components/CreateListDialog'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'
|
||||
import { api } from '@/lib/api'
|
||||
|
||||
export function ListsPage() {
|
||||
const lists = useQuery({ queryKey: ['lists'], queryFn: api.lists.list })
|
||||
const [creating, setCreating] = useState(false)
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<h1 className="text-2xl font-semibold">Lists</h1>
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-2xl font-semibold">Lists</h1>
|
||||
<Button onClick={() => setCreating(true)}>New list</Button>
|
||||
</div>
|
||||
<CreateListDialog open={creating} onOpenChange={setCreating} />
|
||||
{lists.isPending && <p className="text-muted-foreground">Loading…</p>}
|
||||
{lists.isError && <p role="alert" className="text-destructive">{lists.error.message}</p>}
|
||||
{lists.data && lists.data.length === 0 && <p className="text-muted-foreground">No lists yet.</p>}
|
||||
|
|
|
|||
Loading…
Reference in a new issue