From 9a233ae2d5551d7c0b6839631ce08f4c672c40c8 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Tue, 3 Sep 2024 15:16:31 +0200 Subject: [PATCH] refactor(page): clearer flow in conditional rendering --- src/app/(main)/[slug]/page.tsx | 46 ++++++++++++++-------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/src/app/(main)/[slug]/page.tsx b/src/app/(main)/[slug]/page.tsx index 44fa17ef1..a66985525 100644 --- a/src/app/(main)/[slug]/page.tsx +++ b/src/app/(main)/[slug]/page.tsx @@ -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) => ( + + ))} + + ); } // 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( POSTS_QUERY, { slug }, @@ -85,23 +92,6 @@ async function Page({ params }: Props) { ); } - if (initialPage.data && !initialBlogPage.data) { - return ( - <> - {initialPage.data?.sections?.map((section, index) => ( - - ))} - - ); - } - if (initialCompensationsPage.data) { return isDraftMode ? ( @@ -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;