From 75a32386b2b31765c18431b4e6f83f7caea5bec4 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 9 Nov 2026 15:35:43 +0100 Subject: [PATCH] feat(admin): members tab - Add member table with status, bounce count and client-side filter - Add add-member form, re-enable for disabled members and confirmed removal --- admin/src/tabs/MembersTab.test.tsx | 133 +++++++++++++++++++ admin/src/tabs/MembersTab.tsx | 201 ++++++++++++++++++++++++++++- 2 files changed, 333 insertions(+), 1 deletion(-) create mode 100644 admin/src/tabs/MembersTab.test.tsx diff --git a/admin/src/tabs/MembersTab.test.tsx b/admin/src/tabs/MembersTab.test.tsx new file mode 100644 index 0000000..baf5cb8 --- /dev/null +++ b/admin/src/tabs/MembersTab.test.tsx @@ -0,0 +1,133 @@ +import { screen, waitFor, within } from '@testing-library/react' +import { describe, expect, it } from 'vitest' +import type { Member } from '@/lib/api' +import { aList, installFakeApi, newState } from '@/test/fakeApi' +import { renderApp } from '@/test/render' + +const member = (address: string, over: Partial = {}): Member => ({ + address, + display_name: '', + disabled: false, + bounce_count: 0, + ...over, +}) + +async function setup(members: Member[] = []) { + const state = installFakeApi(newState({ lists: [aList({ id: 1 })], members: { 1: members } })) + const ctx = renderApp('/lists/1/members', { token: 'good' }) + await screen.findByRole('heading', { name: /^members/i, level: 2 }) + return { ...ctx, state } +} + +describe('members tab', () => { + it('lists members with their name and status', async () => { + await setup([ + member('alice@example.com', { display_name: 'Alice' }), + member('bob@example.com', { disabled: true, bounce_count: 3 }), + member('carol@example.com', { bounce_count: 1 }), + ]) + const alice = (await screen.findByText('alice@example.com')).closest('tr')! + expect(within(alice).getByText('Alice')).toBeInTheDocument() + expect(within(alice).getByText('Active')).toBeInTheDocument() + const bob = screen.getByText('bob@example.com').closest('tr')! + expect(within(bob).getByText(/disabled/i)).toBeInTheDocument() + expect(within(bob).getByText(/3 bounces/i)).toBeInTheDocument() + const carol = screen.getByText('carol@example.com').closest('tr')! + expect(within(carol).getByText('Active')).toBeInTheDocument() + expect(within(carol).getByText(/1 bounce$/i)).toBeInTheDocument() + expect(screen.getByRole('heading', { name: 'Members (3)', level: 2 })).toBeInTheDocument() + }) + + it('has an empty state', async () => { + await setup() + expect(await screen.findByText(/no members yet/i)).toBeInTheDocument() + }) + + it('filters the table as you type', async () => { + const { user } = await setup([member('alice@example.com', { display_name: 'Alice' }), member('bob@example.com')]) + await screen.findByText('bob@example.com') + await user.type(screen.getByRole('searchbox', { name: /filter members/i }), 'ALI') + expect(screen.getByText('alice@example.com')).toBeInTheDocument() + expect(screen.queryByText('bob@example.com')).not.toBeInTheDocument() + await user.clear(screen.getByRole('searchbox', { name: /filter members/i })) + await user.type(screen.getByRole('searchbox', { name: /filter members/i }), 'zzz') + expect(await screen.findByText(/no members match/i)).toBeInTheDocument() + }) + + it('adds a member and clears the form', async () => { + const { user, state } = await setup([member('alice@example.com')]) + await user.type(screen.getByLabelText(/email address/i), 'Dave@Example.com') + await user.type(screen.getByLabelText(/display name/i), 'Dave') + await user.click(screen.getByRole('button', { name: /add member/i })) + expect(await screen.findByText('dave@example.com')).toBeInTheDocument() + expect(state.members[1].map((m) => m.address)).toEqual(['alice@example.com', 'dave@example.com']) + expect(screen.getByLabelText(/email address/i)).toHaveValue('') + expect(screen.getByLabelText(/display name/i)).toHaveValue('') + expect(await screen.findByText(/added dave@example.com/i)).toBeInTheDocument() + }) + + it('asks for an address before calling the API', async () => { + const { user, state } = await setup() + await user.click(screen.getByRole('button', { name: /add member/i })) + expect(await screen.findByText(/enter an email address/i)).toBeInTheDocument() + expect(state.calls.filter((c) => c.startsWith('POST'))).toEqual([]) + }) + + it('shows the server error for a bad address and keeps what was typed', async () => { + const { user } = await setup() + await user.type(screen.getByLabelText(/email address/i), 'not-an-address') + await user.click(screen.getByRole('button', { name: /add member/i })) + expect(await screen.findByRole('alert')).toHaveTextContent('a valid email address is required') + expect(screen.getByLabelText(/email address/i)).toHaveValue('not-an-address') + }) + + it('re-enables a disabled member', async () => { + const { user, state } = await setup([member('bob@example.com', { display_name: 'Bob', disabled: true, bounce_count: 3 })]) + const row = (await screen.findByText('bob@example.com')).closest('tr')! + await user.click(within(row).getByRole('button', { name: /re-enable/i })) + await waitFor(() => expect(state.members[1][0].disabled).toBe(false)) + expect(state.members[1][0].display_name).toBe('Bob') + expect(await screen.findByText('Active')).toBeInTheDocument() + expect(screen.queryByRole('button', { name: /re-enable/i })).not.toBeInTheDocument() + }) + + it('does not offer re-enable for active members', async () => { + await setup([member('alice@example.com')]) + await screen.findByText('alice@example.com') + expect(screen.queryByRole('button', { name: /re-enable/i })).not.toBeInTheDocument() + }) + + describe('removing', () => { + async function open() { + const ctx = await setup([member('alice@example.com'), member('a+b@example.com')]) + const row = (await screen.findByText('a+b@example.com')).closest('tr')! + await ctx.user.click(within(row).getByRole('button', { name: /remove/i })) + return { ...ctx, dialog: await screen.findByRole('alertdialog') } + } + + it('confirms first, naming the address; cancel keeps the member', async () => { + const { user, state, dialog } = await open() + expect(dialog).toHaveTextContent('a+b@example.com') + await user.click(within(dialog).getByRole('button', { name: /cancel/i })) + await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()) + expect(state.members[1]).toHaveLength(2) + }) + + it('removes the member, encoding the address in the URL', async () => { + const { user, state, dialog } = await open() + await user.click(within(dialog).getByRole('button', { name: /^remove$/i })) + await waitFor(() => expect(screen.queryByText('a+b@example.com')).not.toBeInTheDocument()) + expect(state.members[1].map((m) => m.address)).toEqual(['alice@example.com']) + expect(state.calls).toContain('DELETE /lists/1/members/a%2Bb%40example.com') + expect(screen.getByText('alice@example.com')).toBeInTheDocument() + }) + + it('shows a server error inside the dialog', async () => { + const { user, state, dialog } = await open() + state.fail['DELETE /lists/1/members/a%2Bb%40example.com'] = { status: 500, error: 'locked' } + await user.click(within(dialog).getByRole('button', { name: /^remove$/i })) + expect(await within(dialog).findByRole('alert')).toHaveTextContent('locked') + expect(state.members[1]).toHaveLength(2) + }) + }) +}) diff --git a/admin/src/tabs/MembersTab.tsx b/admin/src/tabs/MembersTab.tsx index cf0639f..6a669ef 100644 --- a/admin/src/tabs/MembersTab.tsx +++ b/admin/src/tabs/MembersTab.tsx @@ -1,3 +1,202 @@ +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' +import { useState, type FormEvent } from 'react' +import { useOutletContext } from 'react-router' +import { toast } from 'sonner' +import { + AlertDialog, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, +} from '@/components/ui/alert-dialog' +import { Badge } from '@/components/ui/badge' +import { Button } from '@/components/ui/button' +import { Input } from '@/components/ui/input' +import { Label } from '@/components/ui/label' +import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table' +import { api, type List, type Member } from '@/lib/api' + export function MembersTab() { - return

Members

+ const list = useOutletContext() + const queryClient = useQueryClient() + const key = ['members', list.id] + const members = useQuery({ queryKey: key, queryFn: () => api.members.list(list.id) }) + const [filter, setFilter] = useState('') + const [removing, setRemoving] = useState(null) + + const reenable = useMutation({ + // Adding an address again clears the bounce state. + mutationFn: (m: Member) => api.members.add(list.id, { address: m.address, display_name: m.display_name }), + onSuccess: async (m) => { + await queryClient.invalidateQueries({ queryKey: key }) + toast.success(`Re-enabled ${m.address}`) + }, + onError: (e: Error) => toast.error(e.message), + }) + + const all = members.data ?? [] + const q = filter.trim().toLowerCase() + const shown = q ? all.filter((m) => m.address.includes(q) || m.display_name.toLowerCase().includes(q)) : all + + return ( +
+

Members{members.data ? ` (${all.length})` : ''}

+ + + {members.isPending &&

Loading…

} + {members.isError &&

{members.error.message}

} + {members.data && all.length === 0 &&

No members yet.

} + {all.length > 0 && ( + <> + setFilter(e.target.value)} + /> + {shown.length === 0 ? ( +

No members match.

+ ) : ( + + + + Address + Name + Status + Actions + + + + {shown.map((m) => ( + + {m.address} + {m.display_name} + + {m.disabled ? Disabled : Active} + {m.bounce_count > 0 && ( + + {m.bounce_count} {m.bounce_count === 1 ? 'bounce' : 'bounces'} + + )} + + + {m.disabled && ( + + )} + + + + ))} + +
+ )} + + )} + setRemoving(null)} /> +
+ ) +} + +function AddMemberForm({ list }: { list: List }) { + const queryClient = useQueryClient() + const [address, setAddress] = useState('') + const [name, setName] = useState('') + const [error, setError] = useState(null) + + const add = useMutation({ + mutationFn: () => api.members.add(list.id, { address: address.trim(), display_name: name.trim() }), + onSuccess: async (m) => { + await queryClient.invalidateQueries({ queryKey: ['members', list.id] }) + toast.success(`Added ${m.address}`) + setAddress('') + setName('') + setError(null) + }, + onError: (e: Error) => setError(e.message), + }) + + function submit(e: FormEvent) { + e.preventDefault() + if (address.trim() === '') { + setError('Enter an email address') + return + } + setError(null) + add.mutate() + } + + return ( +
+
+
+ + setAddress(e.target.value)} autoComplete="off" /> +
+
+ + setName(e.target.value)} autoComplete="off" /> +
+ +
+ {error && ( +

+ {error} +

+ )} +
+ ) +} + +function RemoveDialog({ list, member, onClose }: { list: List; member: Member | null; onClose: () => void }) { + const queryClient = useQueryClient() + const [error, setError] = useState(null) + const remove = useMutation({ + mutationFn: (address: string) => api.members.remove(list.id, address), + onSuccess: async (_, address) => { + await queryClient.invalidateQueries({ queryKey: ['members', list.id] }) + toast.success(`Removed ${address}`) + onClose() + }, + onError: (e: Error) => setError(e.message), + }) + + return ( + { + if (!open) { + setError(null) + onClose() + } + }} + > + + + Remove {member?.address}? + They stop receiving mail from {list.address} and can no longer post. + + {error && ( +

+ {error} +

+ )} + + Cancel + + +
+
+ ) }