2024-02-26 09:39:09 -05:00
|
|
|
import { Badge, Box, Container, Flex, Heading, Spinner, Table, TableContainer, Tbody, Td, Th, Thead, Tr } from '@chakra-ui/react';
|
2024-03-07 19:16:23 +01:00
|
|
|
import { createFileRoute } from '@tanstack/react-router';
|
|
|
|
import { useQuery, useQueryClient } from 'react-query';
|
|
|
|
|
|
|
|
import { ApiError, UserOut, UsersService } from '../../client';
|
|
|
|
import ActionsMenu from '../../components/Common/ActionsMenu';
|
|
|
|
import Navbar from '../../components/Common/Navbar';
|
|
|
|
import useCustomToast from '../../hooks/useCustomToast';
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-03-07 19:16:23 +01:00
|
|
|
export const Route = createFileRoute('/_layout/admin')({
|
|
|
|
component: Admin,
|
|
|
|
})
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-03-07 19:16:23 +01:00
|
|
|
function Admin() {
|
|
|
|
const queryClient = useQueryClient();
|
2024-02-26 09:39:09 -05:00
|
|
|
const showToast = useCustomToast();
|
2024-03-07 19:16:23 +01:00
|
|
|
const currentUser = queryClient.getQueryData<UserOut>('currentUser');
|
|
|
|
const { data: users, isLoading, isError, error } = useQuery('users', () => UsersService.readUsers({}))
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-03-07 19:16:23 +01:00
|
|
|
if (isError) {
|
|
|
|
const errDetail = (error as ApiError).body?.detail;
|
|
|
|
showToast('Something went wrong.', `${errDetail}`, 'error');
|
|
|
|
}
|
2024-02-12 16:46:51 -05:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{isLoading ? (
|
|
|
|
// TODO: Add skeleton
|
2024-02-26 09:39:09 -05:00
|
|
|
<Flex justify='center' align='center' height='100vh' width='full'>
|
|
|
|
<Spinner size='xl' color='ui.main' />
|
2024-02-12 16:46:51 -05:00
|
|
|
</Flex>
|
|
|
|
) : (
|
|
|
|
users &&
|
2024-02-26 09:39:09 -05:00
|
|
|
<Container maxW='full'>
|
|
|
|
<Heading size='lg' textAlign={{ base: 'center', md: 'left' }} pt={12}>
|
2024-02-12 16:46:51 -05:00
|
|
|
User Management
|
|
|
|
</Heading>
|
2024-02-26 09:39:09 -05:00
|
|
|
<Navbar type={'User'} />
|
2024-02-12 16:46:51 -05:00
|
|
|
<TableContainer>
|
2024-02-26 09:39:09 -05:00
|
|
|
<Table fontSize='md' size={{ base: 'sm', md: 'md' }}>
|
2024-02-12 16:46:51 -05:00
|
|
|
<Thead>
|
|
|
|
<Tr>
|
|
|
|
<Th>Full name</Th>
|
|
|
|
<Th>Email</Th>
|
|
|
|
<Th>Role</Th>
|
|
|
|
<Th>Status</Th>
|
2024-02-15 17:17:26 -05:00
|
|
|
<Th>Actions</Th>
|
2024-02-12 16:46:51 -05:00
|
|
|
</Tr>
|
|
|
|
</Thead>
|
|
|
|
<Tbody>
|
2024-03-07 19:16:23 +01:00
|
|
|
{users.data.map((user) => (
|
2024-02-12 16:46:51 -05:00
|
|
|
<Tr key={user.id}>
|
2024-02-26 09:39:09 -05:00
|
|
|
<Td color={!user.full_name ? 'gray.600' : 'inherit'}>{user.full_name || 'N/A'}{currentUser?.id === user.id && <Badge ml='1' colorScheme='teal'>You</Badge>}</Td>
|
2024-02-12 16:46:51 -05:00
|
|
|
<Td>{user.email}</Td>
|
2024-02-26 09:39:09 -05:00
|
|
|
<Td>{user.is_superuser ? 'Superuser' : 'User'}</Td>
|
2024-02-12 16:46:51 -05:00
|
|
|
<Td>
|
|
|
|
<Flex gap={2}>
|
|
|
|
<Box
|
2024-02-26 09:39:09 -05:00
|
|
|
w='2'
|
|
|
|
h='2'
|
|
|
|
borderRadius='50%'
|
|
|
|
bg={user.is_active ? 'ui.success' : 'ui.danger'}
|
|
|
|
alignSelf='center'
|
2024-02-12 16:46:51 -05:00
|
|
|
/>
|
2024-02-26 09:39:09 -05:00
|
|
|
{user.is_active ? 'Active' : 'Inactive'}
|
2024-02-12 16:46:51 -05:00
|
|
|
</Flex>
|
|
|
|
</Td>
|
|
|
|
<Td>
|
2024-03-07 19:16:23 +01:00
|
|
|
<ActionsMenu type='User' value={user} disabled={currentUser?.id === user.id ? true : false} />
|
2024-02-12 16:46:51 -05:00
|
|
|
</Td>
|
|
|
|
</Tr>
|
|
|
|
))}
|
|
|
|
</Tbody>
|
|
|
|
</Table>
|
|
|
|
</TableContainer>
|
|
|
|
</Container>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Admin;
|