diff --git a/apps/web/e2e/tests/platform/rest-people.spec.ts b/apps/web/e2e/tests/platform/rest-people.spec.ts index 2286ae6ee4..7d933f80f7 100644 --- a/apps/web/e2e/tests/platform/rest-people.spec.ts +++ b/apps/web/e2e/tests/platform/rest-people.spec.ts @@ -927,7 +927,8 @@ test.describe('REST segment members', () => { }), 200 ) - expect(again.data).toEqual({ added: 1, failed: [] }) + // Already a member: nothing is added, and the manual row is kept. + expect(again.data).toEqual({ added: 0, failed: [] }) expect((await segmentRows(seg.uuid)).get(toUuid(u3.principalId))).toBe('manual') await admin.api.dispose() } diff --git a/apps/web/src/components/admin/settings/__tests__/scalar-attribute-list.test.tsx b/apps/web/src/components/admin/settings/__tests__/scalar-attribute-list.test.tsx index 0d96804355..be9b38bd65 100644 --- a/apps/web/src/components/admin/settings/__tests__/scalar-attribute-list.test.tsx +++ b/apps/web/src/components/admin/settings/__tests__/scalar-attribute-list.test.tsx @@ -18,9 +18,12 @@ const m = vi.hoisted(() => { companyCreate: make(), companyUpdate: make(), companyDelete: make(), + toastError: vi.fn(), } }) +vi.mock('@/lib/client/error-notice', () => ({ toastError: m.toastError })) + vi.mock('@/lib/client/mutations', () => ({ useCreateUserAttribute: () => m.userCreate, useUpdateUserAttribute: () => m.userUpdate, @@ -104,6 +107,57 @@ describe.each(ENTITIES)('ScalarAttributeList for $name', (entity) => { await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()) }) + it('keeps the dialog open and says why when the server refuses the key', async () => { + const { create } = entity.mocks() + create.mutateAsync.mockRejectedValue(new Error('An attribute with that key already exists')) + const user = userEvent.setup() + renderList([]) + await user.click(screen.getAllByRole('button', { name: /new attribute/i })[0]) + const dialog = await screen.findByRole('dialog') + await user.type(within(dialog).getByLabelText(/^Key/), 'seats') + await user.type(within(dialog).getByLabelText('Display label'), 'Seats again') + await user.click(within(dialog).getByRole('button', { name: 'Create attribute' })) + expect((await within(dialog).findByRole('alert')).textContent).toBe( + 'An attribute with that key already exists' + ) + expect(screen.getByRole('dialog')).toBeTruthy() + + // Reopening starts clean. + await user.click(within(dialog).getByRole('button', { name: 'Cancel' })) + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()) + await user.click(screen.getAllByRole('button', { name: /new attribute/i })[0]) + expect(within(await screen.findByRole('dialog')).queryByRole('alert')).toBeNull() + }) + + it('says why when the server refuses an edit', async () => { + const { update } = entity.mocks() + update.mutateAsync.mockRejectedValue(new Error('Attribute not found')) + const user = userEvent.setup() + renderList() + await user.click(screen.getByRole('button', { name: 'Actions for Seats' })) + await user.click(await screen.findByRole('menuitem', { name: 'Edit' })) + const dialog = await screen.findByRole('dialog') + await user.click(within(dialog).getByRole('button', { name: 'Save changes' })) + expect((await within(dialog).findByRole('alert')).textContent).toBe('Attribute not found') + }) + + it('toasts a refused delete and keeps the row', async () => { + const { del } = entity.mocks() + del.mutateAsync.mockRejectedValue(new Error('Attribute not found')) + const user = userEvent.setup() + renderList() + await user.click(screen.getByRole('button', { name: 'Actions for Seats' })) + await user.click(await screen.findByRole('menuitem', { name: 'Delete' })) + await user.click( + within(await screen.findByRole('alertdialog')).getByRole('button', { + name: 'Delete attribute', + }) + ) + await waitFor(() => expect(m.toastError).toHaveBeenCalledTimes(1)) + expect(m.toastError.mock.calls[0][0]).toMatchObject({ message: 'Attribute not found' }) + expect(screen.getByText('Seats')).toBeTruthy() + }) + it('edits an attribute with the key locked and saves the new label', async () => { const { update } = entity.mocks() update.mutateAsync.mockResolvedValue(stored({ label: 'Active seats' })) diff --git a/apps/web/src/components/admin/settings/scalar-attribute-list.tsx b/apps/web/src/components/admin/settings/scalar-attribute-list.tsx index e546b7d187..e40c293ee2 100644 --- a/apps/web/src/components/admin/settings/scalar-attribute-list.tsx +++ b/apps/web/src/components/admin/settings/scalar-attribute-list.tsx @@ -18,6 +18,8 @@ import { DialogFooter, } from '@/components/ui/dialog' import { ConfirmDialog } from '@/components/shared/confirm-dialog' +import { errorText } from '@/lib/client/error-text' +import { toastError } from '@/lib/client/error-notice' import { AttributeList, type AttributeListItem } from './attribute-list' const ATTRIBUTE_TYPES = [ @@ -131,9 +133,11 @@ function AttributeFormDialog({ const [type, setType] = useState(initialValues?.type ?? 'string') const [currencyCode, setCurrencyCode] = useState(initialValues?.currencyCode ?? 'USD') const [externalKey, setExternalKey] = useState(initialValues?.externalKey ?? '') + const [error, setError] = useState(null) useEffect(() => { if (open) { + setError(null) setKey(initialValues?.key ?? '') setLabel(initialValues?.label ?? '') setDescription(initialValues?.description ?? '') @@ -146,7 +150,17 @@ function AttributeFormDialog({ const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() - await onSubmit({ key, label, description, type, currencyCode, externalKey }) + setError(null) + try { + await onSubmit({ key, label, description, type, currencyCode, externalKey }) + } catch (err) { + setError( + errorText( + err, + isEditing ? "Couldn't save the attribute." : "Couldn't create the attribute." + ) + ) + } } const canSubmit = key.trim().length > 0 && label.trim().length > 0 @@ -253,6 +267,12 @@ function AttributeFormDialog({

{copy.externalHint}

+ {error && ( +

+ {error} +

+ )} +