Skip to content

Commit

Permalink
fix: custom id field not shown depending on field and db types (#9091)
Browse files Browse the repository at this point in the history
Closes #9080
  • Loading branch information
paulpopus authored Nov 11, 2024
1 parent 8a20231 commit 9a970d2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
33 changes: 25 additions & 8 deletions packages/next/src/utilities/initPage/handleAdminPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import type {
SanitizedGlobalConfig,
} from 'payload'

import { fieldAffectsData } from 'payload/shared'

import { getRouteWithoutAdmin, isAdminRoute } from './shared.js'

type Args = {
adminRoute: string
config: SanitizedConfig
defaultIDType: Payload['db']['defaultIDType']
payload?: Payload
route: string
}

Expand All @@ -22,23 +25,23 @@ type RouteInfo = {
globalSlug?: string
}

export function getRouteInfo({ adminRoute, config, defaultIDType, route }: Args): RouteInfo {
export function getRouteInfo({
adminRoute,
config,
defaultIDType,
payload,
route,
}: Args): RouteInfo {
if (isAdminRoute({ adminRoute, config, route })) {
const routeWithoutAdmin = getRouteWithoutAdmin({ adminRoute, route })
const routeSegments = routeWithoutAdmin.split('/').filter(Boolean)
const [entityType, entitySlug, createOrID] = routeSegments
const collectionSlug = entityType === 'collections' ? entitySlug : undefined
const globalSlug = entityType === 'globals' ? entitySlug : undefined

const docID =
collectionSlug && createOrID !== 'create'
? defaultIDType === 'number'
? Number(createOrID)
: createOrID
: undefined

let collectionConfig: SanitizedCollectionConfig | undefined
let globalConfig: SanitizedGlobalConfig | undefined
let idType = defaultIDType

if (collectionSlug) {
collectionConfig = config.collections.find((collection) => collection.slug === collectionSlug)
Expand All @@ -48,6 +51,20 @@ export function getRouteInfo({ adminRoute, config, defaultIDType, route }: Args)
globalConfig = config.globals.find((global) => global.slug === globalSlug)
}

// If the collection has an ID field, we need to determine the type of the ID field
if (collectionConfig && payload) {
if (payload.collections?.[collectionSlug]) {
idType = payload.collections?.[collectionSlug].customIDType
}
}

const docID =
collectionSlug && createOrID !== 'create'
? idType === 'number'
? Number(createOrID)
: createOrID
: undefined

return {
collectionConfig,
collectionSlug,
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/utilities/initPage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export const initPage = async ({
adminRoute,
config: payload.config,
defaultIDType: payload.db.defaultIDType,
payload,
route,
})

Expand Down

0 comments on commit 9a970d2

Please sign in to comment.