Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions __tests__/e2e/admin/collections/tenants.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CollectionSlugs,
getSelectInputOptions,
getSelectInputValue,
isSelectReadOnly,
openDocControls,
selectInput,
waitForFormReady,
Expand Down Expand Up @@ -58,22 +59,28 @@ authTest.describe('Tenants', () => {
authTest.describe('Read', () => {
authTest.describe.configure({ timeout: 60000 })

authTest('slug field shows current value on existing tenant', async ({ adminPage }) => {
await adminPage.goto(tenantsUrl.list)
await adminPage.waitForLoadState('networkidle')
authTest(
'slug field shows current value and read-only on existing tenant',
async ({ adminPage }) => {
await adminPage.goto(tenantsUrl.list)
await adminPage.waitForLoadState('networkidle')

const firstLink = adminPage.locator('table tbody tr td a').first()
await firstLink.waitFor({ timeout: 15000 })
await firstLink.click()
await adminPage.waitForURL(/\/collections\/tenants\/\d+/)
await waitForFormReady(adminPage)
const firstLink = adminPage.locator('table tbody tr td a').first()
await firstLink.waitFor({ timeout: 15000 })
await firstLink.click()
await adminPage.waitForURL(/\/collections\/tenants\/\d+/)
await waitForFormReady(adminPage)

const slugField = adminPage.locator('#field-slug')
await expect(slugField).toBeVisible()
const slugField = adminPage.locator('#field-slug')
await expect(slugField).toBeVisible()

const value = await getSelectInputValue({ selectLocator: slugField })
expect(value).toBeTruthy()
})
const value = await getSelectInputValue({ selectLocator: slugField })
expect(value).toBeTruthy()

const readOnly = await isSelectReadOnly({ selectLocator: slugField })
expect(readOnly).toBe(true)
},
)
})

authTest.describe('Delete', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/app/(payload)/admin/importMap.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/collections/Tenants/components/SlugCell.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { DefaultServerCellComponentProps } from 'payload'

export function SlugCell({ cellData }: Pick<DefaultServerCellComponentProps, 'cellData'>) {
if (typeof cellData !== 'string') return null
return <>{cellData.toUpperCase()}</>
}
23 changes: 21 additions & 2 deletions src/collections/Tenants/components/TenantSlugField.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import type { SelectFieldServerComponent } from 'payload'

import { SelectField } from '@payloadcms/ui'
import { SelectField, SelectInput } from '@payloadcms/ui'

export const TenantSlugField: SelectFieldServerComponent = async ({
clientField,
data,
field,
payload,
}) => {
const currentSlug = data?.slug
// Slug is immutable after creation
if (data.id) {
return (
<SelectInput
name={field.name}
path={field.name}
label={clientField.label}
description={clientField.admin?.description}
options={[{ label: String(currentSlug).toUpperCase(), value: currentSlug }]}
value={currentSlug}
required
readOnly
/>
)
}

const { docs } = await payload.find({
collection: 'tenants',
limit: 0,
Expand All @@ -18,7 +36,8 @@ export const TenantSlugField: SelectFieldServerComponent = async ({

const options = clientField.options?.filter((option) => {
const value = typeof option === 'string' ? option : option.value
return !usedSlugs.has(value)
// Keep the current document's slug so the label displays correctly
return value === currentSlug || !usedSlugs.has(value)
})

return <SelectField field={{ ...clientField, options }} path={field.name} />
Expand Down
3 changes: 2 additions & 1 deletion src/collections/Tenants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ export const Tenants: CollectionConfig = {
type: 'select',
admin: {
components: {
Cell: '@/collections/Tenants/components/SlugCell#SlugCell',
Field: '@/collections/Tenants/components/TenantSlugField#TenantSlugField',
},
description: 'Avalanche center identifier. Used for subdomains and URL paths.',
},
options: VALID_TENANT_SLUGS.map((slug) => ({
label: `${AVALANCHE_CENTERS[slug].name} (${slug})`,
label: `${slug.toUpperCase()} — ${AVALANCHE_CENTERS[slug].name}`,
value: slug,
})),
index: true,
Expand Down
Loading