2024-02-12 16:46:51 -05:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
|
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-02-12 16:46:51 -05:00
|
|
|
|
2024-02-26 09:39:09 -05:00
|
|
|
import { ApiError } from '../client';
|
|
|
|
import ActionsMenu from '../components/Common/ActionsMenu';
|
|
|
|
import Navbar from '../components/Common/Navbar';
|
|
|
|
import useCustomToast from '../hooks/useCustomToast';
|
2024-02-15 17:17:26 -05:00
|
|
|
import { useItemsStore } from '../store/items-store';
|
2024-02-12 16:46:51 -05:00
|
|
|
|
|
|
|
const Items: React.FC = () => {
|
2024-02-26 09:39:09 -05:00
|
|
|
const showToast = useCustomToast();
|
2024-02-12 16:46:51 -05:00
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
const { items, getItems } = useItemsStore();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchItems = async () => {
|
2024-02-15 17:17:26 -05:00
|
|
|
setIsLoading(true);
|
2024-02-12 16:46:51 -05:00
|
|
|
try {
|
|
|
|
await getItems();
|
|
|
|
} catch (err) {
|
2024-02-26 09:39:09 -05:00
|
|
|
const errDetail = (err as ApiError).body.detail;
|
|
|
|
showToast('Something went wrong.', `${errDetail}`, 'error');
|
2024-02-15 17:17:26 -05:00
|
|
|
} finally {
|
|
|
|
setIsLoading(false);
|
2024-02-12 16:46:51 -05:00
|
|
|
}
|
|
|
|
}
|
2024-02-15 17:17:26 -05:00
|
|
|
if (items.length === 0) {
|
|
|
|
fetchItems();
|
|
|
|
}
|
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>
|
|
|
|
{items.map((item) => (
|
|
|
|
<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-02-26 09:39:09 -05:00
|
|
|
<ActionsMenu type={'Item'} id={item.id} />
|
2024-02-12 16:46:51 -05:00
|
|
|
</Td>
|
|
|
|
</Tr>
|
|
|
|
))}
|
|
|
|
</Tbody>
|
|
|
|
</Table>
|
|
|
|
</TableContainer>
|
|
|
|
</Container>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Items;
|