-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fetch page data based on catch-all route
first step in supporting more advanced paths like nested slugs
- Loading branch information
Showing
4 changed files
with
363 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { Metadata } from "next"; | ||
|
||
import CustomErrorMessage from "src/blog/components/customErrorMessage/CustomErrorMessage"; | ||
import Legal from "src/blog/components/legal/Legal"; | ||
import LegalPreview from "src/blog/components/legal/LegalPreview"; | ||
import { homeLink } from "src/blog/components/utils/linkTypes"; | ||
import Compensations from "src/compensations/Compensations"; | ||
import CompensationsPreview from "src/compensations/CompensationsPreview"; | ||
import CustomerCases from "src/customerCases/CustomerCases"; | ||
import CustomerCasesPreview from "src/customerCases/CustomerCasesPreview"; | ||
import { getDraftModeInfo } from "src/utils/draftmode"; | ||
import { fetchPageDataFromParams } from "src/utils/pageData"; | ||
import SectionRenderer from "src/utils/renderSection"; | ||
import { fetchSeoData, generateMetadataFromSeo } from "src/utils/seo"; | ||
import { SEO_SLUG_QUERY } from "studio/lib/queries/pages"; | ||
|
||
export const dynamic = "force-dynamic"; | ||
|
||
type Props = { | ||
params: { lang: string; path: string[] }; | ||
}; | ||
|
||
function deprecated__slugFromPath(path: string[]) { | ||
return path[0]; | ||
} | ||
|
||
export async function generateMetadata({ params }: Props): Promise<Metadata> { | ||
const seo = await fetchSeoData(SEO_SLUG_QUERY, { | ||
slug: deprecated__slugFromPath(params.path), | ||
}); | ||
|
||
return generateMetadataFromSeo(seo); | ||
} | ||
|
||
const Page404 = ( | ||
<CustomErrorMessage | ||
title="404 — Something went wrong" | ||
body="The page you are looking for does not exist. There may be an error in the URL, or the page may have been moved or deleted." | ||
link={homeLink} | ||
/> | ||
); | ||
|
||
async function Page({ params }: Props) { | ||
const { lang, path } = params; | ||
|
||
const { perspective, isDraftMode } = getDraftModeInfo(); | ||
|
||
const pageData = await fetchPageDataFromParams({ | ||
language: lang, | ||
path, | ||
perspective: perspective ?? "published", | ||
}); | ||
|
||
if (pageData == null) { | ||
return Page404; | ||
} | ||
|
||
const { queryResponse, docType } = pageData; | ||
|
||
switch (docType) { | ||
case "pageBuilder": | ||
return ( | ||
<> | ||
{queryResponse.data?.sections?.map((section, index) => ( | ||
<SectionRenderer | ||
key={section._key} | ||
section={section} | ||
isDraftMode={isDraftMode} | ||
initialData={queryResponse} | ||
isLandingPage={false} | ||
sectionIndex={index} | ||
/> | ||
))} | ||
</> | ||
); | ||
case "compensations": | ||
return isDraftMode ? ( | ||
<CompensationsPreview | ||
initialCompensations={queryResponse.compensationsPage} | ||
initialLocations={queryResponse.companyLocations} | ||
initialLocale={queryResponse.locale} | ||
/> | ||
) : ( | ||
<Compensations | ||
compensations={queryResponse.compensationsPage.data} | ||
locations={queryResponse.companyLocations.data} | ||
locale={queryResponse.locale.data} | ||
/> | ||
); | ||
case "customerCasesPage": | ||
return isDraftMode ? ( | ||
<CustomerCasesPreview initialCustomerCases={queryResponse} /> | ||
) : ( | ||
<CustomerCases customerCasesPage={queryResponse.data} /> | ||
); | ||
case "customerCase": | ||
return ( | ||
// TODO: implement customer case detail page | ||
<pre style={{ background: "hotpink", marginTop: "8rem" }}> | ||
{JSON.stringify(pageData, null, 2)} | ||
</pre> | ||
); | ||
case "legalDocument": | ||
return isDraftMode ? ( | ||
<LegalPreview initialDocument={queryResponse} /> | ||
) : ( | ||
<Legal document={queryResponse.data} /> | ||
); | ||
} | ||
|
||
return Page404; | ||
} | ||
|
||
export default Page; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.