Skip to content

Commit

Permalink
refactor(page): clearer flow in conditional rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Sep 3, 2024
1 parent 4fe23e8 commit 13d8828
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions src/app/(main)/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,25 @@ async function Page({ params }: Props) {
),
]);

if (
!initialPage.data &&
!initialBlogPage.data &&
!initialCompensationsPage.data
) {
console.log(`Page ${slug} not found`);
// TODO: add error snackbar
redirect("/");
if (initialPage.data) {
return (
<>
{initialPage.data?.sections?.map((section, index) => (
<SectionRenderer
key={section._key}
section={section}
isDraftMode={isDraftMode}
initialData={initialPage}
isLandingPage={false}
sectionIndex={index}
/>
))}
</>
);
}

// TODO: fix error for when initialBlogPage.data is empty (say slug doesn't exists)
if (!initialPage.data && initialBlogPage.data) {
if (initialBlogPage.data) {
const initialPosts = await loadQuery<Post[]>(
POSTS_QUERY,
{ slug },
Expand All @@ -85,23 +92,6 @@ async function Page({ params }: Props) {
);
}

if (initialPage.data && !initialBlogPage.data) {
return (
<>
{initialPage.data?.sections?.map((section, index) => (
<SectionRenderer
key={section._key}
section={section}
isDraftMode={isDraftMode}
initialData={initialPage}
isLandingPage={false}
sectionIndex={index}
/>
))}
</>
);
}

if (initialCompensationsPage.data) {
return isDraftMode ? (
<CompensationsPreview initialCompensations={initialCompensationsPage} />
Expand All @@ -110,7 +100,9 @@ async function Page({ params }: Props) {
);
}

return null;
console.log(`Page ${slug} not found`);
// TODO: add error snackbar
redirect("/");
}

export default Page;

0 comments on commit 13d8828

Please sign in to comment.