Files
full-stack-fastapi-template/frontend/src/routes/_layout/items.tsx

108 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-03-08 14:58:36 +01:00
import {
Container,
Flex,
Heading,
Skeleton,
2024-03-08 14:58:36 +01:00
Table,
TableContainer,
Tbody,
Td,
Th,
Thead,
Tr,
2024-03-17 17:28:45 +01:00
} from "@chakra-ui/react"
import { useSuspenseQuery } from "@tanstack/react-query"
2024-04-08 15:49:22 -05:00
import { createFileRoute } from "@tanstack/react-router"
import { Suspense } from "react"
import { ErrorBoundary } from "react-error-boundary"
import { ItemsService } from "../../client"
2024-03-17 17:28:45 +01:00
import ActionsMenu from "../../components/Common/ActionsMenu"
import Navbar from "../../components/Common/Navbar"
2024-03-17 17:28:45 +01:00
export const Route = createFileRoute("/_layout/items")({
2024-03-08 14:58:36 +01:00
component: Items,
})
function ItemsTableBody() {
const { data: items } = useSuspenseQuery({
queryKey: ["items"],
queryFn: () => ItemsService.readItems({}),
})
2024-03-08 14:58:36 +01:00
return (
<Tbody>
{items.data.map((item) => (
<Tr key={item.id}>
<Td>{item.id}</Td>
<Td>{item.title}</Td>
<Td color={!item.description ? "ui.dim" : "inherit"}>
{item.description || "N/A"}
</Td>
<Td>
<ActionsMenu type={"Item"} value={item} />
</Td>
</Tr>
))}
</Tbody>
)
}
function ItemsTable() {
return (
<TableContainer>
<Table size={{ base: "sm", md: "md" }}>
<Thead>
<Tr>
<Th>ID</Th>
<Th>Title</Th>
<Th>Description</Th>
<Th>Actions</Th>
</Tr>
</Thead>
<ErrorBoundary
fallbackRender={({ error }) => (
<Tbody>
<Tr>
<Td colSpan={4}>Something went wrong: {error.message}</Td>
</Tr>
</Tbody>
)}
>
<Suspense
fallback={
<Tbody>
{new Array(5).fill(null).map((_, index) => (
<Tr key={index}>
{new Array(4).fill(null).map((_, index) => (
<Td key={index}>
<Flex>
<Skeleton height="20px" width="20px" />
</Flex>
2024-03-08 14:58:36 +01:00
</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>
2024-03-08 14:58:36 +01:00
)
}