2024-03-08 14:58:36 +01:00
|
|
|
import {
|
|
|
|
Badge,
|
|
|
|
Box,
|
2024-08-01 13:01:03 -05:00
|
|
|
Button,
|
2024-03-08 14:58:36 +01:00
|
|
|
Container,
|
|
|
|
Flex,
|
|
|
|
Heading,
|
2024-04-16 16:56:59 +02:00
|
|
|
SkeletonText,
|
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"
|
2024-08-01 13:01:03 -05:00
|
|
|
import { useQuery, useQueryClient } from "@tanstack/react-query"
|
|
|
|
import { createFileRoute, useNavigate } from "@tanstack/react-router"
|
|
|
|
import { useEffect } from "react"
|
|
|
|
import { z } from "zod"
|
2024-03-07 19:16:23 +01:00
|
|
|
|
2024-04-06 18:26:12 -05:00
|
|
|
import { type UserPublic, UsersService } from "../../client"
|
2024-07-21 19:33:50 -05:00
|
|
|
import AddUser from "../../components/Admin/AddUser"
|
2024-03-17 17:28:45 +01:00
|
|
|
import ActionsMenu from "../../components/Common/ActionsMenu"
|
|
|
|
import Navbar from "../../components/Common/Navbar"
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-08-01 13:01:03 -05:00
|
|
|
const usersSearchSchema = z.object({
|
|
|
|
page: z.number().catch(1),
|
|
|
|
})
|
|
|
|
|
2024-03-17 17:28:45 +01:00
|
|
|
export const Route = createFileRoute("/_layout/admin")({
|
2024-03-08 14:58:36 +01:00
|
|
|
component: Admin,
|
2024-08-01 13:01:03 -05:00
|
|
|
validateSearch: (search) => usersSearchSchema.parse(search),
|
2024-03-07 19:16:23 +01:00
|
|
|
})
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-08-01 13:01:03 -05:00
|
|
|
const PER_PAGE = 5
|
|
|
|
|
|
|
|
function getUsersQueryOptions({ page }: { page: number }) {
|
|
|
|
return {
|
|
|
|
queryFn: () =>
|
|
|
|
UsersService.readUsers({ skip: (page - 1) * PER_PAGE, limit: PER_PAGE }),
|
|
|
|
queryKey: ["users", { page }],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function UsersTable() {
|
2024-03-08 14:58:36 +01:00
|
|
|
const queryClient = useQueryClient()
|
2024-04-06 18:26:12 -05:00
|
|
|
const currentUser = queryClient.getQueryData<UserPublic>(["currentUser"])
|
2024-08-01 13:01:03 -05:00
|
|
|
const { page } = Route.useSearch()
|
|
|
|
const navigate = useNavigate({ from: Route.fullPath })
|
|
|
|
const setPage = (page: number) =>
|
|
|
|
navigate({ search: (prev) => ({ ...prev, page }) })
|
2024-04-16 16:56:59 +02:00
|
|
|
|
2024-08-01 13:01:03 -05:00
|
|
|
const {
|
|
|
|
data: users,
|
|
|
|
isPending,
|
|
|
|
isPlaceholderData,
|
|
|
|
} = useQuery({
|
|
|
|
...getUsersQueryOptions({ page }),
|
|
|
|
placeholderData: (prevData) => prevData,
|
2024-04-04 16:30:42 +02:00
|
|
|
})
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-08-01 13:01:03 -05:00
|
|
|
const hasNextPage = !isPlaceholderData && users?.data.length === PER_PAGE
|
|
|
|
const hasPreviousPage = page > 1
|
2024-04-16 16:56:59 +02:00
|
|
|
|
2024-08-01 13:01:03 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (hasNextPage) {
|
|
|
|
queryClient.prefetchQuery(getUsersQueryOptions({ page: page + 1 }))
|
|
|
|
}
|
|
|
|
}, [page, queryClient, hasNextPage])
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
return (
|
2024-08-01 13:01:03 -05:00
|
|
|
<>
|
2024-04-16 16:56:59 +02:00
|
|
|
<TableContainer>
|
2024-08-01 13:01:03 -05:00
|
|
|
<Table size={{ base: "sm", md: "md" }}>
|
2024-04-16 16:56:59 +02:00
|
|
|
<Thead>
|
|
|
|
<Tr>
|
|
|
|
<Th width="20%">Full name</Th>
|
|
|
|
<Th width="50%">Email</Th>
|
|
|
|
<Th width="10%">Role</Th>
|
|
|
|
<Th width="10%">Status</Th>
|
|
|
|
<Th width="10%">Actions</Th>
|
|
|
|
</Tr>
|
|
|
|
</Thead>
|
2024-08-01 13:01:03 -05:00
|
|
|
{isPending ? (
|
|
|
|
<Tbody>
|
|
|
|
<Tr>
|
|
|
|
{new Array(4).fill(null).map((_, index) => (
|
|
|
|
<Td key={index}>
|
|
|
|
<SkeletonText noOfLines={1} paddingBlock="16px" />
|
|
|
|
</Td>
|
|
|
|
))}
|
|
|
|
</Tr>
|
|
|
|
</Tbody>
|
|
|
|
) : (
|
|
|
|
<Tbody>
|
|
|
|
{users?.data.map((user) => (
|
|
|
|
<Tr key={user.id}>
|
|
|
|
<Td
|
|
|
|
color={!user.full_name ? "ui.dim" : "inherit"}
|
|
|
|
isTruncated
|
|
|
|
maxWidth="150px"
|
|
|
|
>
|
|
|
|
{user.full_name || "N/A"}
|
|
|
|
{currentUser?.id === user.id && (
|
|
|
|
<Badge ml="1" colorScheme="teal">
|
|
|
|
You
|
|
|
|
</Badge>
|
|
|
|
)}
|
|
|
|
</Td>
|
|
|
|
<Td isTruncated maxWidth="150px">
|
|
|
|
{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>
|
|
|
|
)}
|
2024-04-16 16:56:59 +02:00
|
|
|
</Table>
|
|
|
|
</TableContainer>
|
2024-08-01 13:01:03 -05:00
|
|
|
<Flex
|
|
|
|
gap={4}
|
|
|
|
alignItems="center"
|
|
|
|
mt={4}
|
|
|
|
direction="row"
|
|
|
|
justifyContent="flex-end"
|
|
|
|
>
|
|
|
|
<Button onClick={() => setPage(page - 1)} isDisabled={!hasPreviousPage}>
|
|
|
|
Previous
|
|
|
|
</Button>
|
|
|
|
<span>Page {page}</span>
|
|
|
|
<Button isDisabled={!hasNextPage} onClick={() => setPage(page + 1)}>
|
|
|
|
Next
|
|
|
|
</Button>
|
|
|
|
</Flex>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function Admin() {
|
|
|
|
return (
|
|
|
|
<Container maxW="full">
|
|
|
|
<Heading size="lg" textAlign={{ base: "center", md: "left" }} pt={12}>
|
|
|
|
Users Management
|
|
|
|
</Heading>
|
|
|
|
|
|
|
|
<Navbar type={"User"} addModalAs={AddUser} />
|
|
|
|
<UsersTable />
|
2024-04-16 16:56:59 +02:00
|
|
|
</Container>
|
2024-03-08 14:58:36 +01:00
|
|
|
)
|
2024-02-12 16:46:51 -05:00
|
|
|
}
|