adminFront/src/pages/dashboard/Content/Support/Support.tsx

151 lines
5.3 KiB
TypeScript
Raw Normal View History

2023-03-20 15:27:42 +00:00
import { useEffect } from "react";
import { Box, Button, useTheme } from "@mui/material";
import SearchOutlinedIcon from '@mui/icons-material/SearchOutlined';
import HighlightOffOutlinedIcon from '@mui/icons-material/HighlightOffOutlined';
import { enqueueSnackbar } from 'notistack';
2023-03-21 12:59:46 +00:00
import { GetTicketsRequest, Ticket } from "@root/model/ticket";
2023-03-20 15:27:42 +00:00
import { setTickets, addOrUpdateTicket, useTicketStore } from "@root/stores/tickets";
import TicketItem from "./TicketItem";
2023-03-21 12:59:46 +00:00
import { getTickets, subscribeToAllTickets } from "@root/api/tickets";
2023-03-20 15:27:42 +00:00
import Chat from "./Chat";
export default function Support() {
const theme = useTheme();
const tickets = useTicketStore(state => state.tickets);
useEffect(function fetchTickets() {
const getTicketsBody: GetTicketsRequest = {
amt: 10,
page: 0,
status: "open",
};
const controller = new AbortController();
2023-03-21 14:43:21 +00:00
setTickets([]);
2023-03-21 12:59:46 +00:00
getTickets({
2023-03-20 15:27:42 +00:00
body: getTicketsBody,
signal: controller.signal,
2023-03-21 12:59:46 +00:00
}).then(result => {
2023-03-20 15:27:42 +00:00
console.log("GetTicketsResponse", result);
setTickets(result.data);
}).catch(error => {
console.log("Error fetching tickets", error);
enqueueSnackbar(error.message);
});
return () => controller.abort();
}, []);
useEffect(function subscribeToTickets() {
const token = localStorage.getItem("AT");
if (!token) return;
const unsubscribe = subscribeToAllTickets({
accessToken: token,
onMessage(event) {
2023-03-21 12:59:46 +00:00
console.log("SSE: ticket received:", event.data);
2023-03-20 15:27:42 +00:00
try {
const newTicket = JSON.parse(event.data) as Ticket;
addOrUpdateTicket(newTicket);
} catch (error) {
2023-03-21 12:59:46 +00:00
console.log("Error parsing ticket SSE", error);
2023-03-20 15:27:42 +00:00
}
},
onError(event) {
console.log("SSE Error:", event);
}
});
return () => {
unsubscribe();
};
}, []);
2023-03-21 14:43:21 +00:00
const sortedTickets = tickets.sort(sortTicketsByUpdateTime).sort(sortTicketsByUnread);
2023-03-20 15:27:42 +00:00
return (
<Box sx={{
width: "100%",
display: "flex",
2023-03-21 12:59:46 +00:00
justifyContent: "space-between",
height: "600px",
2023-03-20 15:27:42 +00:00
}}>
<Chat />
<Box sx={{
width: "max(40%, 460px)",
display: "flex",
flexDirection: "column",
alignItems: "center",
}}>
<Box sx={{
width: "100%",
border: "1px solid",
borderColor: theme.palette.grayDark.main,
borderRadius: "3px",
padding: "10px"
}}>
<Button
variant="contained"
sx={{
backgroundColor: theme.palette.grayDark.main,
width: "100%",
height: "45px",
fontSize: "15px",
fontWeight: "normal",
textTransform: "capitalize",
"&:hover": {
backgroundColor: theme.palette.menu.main
}
}}>
Поиск
<SearchOutlinedIcon />
</Button>
<Button
variant="text"
sx={{
width: "100%",
height: "35px",
fontSize: "14px",
fontWeight: "normal",
color: theme.palette.secondary.main,
border: "1px solid",
borderColor: theme.palette.golden.main,
borderRadius: 0,
"&:hover": {
backgroundColor: theme.palette.menu.main
}
}}>
ЗАКРЫТЬ ТИКЕТ
<HighlightOffOutlinedIcon />
</Button>
</Box>
<Box sx={{
width: "100%",
border: "1px solid",
borderColor: theme.palette.grayDark.main,
borderRadius: "3px",
overflow: "auto",
overflowY: "auto",
padding: "10px"
}}>
2023-03-21 14:43:21 +00:00
{sortedTickets.map(ticket =>
<TicketItem ticket={ticket} key={ticket.id} isUnread={ticket.user === ticket.top_message.user_id} />
2023-03-20 15:27:42 +00:00
)}
</Box>
</Box>
</Box>
);
2023-03-21 14:43:21 +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;
}
function sortTicketsByUnread(ticket1: Ticket, ticket2: Ticket) {
const isUnread1 = ticket1.user === ticket1.top_message.user_id;
const isUnread2 = ticket2.user === ticket2.top_message.user_id;
return Number(isUnread2) - Number(isUnread1);
2023-03-20 15:27:42 +00:00
}