2024-03-08 14:58:36 +01:00
|
|
|
import {
|
|
|
|
Container,
|
|
|
|
Flex,
|
|
|
|
Heading,
|
|
|
|
Spinner,
|
|
|
|
Table,
|
|
|
|
TableContainer,
|
|
|
|
Tbody,
|
|
|
|
Td,
|
|
|
|
Th,
|
|
|
|
Thead,
|
|
|
|
Tr,
|
|
|
|
} from '@chakra-ui/react'
|
|
|
|
import { createFileRoute } from '@tanstack/react-router'
|
|
|
|
import { useQuery } from 'react-query'
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-03-08 14:58:36 +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')({
|
2024-03-08 14:58:36 +01:00
|
|
|
component: Items,
|
2024-03-07 19:16:23 +01:00
|
|
|
})
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-03-07 19:16:23 +01:00
|
|
|
function Items() {
|
2024-03-08 14:58:36 +01:00
|
|
|
const showToast = useCustomToast()
|
|
|
|
const {
|
|
|
|
data: items,
|
|
|
|
isLoading,
|
|
|
|
isError,
|
|
|
|
error,
|
|
|
|
} = useQuery('items', () => ItemsService.readItems({}))
|
2024-02-12 16:46:51 -05:00
|
|
|
|
2024-03-08 14:58:36 +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
|
|
|
|
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>
|
|
|
|
) : (
|
|
|
|
items && (
|
|
|
|
<Container maxW="full">
|
|
|
|
<Heading
|
|
|
|
size="lg"
|
|
|
|
textAlign={{ base: 'center', md: 'left' }}
|
|
|
|
pt={12}
|
|
|
|
>
|
|
|
|
Items Management
|
|
|
|
</Heading>
|
|
|
|
<Navbar type={'Item'} />
|
|
|
|
<TableContainer>
|
|
|
|
<Table size={{ base: 'sm', md: 'md' }}>
|
|
|
|
<Thead>
|
|
|
|
<Tr>
|
|
|
|
<Th>ID</Th>
|
|
|
|
<Th>Title</Th>
|
|
|
|
<Th>Description</Th>
|
|
|
|
<Th>Actions</Th>
|
|
|
|
</Tr>
|
|
|
|
</Thead>
|
|
|
|
<Tbody>
|
|
|
|
{items.data.map((item) => (
|
|
|
|
<Tr key={item.id}>
|
|
|
|
<Td>{item.id}</Td>
|
|
|
|
<Td>{item.title}</Td>
|
2024-03-11 16:50:46 +01:00
|
|
|
<Td color={!item.description ? 'gray.400' : 'inherit'}>
|
2024-03-08 14:58:36 +01:00
|
|
|
{item.description || 'N/A'}
|
|
|
|
</Td>
|
|
|
|
<Td>
|
|
|
|
<ActionsMenu type={'Item'} value={item} />
|
|
|
|
</Td>
|
|
|
|
</Tr>
|
|
|
|
))}
|
|
|
|
</Tbody>
|
|
|
|
</Table>
|
|
|
|
</TableContainer>
|
|
|
|
</Container>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
2024-02-12 16:46:51 -05:00
|
|
|
}
|
|
|
|
|
2024-03-08 14:58:36 +01:00
|
|
|
export default Items
|