2023-03-23 11:32:09 +00:00
|
|
|
import { Box, useMediaQuery, useTheme } from "@mui/material";
|
2023-03-29 10:57:33 +00:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
2023-03-29 10:31:02 +00:00
|
|
|
import Chat from "./Chat/Chat";
|
2023-03-23 11:32:09 +00:00
|
|
|
import Collapse from "./Collapse";
|
2023-03-29 10:31:02 +00:00
|
|
|
import TicketList from "./TicketList/TicketList";
|
2023-03-29 10:57:33 +00:00
|
|
|
import { getTickets, subscribeToAllTickets } from "@root/api/tickets";
|
|
|
|
import { GetTicketsRequest, Ticket } from "@root/model/ticket";
|
|
|
|
import { updateTickets } from "@root/stores/tickets";
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
2023-03-20 15:27:42 +00:00
|
|
|
|
|
|
|
|
2023-03-29 10:57:33 +00:00
|
|
|
const TICKETS_PER_PAGE = 20;
|
|
|
|
|
2023-03-20 15:27:42 +00:00
|
|
|
export default function Support() {
|
2023-03-23 11:32:09 +00:00
|
|
|
const theme = useTheme();
|
|
|
|
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
2023-03-29 10:57:33 +00:00
|
|
|
const [currentPage, setCurrentPage] = useState<number>(0);
|
|
|
|
const fetchingStateRef = useRef<"idle" | "fetching" | "all fetched">("idle");
|
|
|
|
|
|
|
|
useEffect(function fetchTickets() {
|
|
|
|
const getTicketsBody: GetTicketsRequest = {
|
|
|
|
amt: TICKETS_PER_PAGE,
|
|
|
|
page: currentPage,
|
|
|
|
status: "open",
|
|
|
|
};
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
|
|
|
fetchingStateRef.current = "fetching";
|
|
|
|
getTickets({
|
|
|
|
body: getTicketsBody,
|
|
|
|
signal: controller.signal,
|
|
|
|
}).then(result => {
|
|
|
|
console.log("GetTicketsResponse", result);
|
|
|
|
if (result.data) {
|
|
|
|
updateTickets(result.data);
|
|
|
|
fetchingStateRef.current = "idle";
|
|
|
|
} else fetchingStateRef.current = "all fetched";
|
|
|
|
}).catch(error => {
|
|
|
|
console.log("Error fetching tickets", error);
|
|
|
|
enqueueSnackbar(error.message);
|
|
|
|
});
|
|
|
|
|
|
|
|
return () => controller.abort();
|
|
|
|
}, [currentPage]);
|
|
|
|
|
|
|
|
useEffect(function subscribeToTickets() {
|
|
|
|
const token = localStorage.getItem("AT");
|
|
|
|
if (!token) return;
|
|
|
|
|
|
|
|
const unsubscribe = subscribeToAllTickets({
|
|
|
|
accessToken: token,
|
|
|
|
onMessage(event) {
|
|
|
|
console.log("SSE: ticket received:", event.data);
|
|
|
|
try {
|
|
|
|
const newTicket = JSON.parse(event.data) as Ticket;
|
|
|
|
updateTickets([newTicket]);
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Error parsing ticket SSE", error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onError(event) {
|
|
|
|
console.log("SSE Error:", event);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
unsubscribe();
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const incrementCurrentPage = () => setCurrentPage(prev => prev + 1);
|
|
|
|
|
|
|
|
const ticketList = <TicketList fetchingStateRef={fetchingStateRef} incrementCurrentPage={incrementCurrentPage} />
|
2023-03-20 15:27:42 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Box sx={{
|
|
|
|
display: "flex",
|
2023-03-23 11:32:09 +00:00
|
|
|
width: "100%",
|
|
|
|
flexDirection: upMd ? "row" : "column",
|
|
|
|
gap: "12px",
|
2023-03-20 15:27:42 +00:00
|
|
|
}}>
|
2023-03-23 11:32:09 +00:00
|
|
|
{!upMd &&
|
|
|
|
<Collapse headerText="Тикеты">
|
2023-03-29 10:57:33 +00:00
|
|
|
{ticketList}
|
2023-03-23 11:32:09 +00:00
|
|
|
</Collapse>
|
|
|
|
}
|
2023-03-20 15:27:42 +00:00
|
|
|
<Chat />
|
2023-03-29 10:57:33 +00:00
|
|
|
{upMd && ticketList}
|
2023-03-20 15:27:42 +00:00
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|