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

93 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-03-08 14:58:36 +01:00
import {
Container,
Flex,
Heading,
Spinner,
Table,
TableContainer,
Tbody,
Td,
Th,
Thead,
Tr,
2024-03-17 17:28:45 +01:00
} from "@chakra-ui/react"
import { useQuery } from "@tanstack/react-query"
2024-04-08 15:49:22 -05:00
import { createFileRoute } from "@tanstack/react-router"
import { ItemsService } from "../../client"
2024-03-17 17:28:45 +01:00
import ActionsMenu from "../../components/Common/ActionsMenu"
import Navbar from "../../components/Common/Navbar"
import useCustomToast from "../../hooks/useCustomToast"
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 Items() {
2024-03-08 14:58:36 +01:00
const showToast = useCustomToast()
const {
data: items,
isLoading,
isError,
error,
} = useQuery({
queryKey: ["items"],
queryFn: () => ItemsService.readItems({}),
})
2024-03-08 14:58:36 +01:00
if (isError) {
const errDetail = (error as any).body?.detail
2024-03-17 17:28:45 +01:00
showToast("Something went wrong.", `${errDetail}`, "error")
2024-03-08 14:58:36 +01:00
}
2024-03-08 14:58:36 +01:00
return (
<>
{isLoading ? (
// TODO: Add skeleton
<Flex justify="center" align="center" height="100vh" width="full">
<Spinner size="xl" color="ui.main" />
</Flex>
) : (
items && (
<Container maxW="full">
<Heading
size="lg"
2024-03-17 17:28:45 +01:00
textAlign={{ base: "center", md: "left" }}
2024-03-08 14:58:36 +01:00
pt={12}
>
Items Management
</Heading>
2024-03-17 17:28:45 +01:00
<Navbar type={"Item"} />
2024-03-08 14:58:36 +01:00
<TableContainer>
2024-03-17 17:28:45 +01:00
<Table size={{ base: "sm", md: "md" }}>
2024-03-08 14:58:36 +01:00
<Thead>
<Tr>
<Th>ID</Th>
<Th>Title</Th>
<Th>Description</Th>
<Th>Actions</Th>
</Tr>
</Thead>
<Tbody>
{items.data.map((item) => (
<Tr key={item.id}>
<Td>{item.id}</Td>
<Td>{item.title}</Td>
<Td color={!item.description ? "ui.dim" : "inherit"}>
2024-03-17 17:28:45 +01:00
{item.description || "N/A"}
2024-03-08 14:58:36 +01:00
</Td>
<Td>
2024-03-17 17:28:45 +01:00
<ActionsMenu type={"Item"} value={item} />
2024-03-08 14:58:36 +01:00
</Td>
</Tr>
))}
</Tbody>
</Table>
</TableContainer>
</Container>
)
)}
</>
)
}