-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor virtualized list screen and add car list
error and item components
- Loading branch information
Timothy Miller
committed
Nov 5, 2023
1 parent
4c6d992
commit 3b0b216
Showing
4 changed files
with
63 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,30 @@ | ||
import type { Car } from '@t4/api/src/db/schema' | ||
import { Paragraph, Spinner, VirtualList, YStack } from '@t4/ui' | ||
import { formatNumber, formatPrice } from 'app/utils/number' | ||
import { CarListError } from '@t4/ui/src/cars/CarListError' | ||
import { CarListItem } from '@t4/ui/src/cars/CarListItem' | ||
import { trpc } from 'app/utils/trpc' | ||
import { empty, error, loading, success } from 'app/utils/trpc/patterns' | ||
import React from 'react' | ||
import { SolitoImage } from 'solito/image' | ||
import { match } from 'ts-pattern' | ||
|
||
export const VirtualizedListScreen = (): React.ReactNode => { | ||
const query = trpc.car.all.useQuery() | ||
const carsList = trpc.car.all.useQuery() | ||
const carsListLayout = match(carsList) | ||
.with(error, () => <CarListError message={carsList.failureReason?.message} />) | ||
.with(loading, () => ( | ||
<YStack fullscreen f={1} jc="center" ai="center"> | ||
<Paragraph pb="$3">Loading...</Paragraph> | ||
<Spinner /> | ||
</YStack> | ||
)) | ||
.with(empty, () => <Paragraph>No cars found.</Paragraph>) | ||
.with(success, () => ( | ||
<VirtualList data={carsList.data as any[]} renderItem={CarListItem} itemHeight={80} /> | ||
)) | ||
.otherwise(() => <CarListError message={carsList.failureReason?.message} />) | ||
|
||
return ( | ||
<YStack fullscreen f={1}> | ||
{query.isInitialLoading && !query.error ? ( | ||
// Loading | ||
<YStack fullscreen f={1} jc="center" ai="center"> | ||
<Paragraph pb="$3">Loading...</Paragraph> | ||
<Spinner /> | ||
</YStack> | ||
) : query.data?.length ? ( | ||
// Has Data | ||
<VirtualList data={query.data} renderItem={CarListItem} itemHeight={80} /> | ||
) : query.error ? ( | ||
// Error | ||
<YStack fullscreen f={1} jc="center" ai="center" p="$6"> | ||
<Paragraph pb="$3">Error fetching cars</Paragraph> | ||
<Paragraph>{query.error.message}</Paragraph> | ||
</YStack> | ||
) : ( | ||
// Empty State | ||
<YStack fullscreen f={1} jc="center" ai="center"> | ||
<Paragraph>No cars found.</Paragraph> | ||
</YStack> | ||
)} | ||
</YStack> | ||
) | ||
} | ||
|
||
const CarListItem = (item: Car): React.ReactElement => { | ||
return ( | ||
<YStack flexDirection="row" paddingLeft="$2"> | ||
<SolitoImage | ||
src="/t4-logo.png" | ||
width={56} | ||
height={56} | ||
alt="T4 Logo" | ||
style={{ | ||
marginTop: 8, | ||
}} | ||
/> | ||
<YStack> | ||
<Paragraph paddingTop="$2" paddingLeft="$3" paddingBottom="$1" fontSize={16}> | ||
{item.make + ' ' + item.model} | ||
</Paragraph> | ||
<Paragraph paddingLeft="$3" fontSize={16} opacity={0.6}> | ||
{item.color} - {item.year} - {formatNumber(item.mileage)} miles -{' '} | ||
{formatPrice(item.price)} | ||
</Paragraph> | ||
</YStack> | ||
{carsListLayout} | ||
</YStack> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Paragraph, YStack } from 'tamagui' | ||
|
||
interface Props { | ||
message: string | undefined | ||
} | ||
|
||
export const CarListError = ({ message }: Props): React.ReactElement => { | ||
return ( | ||
<YStack fullscreen f={1} jc="center" ai="center" p="$6"> | ||
<Paragraph pb="$3">Error fetching cars</Paragraph> | ||
<Paragraph>{message}</Paragraph> | ||
</YStack> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { formatNumber, formatPrice } from 'app/utils/number' | ||
import type { Car } from '@t4/api/src/db/schema' | ||
import { SolitoImage } from 'solito/image' | ||
import { Paragraph, YStack } from 'tamagui' | ||
|
||
export const CarListItem = (item: Car): React.ReactElement => { | ||
return ( | ||
<YStack flexDirection="row" paddingLeft="$2"> | ||
<SolitoImage | ||
src="/t4-logo.png" | ||
width={56} | ||
height={56} | ||
alt="T4 Logo" | ||
style={{ | ||
marginTop: 8, | ||
}} | ||
/> | ||
<YStack> | ||
<Paragraph paddingTop="$2" paddingLeft="$3" paddingBottom="$1" fontSize={16}> | ||
{item.make + ' ' + item.model} | ||
</Paragraph> | ||
<Paragraph paddingLeft="$3" fontSize={16} opacity={0.6}> | ||
{item.color} - {item.year} - {formatNumber(item.mileage)} miles -{' '} | ||
{formatPrice(item.price)} | ||
</Paragraph> | ||
</YStack> | ||
</YStack> | ||
) | ||
} |
3b0b216
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✔ EAS production build completed