Browse Source

🚸 Use useSuspenseQuery to fetch members and show skeleton (#1174)

pull/13907/head
Patrick Arminio 1 year ago
committed by GitHub
parent
commit
472e0f254b
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 165
      frontend/src/routes/_layout/admin.tsx

165
frontend/src/routes/_layout/admin.tsx

@ -4,7 +4,7 @@ import {
Container, Container,
Flex, Flex,
Heading, Heading,
Spinner, SkeletonText,
Table, Table,
TableContainer, TableContainer,
Tbody, Tbody,
@ -13,106 +13,103 @@ import {
Thead, Thead,
Tr, Tr,
} from "@chakra-ui/react" } from "@chakra-ui/react"
import { useQuery, useQueryClient } from "@tanstack/react-query" import { useQueryClient, useSuspenseQuery } from "@tanstack/react-query"
import { createFileRoute } from "@tanstack/react-router" import { createFileRoute } from "@tanstack/react-router"
import { Suspense } from "react"
import { type UserPublic, UsersService } from "../../client" import { type UserPublic, UsersService } 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/admin")({ export const Route = createFileRoute("/_layout/admin")({
component: Admin, component: Admin,
}) })
function Admin() { const MembersTableBody = () => {
const queryClient = useQueryClient() const queryClient = useQueryClient()
const showToast = useCustomToast()
const currentUser = queryClient.getQueryData<UserPublic>(["currentUser"]) const currentUser = queryClient.getQueryData<UserPublic>(["currentUser"])
const {
data: users, const { data: users } = useSuspenseQuery({
isLoading,
isError,
error,
} = useQuery({
queryKey: ["users"], queryKey: ["users"],
queryFn: () => UsersService.readUsers({}), queryFn: () => UsersService.readUsers({}),
}) })
if (isError) { return (
const errDetail = (error as any).body?.detail <Tbody>
showToast("Something went wrong.", `${errDetail}`, "error") {users.data.map((user) => (
} <Tr key={user.id}>
<Td color={!user.full_name ? "ui.dim" : "inherit"}>
{user.full_name || "N/A"}
{currentUser?.id === user.id && (
<Badge ml="1" colorScheme="teal">
You
</Badge>
)}
</Td>
<Td>{user.email}</Td>
<Td>{user.is_superuser ? "Superuser" : "User"}</Td>
<Td>
<Flex gap={2}>
<Box
w="2"
h="2"
borderRadius="50%"
bg={user.is_active ? "ui.success" : "ui.danger"}
alignSelf="center"
/>
{user.is_active ? "Active" : "Inactive"}
</Flex>
</Td>
<Td>
<ActionsMenu
type="User"
value={user}
disabled={currentUser?.id === user.id ? true : false}
/>
</Td>
</Tr>
))}
</Tbody>
)
}
const MembersBodySkeleton = () => {
return (
<Tbody>
<Tr>
{new Array(5).fill(null).map((_, index) => (
<Td key={index}>
<SkeletonText noOfLines={1} paddingBlock="16px" />
</Td>
))}
</Tr>
</Tbody>
)
}
function Admin() {
return ( return (
<> <Container maxW="full">
{isLoading ? ( <Heading size="lg" textAlign={{ base: "center", md: "left" }} pt={12}>
// TODO: Add skeleton User Management
<Flex justify="center" align="center" height="100vh" width="full"> </Heading>
<Spinner size="xl" color="ui.main" /> <Navbar type={"User"} />
</Flex> <TableContainer>
) : ( <Table fontSize="md" size={{ base: "sm", md: "md" }}>
users && ( <Thead>
<Container maxW="full"> <Tr>
<Heading <Th width="20%">Full name</Th>
size="lg" <Th width="50%">Email</Th>
textAlign={{ base: "center", md: "left" }} <Th width="10%">Role</Th>
pt={12} <Th width="10%">Status</Th>
> <Th width="10%">Actions</Th>
User Management </Tr>
</Heading> </Thead>
<Navbar type={"User"} /> <Suspense fallback={<MembersBodySkeleton />}>
<TableContainer> <MembersTableBody />
<Table fontSize="md" size={{ base: "sm", md: "md" }}> </Suspense>
<Thead> </Table>
<Tr> </TableContainer>
<Th>Full name</Th> </Container>
<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"}>
{user.full_name || "N/A"}
{currentUser?.id === user.id && (
<Badge ml="1" colorScheme="teal">
You
</Badge>
)}
</Td>
<Td>{user.email}</Td>
<Td>{user.is_superuser ? "Superuser" : "User"}</Td>
<Td>
<Flex gap={2}>
<Box
w="2"
h="2"
borderRadius="50%"
bg={user.is_active ? "ui.success" : "ui.danger"}
alignSelf="center"
/>
{user.is_active ? "Active" : "Inactive"}
</Flex>
</Td>
<Td>
<ActionsMenu
type="User"
value={user}
disabled={currentUser?.id === user.id ? true : false}
/>
</Td>
</Tr>
))}
</Tbody>
</Table>
</TableContainer>
</Container>
)
)}
</>
) )
} }

Loading…
Cancel
Save