front-hub/src/pages/Support/TicketList/TicketList.tsx

98 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-08-03 12:14:31 +00:00
import {
2023-08-29 11:54:35 +00:00
CircularProgress,
List,
ListItem,
Box,
useTheme,
Pagination,
Typography,
2023-08-03 12:14:31 +00:00
} from "@mui/material";
2022-12-22 12:43:14 +00:00
import TicketCard from "./TicketCard";
2023-04-06 15:10:10 +00:00
import { setTicketApiPage, useTicketStore } from "@root/stores/tickets";
2023-06-06 13:13:58 +00:00
import { Ticket } from "@frontend/kitui";
import { withErrorBoundary } from "react-error-boundary";
import { handleComponentError } from "@root/utils/handleComponentError";
2022-12-22 12:43:14 +00:00
2023-06-06 13:13:58 +00:00
function TicketList() {
2023-08-29 11:54:35 +00:00
const theme = useTheme();
const tickets = useTicketStore((state) => state.tickets);
const ticketCount = useTicketStore((state) => state.ticketCount);
const ticketApiPage = useTicketStore((state) => state.apiPage);
const ticketsPerPage = useTicketStore((state) => state.ticketsPerPage);
const fetchState = useTicketStore(state => state.ticketsFetchState);
2022-12-22 12:43:14 +00:00
2023-08-29 11:54:35 +00:00
const sortedTickets = tickets
.sort(sortTicketsByUpdateTime)
.slice(
ticketApiPage * ticketsPerPage,
(ticketApiPage + 1) * ticketsPerPage
);
2022-12-22 12:43:14 +00:00
2023-08-29 11:54:35 +00:00
return (
<Box
sx={{
2023-08-29 11:54:35 +00:00
display: "flex",
gap: "40px",
flexDirection: "column",
}}
2023-08-29 11:54:35 +00:00
>
<List
sx={{
p: 0,
minHeight: "120px",
display: "flex",
flexDirection: "column",
gap: "40px",
opacity: fetchState === "fetching" ? 0.4 : 1,
transitionProperty: "opacity",
transitionDuration: "200ms",
}}
>
{sortedTickets.map((ticket) => (
<ListItem key={ticket.id} disablePadding>
<TicketCard ticket={ticket} />
</ListItem>
))}
{fetchState === "fetching" && (
<Box
sx={{
position: "absolute",
width: "100%",
height: "100%",
minHeight: "120px",
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<CircularProgress
sx={{ color: theme.palette.purple.main }}
size={60}
/>
</Box>
)}
</List>
{ticketCount > ticketsPerPage && (
<Pagination
count={Math.ceil(ticketCount / ticketsPerPage)}
page={ticketApiPage + 1}
onChange={(e, value) => setTicketApiPage(value - 1)}
sx={{ alignSelf: "center" }}
/>
)}
</Box>
);
}
2023-03-31 15:01:43 +00:00
function sortTicketsByUpdateTime(ticket1: Ticket, ticket2: Ticket) {
2023-08-29 11:54:35 +00:00
const date1 = new Date(ticket1.updated_at).getTime();
const date2 = new Date(ticket2.updated_at).getTime();
return date2 - date1;
2023-08-03 12:14:31 +00:00
}
export default withErrorBoundary(TicketList, {
fallback: <Typography mt="8px" textAlign="center">Ошибка загрузки тикетов</Typography>,
onError: handleComponentError,
});