2023-03-23 11:32:09 +00:00
|
|
|
import { Box, useMediaQuery, useTheme } from "@mui/material";
|
2023-04-07 09:43:37 +00:00
|
|
|
import { useEffect } 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";
|
2023-04-07 09:43:37 +00:00
|
|
|
import { clearTickets, setTicketsFetchState, updateTickets, useTicketStore } from "@root/stores/tickets";
|
2023-03-29 10:57:33 +00:00
|
|
|
import { enqueueSnackbar } from "notistack";
|
2023-03-31 15:07:37 +00:00
|
|
|
import { clearMessages } from "@root/stores/messages";
|
2023-04-17 12:24:09 +00:00
|
|
|
import { authStore } from "@root/stores/auth";
|
2023-03-20 15:27:42 +00:00
|
|
|
|
2023-03-29 10:57:33 +00:00
|
|
|
|
2023-03-20 15:27:42 +00:00
|
|
|
export default function Support() {
|
2023-04-07 13:33:25 +00:00
|
|
|
const theme = useTheme();
|
|
|
|
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
|
|
|
const ticketsPerPage = useTicketStore(state => state.ticketsPerPage);
|
|
|
|
const ticketApiPage = useTicketStore(state => state.apiPage);
|
|
|
|
const { token } = authStore();
|
|
|
|
const fetchingStateRef = useRef<"idle" | "fetching" | "all fetched">("idle");
|
2023-03-29 10:57:33 +00:00
|
|
|
|
2023-04-07 13:33:25 +00:00
|
|
|
useEffect(function fetchTickets() {
|
|
|
|
const getTicketsBody: GetTicketsRequest = {
|
|
|
|
amt: ticketsPerPage,
|
|
|
|
page: ticketApiPage,
|
|
|
|
status: "open",
|
|
|
|
};
|
|
|
|
const controller = new AbortController();
|
2023-03-29 10:57:33 +00:00
|
|
|
|
2023-04-07 13:33:25 +00:00
|
|
|
setTicketsFetchState("fetching");
|
|
|
|
getTickets({
|
|
|
|
body: getTicketsBody,
|
|
|
|
signal: controller.signal,
|
|
|
|
}).then(result => {
|
|
|
|
console.log("GetTicketsResponse", result);
|
|
|
|
if (result.data) {
|
|
|
|
updateTickets(result.data);
|
|
|
|
setTicketsFetchState("idle");
|
|
|
|
} else setTicketsFetchState("all fetched");
|
|
|
|
}).catch(error => {
|
|
|
|
console.log("Error fetching tickets", error);
|
|
|
|
enqueueSnackbar(error.message);
|
2023-03-29 10:57:33 +00:00
|
|
|
});
|
|
|
|
|
2023-04-07 13:33:25 +00:00
|
|
|
return () => controller.abort();
|
|
|
|
}, [ticketApiPage, ticketsPerPage]);
|
2023-03-29 10:57:33 +00:00
|
|
|
|
2023-04-07 13:33:25 +00:00
|
|
|
useEffect(function subscribeToTickets() {
|
|
|
|
if (!token) return;
|
2023-03-29 10:57:33 +00:00
|
|
|
|
2023-04-07 13:33:25 +00:00
|
|
|
const unsubscribe = subscribeToAllTickets({
|
|
|
|
accessToken: token,
|
|
|
|
onMessage(event) {
|
|
|
|
try {
|
|
|
|
const newTicket = JSON.parse(event.data) as Ticket;
|
|
|
|
console.log("SSE: parsed newTicket:", newTicket);
|
|
|
|
updateTickets([newTicket]);
|
|
|
|
} catch (error) {
|
|
|
|
console.log("SSE: couldn't parse:", event.data);
|
|
|
|
console.log("Error parsing ticket SSE", error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onError(event) {
|
|
|
|
console.log("SSE Error:", event);
|
|
|
|
}
|
|
|
|
});
|
2023-03-29 10:57:33 +00:00
|
|
|
|
2023-04-07 13:33:25 +00:00
|
|
|
return () => {
|
|
|
|
unsubscribe();
|
|
|
|
clearMessages();
|
|
|
|
clearTickets();
|
|
|
|
};
|
|
|
|
}, []);
|
2023-03-20 15:27:42 +00:00
|
|
|
|
2023-04-07 13:33:25 +00:00
|
|
|
return (
|
|
|
|
<Box sx={{
|
|
|
|
display: "flex",
|
|
|
|
width: "100%",
|
|
|
|
flexDirection: upMd ? "row" : "column",
|
|
|
|
gap: "12px",
|
|
|
|
}}>
|
|
|
|
{!upMd &&
|
|
|
|
<Collapse headerText="Тикеты">
|
|
|
|
<TicketList />
|
|
|
|
</Collapse>
|
|
|
|
}
|
|
|
|
<Chat />
|
|
|
|
{upMd && <TicketList />}
|
|
|
|
</Box>
|
|
|
|
);
|
2023-04-07 09:43:37 +00:00
|
|
|
}
|