From 8ed74f08a4c084e616247414d7a586d66c34a9cf Mon Sep 17 00:00:00 2001 From: Christian Andolf Date: Mon, 28 Apr 2025 14:57:59 +0200 Subject: [PATCH] fix: make proper use of type predicates and minor cleanup to avoid double ternary and reduce computation --- .../DynamicContent/Stays/Previous/Client.tsx | 17 ++++++++--------- .../DynamicContent/Stays/Upcoming/Client.tsx | 17 ++++++++--------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/apps/scandic-web/components/Blocks/DynamicContent/Stays/Previous/Client.tsx b/apps/scandic-web/components/Blocks/DynamicContent/Stays/Previous/Client.tsx index 686bdceb5..2e82511df 100644 --- a/apps/scandic-web/components/Blocks/DynamicContent/Stays/Previous/Client.tsx +++ b/apps/scandic-web/components/Blocks/DynamicContent/Stays/Previous/Client.tsx @@ -36,22 +36,21 @@ export default function ClientPreviousStays({ } ) + if (isLoading) { + return + } + function loadMoreData() { if (hasNextPage) { fetchNextPage() } } - // TS having a hard time with the filtered type. - // This is only temporary as we will not return null - // later on when we handle errors appropriately. - const filteredStays = (data?.pages.filter((page) => page?.data) ?? - []) as unknown as PreviousStaysNonNullResponseObject[] - const stays = filteredStays.flatMap((page) => page.data) + const stays = data.pages + .filter((page): page is PreviousStaysNonNullResponseObject => !!page?.data) + .flatMap((page) => page.data) - return isLoading ? ( - - ) : ( + return ( {stays.map((stay) => ( diff --git a/apps/scandic-web/components/Blocks/DynamicContent/Stays/Upcoming/Client.tsx b/apps/scandic-web/components/Blocks/DynamicContent/Stays/Upcoming/Client.tsx index b793159b6..84c9a01db 100644 --- a/apps/scandic-web/components/Blocks/DynamicContent/Stays/Upcoming/Client.tsx +++ b/apps/scandic-web/components/Blocks/DynamicContent/Stays/Upcoming/Client.tsx @@ -36,22 +36,21 @@ export default function ClientUpcomingStays({ } ) + if (isLoading) { + return + } + function loadMoreData() { if (hasNextPage) { fetchNextPage() } } - // TS having a hard time with the filtered type. - // This is only temporary as we will not return null - // later on when we handle errors appropriately. - const filteredStays = (data?.pages.filter((page) => page && page.data) ?? - []) as unknown as UpcomingStaysNonNullResponseObject[] - const stays = filteredStays.flatMap((page) => page.data) + const stays = data.pages + .filter((page): page is UpcomingStaysNonNullResponseObject => !!page?.data) + .flatMap((page) => page.data) - return isLoading ? ( - - ) : stays.length ? ( + return stays.length ? ( {stays.map((stay) => (