Skip to content

Commit

Permalink
feat: fetch page data based on catch-all route
Browse files Browse the repository at this point in the history
first step in supporting more advanced paths like nested slugs
  • Loading branch information
mathiazom committed Oct 10, 2024
1 parent d3d0db1 commit 847392d
Show file tree
Hide file tree
Showing 4 changed files with 363 additions and 180 deletions.
114 changes: 114 additions & 0 deletions src/app/(main)/[lang]/[...path]/page.tsx
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;
180 changes: 0 additions & 180 deletions src/app/(main)/[lang]/[slug]/page.tsx

This file was deleted.

Loading

0 comments on commit 847392d

Please sign in to comment.