chore: migrate pages and media to be cms controlled.
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import configPromise from '@payload-config'
|
||||
import { getPayload } from 'payload'
|
||||
|
||||
import { draftMode } from 'next/headers'
|
||||
import { notFound } from 'next/navigation'
|
||||
|
||||
import NextApp from '@/spa/NextApp'
|
||||
import { normalizeFooterData, normalizeHeaderData } from '@/spa/cmsNavigation'
|
||||
import { normalizeSpaMediaMap } from '@/spa/cmsMedia'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
@@ -11,22 +13,90 @@ type Args = {
|
||||
params: Promise<{ slug?: string[] }>
|
||||
}
|
||||
|
||||
type RouteCollection = 'pages' | 'posts' | 'events' | 'preistraeger'
|
||||
|
||||
function routeTarget(pathname: string): { collection: RouteCollection; slug: string } {
|
||||
const parts = pathname.split('/').filter(Boolean)
|
||||
|
||||
if (parts[0] === 'preistraeger' && parts[1]) {
|
||||
return { collection: 'preistraeger', slug: parts[1] }
|
||||
}
|
||||
|
||||
if (parts[0] === 'presse' && parts[1] === 'events' && parts[2]) {
|
||||
return { collection: 'events', slug: parts[2] }
|
||||
}
|
||||
|
||||
if (parts[0] === 'presse' && parts[1] === 'blog' && parts[2]) {
|
||||
return { collection: 'posts', slug: parts[2] }
|
||||
}
|
||||
|
||||
return { collection: 'pages', slug: pathname === '/' ? 'startseite' : parts.join('-') }
|
||||
}
|
||||
|
||||
async function findRouteDoc(payload: Awaited<ReturnType<typeof getPayload>>, pathname: string, draft: boolean) {
|
||||
const target = routeTarget(pathname)
|
||||
const result = await payload.find({
|
||||
collection: target.collection as any,
|
||||
depth: 1,
|
||||
draft,
|
||||
overrideAccess: draft,
|
||||
limit: 1,
|
||||
pagination: false,
|
||||
where: {
|
||||
or: [
|
||||
{ spaPath: { equals: pathname } },
|
||||
{ slug: { equals: target.slug } },
|
||||
],
|
||||
},
|
||||
})
|
||||
|
||||
const doc = result.docs[0]
|
||||
return doc ? { collection: target.collection, doc } : undefined
|
||||
}
|
||||
|
||||
async function findList(payload: Awaited<ReturnType<typeof getPayload>>, collection: RouteCollection, draft: boolean) {
|
||||
const result = await payload.find({
|
||||
collection: collection as any,
|
||||
depth: 1,
|
||||
draft,
|
||||
overrideAccess: draft,
|
||||
limit: 300,
|
||||
pagination: false,
|
||||
})
|
||||
|
||||
return result.docs
|
||||
}
|
||||
|
||||
export default async function Page({ params }: Args) {
|
||||
const { slug = [] } = await params
|
||||
const pathname = `/${slug.join('/')}`.replace(/\/$/, '') || '/'
|
||||
const payload = await getPayload({ config: configPromise })
|
||||
const [header, footer, spaMedia] = await Promise.all([
|
||||
const { isEnabled: draft } = await draftMode()
|
||||
const [header, footer, route, preistraeger, events, posts] = await Promise.all([
|
||||
payload.findGlobal({ slug: 'header', depth: 1 }).catch(() => undefined),
|
||||
payload.findGlobal({ slug: 'footer', depth: 1 }).catch(() => undefined),
|
||||
payload.findGlobal({ slug: 'spa-media', depth: 1 }).catch(() => undefined),
|
||||
findRouteDoc(payload, pathname, draft),
|
||||
findList(payload, 'preistraeger', draft).catch(() => []),
|
||||
findList(payload, 'events', draft).catch(() => []),
|
||||
findList(payload, 'posts', draft).catch(() => []),
|
||||
])
|
||||
|
||||
if (!route) notFound()
|
||||
|
||||
return (
|
||||
<NextApp
|
||||
pathname={pathname}
|
||||
header={normalizeHeaderData(header)}
|
||||
footer={normalizeFooterData(footer)}
|
||||
media={normalizeSpaMediaMap(spaMedia)}
|
||||
cmsRoute={{
|
||||
collection: route.collection,
|
||||
doc: route.doc as any,
|
||||
lists: {
|
||||
preistraeger: preistraeger as any,
|
||||
events: events as any,
|
||||
posts: posts as any,
|
||||
},
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user