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

78 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-12-22 12:43:14 +00:00
import { CircularProgress, List, ListItem, Box, useTheme, Pagination } from "@mui/material";
import TicketCard from "./TicketCard";
2023-04-06 15:10:10 +00:00
import { setTicketApiPage, useTicketStore } from "@root/stores/tickets";
2023-03-31 15:01:43 +00:00
import { Ticket } from "@root/model/ticket";
2022-12-22 12:43:14 +00:00
2023-03-30 13:17:06 +00:00
2022-12-22 12:43:14 +00:00
export default function TicketList() {
2023-03-30 13:17:06 +00:00
const theme = useTheme();
2023-03-31 15:01:43 +00:00
const tickets = useTicketStore(state => state.tickets);
2023-04-07 13:32:30 +00:00
const ticketsFetchState = useTicketStore(state => state.fetchState);
2023-03-31 15:01:43 +00:00
const ticketCount = useTicketStore(state => state.ticketCount);
2023-04-06 15:10:10 +00:00
const ticketApiPage = useTicketStore(state => state.apiPage);
2023-03-31 15:01:43 +00:00
const ticketsPerPage = useTicketStore(state => state.ticketsPerPage);
2022-12-22 12:43:14 +00:00
2023-04-06 15:10:10 +00:00
const sortedTickets = tickets.sort(sortTicketsByUpdateTime).slice(ticketApiPage * ticketsPerPage, (ticketApiPage + 1) * ticketsPerPage);
2022-12-22 12:43:14 +00:00
2023-03-30 13:17:06 +00:00
return (
<Box
sx={{
2023-03-30 13:17:06 +00:00
display: "flex",
gap: "40px",
flexDirection: "column",
}}
2023-03-30 13:17:06 +00:00
>
<List
sx={{
p: 0,
display: "flex",
flexDirection: "column",
gap: "40px",
2023-04-07 13:32:30 +00:00
opacity: ticketsFetchState === "fetching" ? 0.4 : 1,
2023-03-30 13:17:06 +00:00
transitionProperty: "opacity",
transitionDuration: "200ms",
}}
>
2023-03-31 15:01:43 +00:00
{sortedTickets.map((ticket) => (
2023-03-30 13:17:06 +00:00
<ListItem key={ticket.id} disablePadding>
<TicketCard
name={ticket.title}
body={ticket.top_message.message}
time={new Date(ticket.updated_at).toLocaleDateString()}
ticketId={ticket.id}
/>
</ListItem>
))}
2023-04-07 13:32:30 +00:00
{ticketsFetchState === "fetching" && (
2023-03-30 13:17:06 +00:00
<Box
sx={{
position: "absolute",
width: "100%",
height: "100%",
minHeight: "80px",
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<CircularProgress sx={{ color: theme.palette.brightPurple.main }} size={60} />
</Box>
)}
</List>
2023-03-31 15:01:43 +00:00
{ticketCount > ticketsPerPage &&
2023-03-30 13:17:06 +00:00
<Pagination
2023-03-31 15:01:43 +00:00
count={Math.ceil(ticketCount / ticketsPerPage)}
2023-04-06 15:10:10 +00:00
page={ticketApiPage + 1}
onChange={(e, value) => setTicketApiPage(value - 1)}
2023-03-30 13:17:06 +00:00
sx={{ alignSelf: "center" }}
/>
2023-03-31 15:01:43 +00:00
}
2023-03-30 13:17:06 +00:00
</Box>
);
}
2023-03-31 15:01:43 +00:00
function sortTicketsByUpdateTime(ticket1: Ticket, ticket2: Ticket) {
const date1 = new Date(ticket1.updated_at).getTime();
const date2 = new Date(ticket2.updated_at).getTime();
return date2 - date1;
}