161 lines
5.6 KiB
TypeScript
161 lines
5.6 KiB
TypeScript
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 { normalizeApplicationFormData, type SpaApplicationFormData } from '@/spa/applicationForm'
|
|
import { normalizeApplicationPhaseData, type SpaApplicationPhaseData } from '@/spa/applicationPhase'
|
|
import { normalizeFooterData, normalizeHeaderData } from '@/spa/cmsNavigation'
|
|
import type { CmsRouteCollection, CmsRouteDoc } from '@/spa/cmsRoute'
|
|
import { normalizeNewsletterFormData, type SpaNewsletterFormData } from '@/spa/newsletterForm'
|
|
import { normalizeSiteSettings, type SpaSiteSettingsData } from '@/spa/siteSettings'
|
|
import { getPublicationQueryOptions } from '@/utilities/publicationQuery'
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
type Args = {
|
|
params: Promise<{ slug?: string[] }>
|
|
}
|
|
|
|
type RouteCollection = Exclude<CmsRouteCollection, 'partners' | 'jury-mitglieder'>
|
|
|
|
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,
|
|
depth: 1,
|
|
...getPublicationQueryOptions(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: CmsRouteCollection, draft: boolean) {
|
|
const result = await payload.find({
|
|
collection,
|
|
depth: 1,
|
|
...getPublicationQueryOptions(draft),
|
|
limit: 300,
|
|
pagination: false,
|
|
...(collection === 'partners' || collection === 'jury-mitglieder' ? { sort: 'sortOrder' } : {}),
|
|
})
|
|
|
|
return result.docs
|
|
}
|
|
|
|
async function findSupportPages(payload: Awaited<ReturnType<typeof getPayload>>, draft: boolean) {
|
|
const result = await payload.find({
|
|
collection: 'pages',
|
|
depth: 1,
|
|
...getPublicationQueryOptions(draft),
|
|
limit: 10,
|
|
pagination: false,
|
|
where: {
|
|
or: [
|
|
{ spaPath: { equals: '/' } },
|
|
{ slug: { equals: 'startseite' } },
|
|
{ slug: { equals: 'home' } },
|
|
{ spaPath: { equals: '/netzwerk' } },
|
|
{ slug: { equals: 'netzwerk' } },
|
|
{ slug: { equals: 'network' } },
|
|
{ spaPath: { equals: '/teilnahme' } },
|
|
{ slug: { equals: 'teilnahme' } },
|
|
{ slug: { equals: 'participation' } },
|
|
],
|
|
},
|
|
})
|
|
|
|
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 { isEnabled: draft } = await draftMode()
|
|
const [
|
|
header,
|
|
footer,
|
|
siteSettings,
|
|
applicationForm,
|
|
applicationPhase,
|
|
newsletterForm,
|
|
route,
|
|
supportPages,
|
|
preistraeger,
|
|
events,
|
|
posts,
|
|
partners,
|
|
juryMitglieder,
|
|
] = await Promise.all([
|
|
payload.findGlobal({ slug: 'header', depth: 1 }).catch(() => undefined),
|
|
payload.findGlobal({ slug: 'footer', depth: 1 }).catch(() => undefined),
|
|
payload.findGlobal({ slug: 'site-settings', depth: 1 }).catch(() => undefined),
|
|
payload.findGlobal({ slug: 'application-form', depth: 1 }).catch(() => undefined),
|
|
payload.findGlobal({ slug: 'application-phase', depth: 0 }).catch(() => undefined),
|
|
payload.findGlobal({ slug: 'newsletter-form', depth: 0 }).catch(() => undefined),
|
|
findRouteDoc(payload, pathname, draft),
|
|
findSupportPages(payload, draft).catch(() => []),
|
|
findList(payload, 'preistraeger', draft).catch(() => []),
|
|
findList(payload, 'events', draft).catch(() => []),
|
|
findList(payload, 'posts', draft).catch(() => []),
|
|
findList(payload, 'partners', draft).catch(() => []),
|
|
findList(payload, 'jury-mitglieder', draft).catch(() => []),
|
|
])
|
|
|
|
if (!route) notFound()
|
|
|
|
return (
|
|
<NextApp
|
|
pathname={pathname}
|
|
header={normalizeHeaderData(header)}
|
|
footer={normalizeFooterData(footer)}
|
|
siteSettings={normalizeSiteSettings(siteSettings as Partial<SpaSiteSettingsData> | undefined)}
|
|
applicationForm={normalizeApplicationFormData(applicationForm as SpaApplicationFormData | undefined)}
|
|
applicationPhase={normalizeApplicationPhaseData(applicationPhase as Partial<SpaApplicationPhaseData> | undefined)}
|
|
newsletterForm={normalizeNewsletterFormData(newsletterForm as Partial<SpaNewsletterFormData> | undefined)}
|
|
cmsRoute={{
|
|
collection: route.collection,
|
|
doc: route.doc as unknown as CmsRouteDoc,
|
|
lists: {
|
|
pages: supportPages as unknown as CmsRouteDoc[],
|
|
preistraeger: preistraeger as unknown as CmsRouteDoc[],
|
|
events: events as unknown as CmsRouteDoc[],
|
|
posts: posts as unknown as CmsRouteDoc[],
|
|
partners: partners as unknown as CmsRouteDoc[],
|
|
'jury-mitglieder': juryMitglieder as unknown as CmsRouteDoc[],
|
|
},
|
|
}}
|
|
/>
|
|
)
|
|
}
|