From bbb5f3f9ceb6b0e04f123662c0f0e6ab62e79be9 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 9 Nov 2026 15:32:39 +0100 Subject: [PATCH] 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 --- admin/src/components/CreateListDialog.tsx | 82 +++++++++++++++++++ admin/src/pages/ListsPage.test.tsx | 95 +++++++++++++++++++++++ admin/src/pages/ListsPage.tsx | 10 ++- 3 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 admin/src/components/CreateListDialog.tsx create mode 100644 admin/src/pages/ListsPage.test.tsx diff --git a/admin/src/components/CreateListDialog.tsx b/admin/src/components/CreateListDialog.tsx new file mode 100644 index 0000000..4cfb539 --- /dev/null +++ b/admin/src/components/CreateListDialog.tsx @@ -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 ( + + + + New list + The address will be name@domain. + + {/* The form lives inside the popup, so its state is dropped every time the dialog closes. */} + onOpenChange(false)} /> + + + ) +} + +function CreateListForm({ onDone }: { onDone: () => void }) { + const queryClient = useQueryClient() + const [name, setName] = useState('') + const [domain, setDomain] = useState('') + const [title, setTitle] = useState('') + const [error, setError] = useState(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 ( +
+
+ + setName(e.target.value)} placeholder="dev" autoComplete="off" /> +
+
+ + setDomain(e.target.value)} placeholder="example.org" autoComplete="off" /> +
+
+ + setTitle(e.target.value)} placeholder="Optional" autoComplete="off" /> +
+ {error && ( +

+ {error} +

+ )} + +
+ ) +} diff --git a/admin/src/pages/ListsPage.test.tsx b/admin/src/pages/ListsPage.test.tsx new file mode 100644 index 0000000..0e8be5c --- /dev/null +++ b/admin/src/pages/ListsPage.test.tsx @@ -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('') + }) +}) diff --git a/admin/src/pages/ListsPage.tsx b/admin/src/pages/ListsPage.tsx index 6666331..228dd00 100644 --- a/admin/src/pages/ListsPage.tsx +++ b/admin/src/pages/ListsPage.tsx @@ -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 (
-

Lists

+
+

Lists

+ +
+ {lists.isPending &&

Loading…

} {lists.isError &&

{lists.error.message}

} {lists.data && lists.data.length === 0 &&

No lists yet.

}