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

121 lines
4.3 KiB
TypeScript
Raw Normal View History

2022-12-22 12:43:14 +00:00
import { Typography, Box, useTheme, useMediaQuery, IconButton } from "@mui/material";
2022-11-25 18:52:46 +00:00
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import { useParams } from "react-router-dom";
import SectionWrapper from "@components/SectionWrapper";
import ComplexNavText from "@components/ComplexNavText";
2023-04-06 08:07:48 +00:00
import SupportChat from "./Chat/SupportChat";
2022-12-15 14:09:07 +00:00
import CreateTicket from "./CreateTicket";
2023-04-06 08:07:48 +00:00
import TicketList from "./TicketList/TicketList";
2023-03-31 15:01:43 +00:00
import { useEffect } from "react";
import { getTickets, subscribeToAllTickets } from "@root/api/tickets";
import { GetTicketsRequest, Ticket } from "@root/model/ticket";
2023-04-06 15:10:22 +00:00
import { updateTickets, setTicketCount, clearTickets, useTicketStore, setTicketsFetchState } from "@root/stores/tickets";
2023-03-31 15:01:43 +00:00
import { enqueueSnackbar } from "notistack";
import { authStore } from "@root/stores/makeRequest";
2022-11-23 11:46:04 +00:00
2023-03-30 12:33:28 +00:00
2022-11-23 11:46:04 +00:00
export default function Support() {
2023-03-30 12:33:28 +00:00
const theme = useTheme();
const upMd = useMediaQuery(theme.breakpoints.up("md"));
const ticketId = useParams().ticketId;
2023-04-06 15:10:22 +00:00
const ticketApiPage = useTicketStore(state => state.apiPage);
2023-03-31 15:01:43 +00:00
const ticketsPerPage = useTicketStore(state => state.ticketsPerPage);
const token = authStore(state => state.token);
useEffect(function fetchTickets() {
const getTicketsBody: GetTicketsRequest = {
amt: ticketsPerPage,
2023-04-06 15:10:22 +00:00
page: ticketApiPage,
2023-03-31 15:01:43 +00:00
status: "open",
};
const controller = new AbortController();
2023-04-06 15:10:22 +00:00
setTicketsFetchState("fetching");
2023-03-31 15:01:43 +00:00
getTickets({
body: getTicketsBody,
signal: controller.signal,
}).then(result => {
console.log("GetTicketsResponse", result);
if (result.data) {
updateTickets(result.data);
setTicketCount(result.count);
2023-04-06 15:10:22 +00:00
setTicketsFetchState("idle");
} else setTicketsFetchState("all fetched");
2023-03-31 15:01:43 +00:00
}).catch(error => {
console.log("Error fetching tickets", error);
enqueueSnackbar(error.message);
});
return () => controller.abort();
2023-04-06 15:10:22 +00:00
}, [ticketApiPage, ticketsPerPage]);
2023-03-31 15:01:43 +00:00
useEffect(function subscribeToTickets() {
if (!token) return;
const unsubscribe = subscribeToAllTickets({
accessToken: token,
onMessage(event) {
try {
const newTicket = JSON.parse(event.data) as Ticket;
console.log("SSE: parsed newTicket:", newTicket);
if ((newTicket as any).count) return; // Костыль пока бэк не починят
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);
}
});
return () => {
unsubscribe();
clearTickets();
};
}, [token]);
2022-11-23 11:46:04 +00:00
2023-03-30 12:33:28 +00:00
return (
<SectionWrapper
maxWidth="lg"
sx={{
pt: upMd ? "25px" : "20px",
pb: upMd ? "82px" : "43px",
height: "100%",
}}
2022-11-23 11:46:04 +00:00
>
2023-03-30 12:33:28 +00:00
{upMd && <ComplexNavText text1="Все тарифы — " text2="Запрос в службу техподдержки" />}
<Box
sx={{
mt: "20px",
mb: "40px",
display: "flex",
gap: "10px",
}}
>
{!upMd && (
<IconButton sx={{ p: 0, height: "28px", width: "28px", color: "black" }}>
<ArrowBackIcon />
</IconButton>
)}
<Typography variant="h4">Запрос в службу техподдержки</Typography>
</Box>
{ticketId ? (
<SupportChat />
) : (
<Box
sx={{
display: "flex",
flexDirection: "column",
gap: upMd ? "40px" : "60px",
}}
>
<CreateTicket />
<TicketList />
</Box>
)}
</SectionWrapper>
);
}