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

119 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-03-08 14:58:36 +01:00
import {
Badge,
Box,
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, useQueryClient } from "@tanstack/react-query"
2024-04-08 15:49:22 -05:00
import { createFileRoute } from "@tanstack/react-router"
import { type UserPublic, UsersService } 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/admin")({
2024-03-08 14:58:36 +01:00
component: Admin,
})
function Admin() {
2024-03-08 14:58:36 +01:00
const queryClient = useQueryClient()
const showToast = useCustomToast()
const currentUser = queryClient.getQueryData<UserPublic>(["currentUser"])
2024-03-08 14:58:36 +01:00
const {
data: users,
isLoading,
isError,
error,
} = useQuery({
queryKey: ["users"],
queryFn: () => UsersService.readUsers({}),
})
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>
) : (
users && (
<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}
>
User Management
</Heading>
2024-03-17 17:28:45 +01:00
<Navbar type={"User"} />
2024-03-08 14:58:36 +01:00
<TableContainer>
2024-03-17 17:28:45 +01:00
<Table fontSize="md" size={{ base: "sm", md: "md" }}>
2024-03-08 14:58:36 +01:00
<Thead>
<Tr>
<Th>Full name</Th>
<Th>Email</Th>
<Th>Role</Th>
<Th>Status</Th>
<Th>Actions</Th>
</Tr>
</Thead>
<Tbody>
{users.data.map((user) => (
<Tr key={user.id}>
<Td color={!user.full_name ? "ui.dim" : "inherit"}>
2024-03-17 17:28:45 +01:00
{user.full_name || "N/A"}
2024-03-08 14:58:36 +01:00
{currentUser?.id === user.id && (
<Badge ml="1" colorScheme="teal">
You
</Badge>
)}
</Td>
<Td>{user.email}</Td>
2024-03-17 17:28:45 +01:00
<Td>{user.is_superuser ? "Superuser" : "User"}</Td>
2024-03-08 14:58:36 +01:00
<Td>
<Flex gap={2}>
<Box
w="2"
h="2"
borderRadius="50%"
2024-03-17 17:28:45 +01:00
bg={user.is_active ? "ui.success" : "ui.danger"}
2024-03-08 14:58:36 +01:00
alignSelf="center"
/>
2024-03-17 17:28:45 +01:00
{user.is_active ? "Active" : "Inactive"}
2024-03-08 14:58:36 +01:00
</Flex>
</Td>
<Td>
<ActionsMenu
type="User"
value={user}
disabled={currentUser?.id === user.id ? true : false}
/>
</Td>
</Tr>
))}
</Tbody>
</Table>
</TableContainer>
</Container>
)
)}
</>
)
}