2024-02-26 09:39:09 -05:00
|
|
|
import { 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 } from 'react-query';
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-03-07 19:16:23 +01:00
|
|
|
import { ApiError, ItemsService } 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/items')({
|
|
|
|
component: Items,
|
|
|
|
})
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-03-07 19:16:23 +01:00
|
|
|
function Items() {
|
|
|
|
const showToast = useCustomToast();
|
|
|
|
const { data: items, isLoading, isError, error } = useQuery('items', () => ItemsService.readItems({}))
|
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>
|
|
|
|
) : (
|
|
|
|
items &&
|
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
|
|
|
Items Management
|
|
|
|
</Heading>
|
2024-02-26 09:39:09 -05:00
|
|
|
<Navbar type={'Item'} />
|
2024-02-12 16:46:51 -05:00
|
|
|
<TableContainer>
|
2024-02-26 09:39:09 -05:00
|
|
|
<Table size={{ base: 'sm', md: 'md' }}>
|
2024-02-12 16:46:51 -05:00
|
|
|
<Thead>
|
|
|
|
<Tr>
|
|
|
|
<Th>ID</Th>
|
|
|
|
<Th>Title</Th>
|
|
|
|
<Th>Description</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
|
|
|
{items.data.map((item) => (
|
2024-02-12 16:46:51 -05:00
|
|
|
<Tr key={item.id}>
|
|
|
|
<Td>{item.id}</Td>
|
|
|
|
<Td>{item.title}</Td>
|
2024-02-26 09:39:09 -05:00
|
|
|
<Td color={!item.description ? 'gray.600' : 'inherit'}>{item.description || 'N/A'}</Td>
|
2024-02-12 16:46:51 -05:00
|
|
|
<Td>
|
2024-03-07 19:16:23 +01:00
|
|
|
<ActionsMenu type={'Item'} value={item} />
|
2024-02-12 16:46:51 -05:00
|
|
|
</Td>
|
|
|
|
</Tr>
|
|
|
|
))}
|
|
|
|
</Tbody>
|
|
|
|
</Table>
|
|
|
|
</TableContainer>
|
|
|
|
</Container>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Items;
|