Merged in fix/cleanup-ts-stays-components (pull request #1884)

fix: make proper use of type predicates and minor cleanup to avoid double ternary and reduce computation

Approved-by: Erik Tiekstra
This commit is contained in:
Christian Andolf
2025-04-29 07:02:31 +00:00
2 changed files with 16 additions and 18 deletions

View File

@@ -36,22 +36,21 @@ export default function ClientPreviousStays({
} }
) )
if (isLoading) {
return <LoadingSpinner />
}
function loadMoreData() { function loadMoreData() {
if (hasNextPage) { if (hasNextPage) {
fetchNextPage() fetchNextPage()
} }
} }
// TS having a hard time with the filtered type. const stays = data.pages
// This is only temporary as we will not return null .filter((page): page is PreviousStaysNonNullResponseObject => !!page?.data)
// later on when we handle errors appropriately. .flatMap((page) => page.data)
const filteredStays = (data?.pages.filter((page) => page?.data) ??
[]) as unknown as PreviousStaysNonNullResponseObject[]
const stays = filteredStays.flatMap((page) => page.data)
return isLoading ? ( return (
<LoadingSpinner />
) : (
<ListContainer> <ListContainer>
<Grids.Stackable> <Grids.Stackable>
{stays.map((stay) => ( {stays.map((stay) => (

View File

@@ -36,22 +36,21 @@ export default function ClientUpcomingStays({
} }
) )
if (isLoading) {
return <LoadingSpinner />
}
function loadMoreData() { function loadMoreData() {
if (hasNextPage) { if (hasNextPage) {
fetchNextPage() fetchNextPage()
} }
} }
// TS having a hard time with the filtered type. const stays = data.pages
// This is only temporary as we will not return null .filter((page): page is UpcomingStaysNonNullResponseObject => !!page?.data)
// later on when we handle errors appropriately. .flatMap((page) => page.data)
const filteredStays = (data?.pages.filter((page) => page && page.data) ??
[]) as unknown as UpcomingStaysNonNullResponseObject[]
const stays = filteredStays.flatMap((page) => page.data)
return isLoading ? ( return stays.length ? (
<LoadingSpinner />
) : stays.length ? (
<ListContainer> <ListContainer>
<Grids.Stackable> <Grids.Stackable>
{stays.map((stay) => ( {stays.map((stay) => (