Use suspense for items page (#1167)

This commit is contained in:
Patrick Arminio
2024-04-15 22:03:39 +02:00
committed by GitHub
parent cc10bf6e31
commit c31a12c0fb
3 changed files with 99 additions and 63 deletions

View File

@@ -20,6 +20,7 @@
"framer-motion": "10.16.16", "framer-motion": "10.16.16",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
"react-error-boundary": "^4.0.13",
"react-hook-form": "7.49.3", "react-hook-form": "7.49.3",
"react-icons": "5.0.1" "react-icons": "5.0.1"
}, },
@@ -3409,6 +3410,17 @@
"react": "^18.2.0" "react": "^18.2.0"
} }
}, },
"node_modules/react-error-boundary": {
"version": "4.0.13",
"resolved": "https://registry.npmjs.org/react-error-boundary/-/react-error-boundary-4.0.13.tgz",
"integrity": "sha512-b6PwbdSv8XeOSYvjt8LpgpKrZ0yGdtZokYwkwV2wlcZbxgopHX/hgPl5VgpnoVOWd868n1hktM8Qm4b+02MiLQ==",
"dependencies": {
"@babel/runtime": "^7.12.5"
},
"peerDependencies": {
"react": ">=16.13.1"
}
},
"node_modules/react-fast-compare": { "node_modules/react-fast-compare": {
"version": "3.2.2", "version": "3.2.2",
"resolved": "https://registry.npmjs.org/react-fast-compare/-/react-fast-compare-3.2.2.tgz", "resolved": "https://registry.npmjs.org/react-fast-compare/-/react-fast-compare-3.2.2.tgz",
@@ -6108,6 +6120,14 @@
"scheduler": "^0.23.0" "scheduler": "^0.23.0"
} }
}, },
"react-error-boundary": {
"version": "4.0.13",
"resolved": "https://registry.npmjs.org/react-error-boundary/-/react-error-boundary-4.0.13.tgz",
"integrity": "sha512-b6PwbdSv8XeOSYvjt8LpgpKrZ0yGdtZokYwkwV2wlcZbxgopHX/hgPl5VgpnoVOWd868n1hktM8Qm4b+02MiLQ==",
"requires": {
"@babel/runtime": "^7.12.5"
}
},
"react-fast-compare": { "react-fast-compare": {
"version": "3.2.2", "version": "3.2.2",
"resolved": "https://registry.npmjs.org/react-fast-compare/-/react-fast-compare-3.2.2.tgz", "resolved": "https://registry.npmjs.org/react-fast-compare/-/react-fast-compare-3.2.2.tgz",

View File

@@ -23,6 +23,7 @@
"framer-motion": "10.16.16", "framer-motion": "10.16.16",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
"react-error-boundary": "^4.0.13",
"react-hook-form": "7.49.3", "react-hook-form": "7.49.3",
"react-icons": "5.0.1" "react-icons": "5.0.1"
}, },

View File

@@ -2,7 +2,7 @@ import {
Container, Container,
Flex, Flex,
Heading, Heading,
Spinner, Skeleton,
Table, Table,
TableContainer, TableContainer,
Tbody, Tbody,
@@ -11,82 +11,97 @@ import {
Thead, Thead,
Tr, Tr,
} from "@chakra-ui/react" } from "@chakra-ui/react"
import { useQuery } from "@tanstack/react-query" import { useSuspenseQuery } from "@tanstack/react-query"
import { createFileRoute } from "@tanstack/react-router" import { createFileRoute } from "@tanstack/react-router"
import { Suspense } from "react"
import { ErrorBoundary } from "react-error-boundary"
import { ItemsService } from "../../client" import { ItemsService } from "../../client"
import ActionsMenu from "../../components/Common/ActionsMenu" import ActionsMenu from "../../components/Common/ActionsMenu"
import Navbar from "../../components/Common/Navbar" import Navbar from "../../components/Common/Navbar"
import useCustomToast from "../../hooks/useCustomToast"
export const Route = createFileRoute("/_layout/items")({ export const Route = createFileRoute("/_layout/items")({
component: Items, component: Items,
}) })
function Items() { function ItemsTableBody() {
const showToast = useCustomToast() const { data: items } = useSuspenseQuery({
const {
data: items,
isLoading,
isError,
error,
} = useQuery({
queryKey: ["items"], queryKey: ["items"],
queryFn: () => ItemsService.readItems({}), queryFn: () => ItemsService.readItems({}),
}) })
if (isError) {
const errDetail = (error as any).body?.detail
showToast("Something went wrong.", `${errDetail}`, "error")
}
return ( return (
<> <Tbody>
{isLoading ? ( {items.data.map((item) => (
// TODO: Add skeleton <Tr key={item.id}>
<Flex justify="center" align="center" height="100vh" width="full"> <Td>{item.id}</Td>
<Spinner size="xl" color="ui.main" /> <Td>{item.title}</Td>
</Flex> <Td color={!item.description ? "ui.dim" : "inherit"}>
) : ( {item.description || "N/A"}
items && ( </Td>
<Container maxW="full"> <Td>
<Heading <ActionsMenu type={"Item"} value={item} />
size="lg" </Td>
textAlign={{ base: "center", md: "left" }} </Tr>
pt={12} ))}
> </Tbody>
Items Management )
</Heading> }
<Navbar type={"Item"} /> function ItemsTable() {
<TableContainer> return (
<Table size={{ base: "sm", md: "md" }}> <TableContainer>
<Thead> <Table size={{ base: "sm", md: "md" }}>
<Tr> <Thead>
<Th>ID</Th> <Tr>
<Th>Title</Th> <Th>ID</Th>
<Th>Description</Th> <Th>Title</Th>
<Th>Actions</Th> <Th>Description</Th>
</Tr> <Th>Actions</Th>
</Thead> </Tr>
<Tbody> </Thead>
{items.data.map((item) => ( <ErrorBoundary
<Tr key={item.id}> fallbackRender={({ error }) => (
<Td>{item.id}</Td> <Tbody>
<Td>{item.title}</Td> <Tr>
<Td color={!item.description ? "ui.dim" : "inherit"}> <Td colSpan={4}>Something went wrong: {error.message}</Td>
{item.description || "N/A"} </Tr>
</Td> </Tbody>
<Td> )}
<ActionsMenu type={"Item"} value={item} /> >
</Td> <Suspense
</Tr> fallback={
))} <Tbody>
</Tbody> {new Array(5).fill(null).map((_, index) => (
</Table> <Tr key={index}>
</TableContainer> {new Array(4).fill(null).map((_, index) => (
</Container> <Td key={index}>
) <Flex>
)} <Skeleton height="20px" width="20px" />
</> </Flex>
</Td>
))}
</Tr>
))}
</Tbody>
}
>
<ItemsTableBody />
</Suspense>
</ErrorBoundary>
</Table>
</TableContainer>
)
}
function Items() {
return (
<Container maxW="full">
<Heading size="lg" textAlign={{ base: "center", md: "left" }} pt={12}>
Items Management
</Heading>
<Navbar type={"Item"} />
<ItemsTable />
</Container>
) )
} }