2023-06-12 18:55:54 +00:00
|
|
|
import React, { useState, useEffect } from "react";
|
2022-01-06 01:26:15 +00:00
|
|
|
import { useParams, useHistory } from "react-router-dom";
|
|
|
|
|
|
|
|
import { toast } from "react-toastify";
|
|
|
|
import openSocket from "socket.io-client";
|
|
|
|
import clsx from "clsx";
|
|
|
|
|
|
|
|
import { Paper, makeStyles } from "@material-ui/core";
|
|
|
|
|
|
|
|
import ContactDrawer from "../ContactDrawer";
|
|
|
|
import MessageInput from "../MessageInput/";
|
|
|
|
import TicketHeader from "../TicketHeader";
|
|
|
|
import TicketInfo from "../TicketInfo";
|
|
|
|
import TicketActionButtons from "../TicketActionButtons";
|
|
|
|
import MessagesList from "../MessagesList";
|
|
|
|
import api from "../../services/api";
|
|
|
|
import { ReplyMessageProvider } from "../../context/ReplyingMessage/ReplyingMessageContext";
|
|
|
|
import toastError from "../../errors/toastError";
|
|
|
|
|
|
|
|
const drawerWidth = 320;
|
|
|
|
|
|
|
|
const useStyles = makeStyles((theme) => ({
|
|
|
|
root: {
|
|
|
|
display: "flex",
|
|
|
|
height: "100%",
|
|
|
|
position: "relative",
|
|
|
|
overflow: "hidden",
|
|
|
|
},
|
|
|
|
|
|
|
|
ticketInfo: {
|
|
|
|
maxWidth: "50%",
|
|
|
|
flexBasis: "50%",
|
|
|
|
[theme.breakpoints.down("sm")]: {
|
|
|
|
maxWidth: "80%",
|
|
|
|
flexBasis: "80%",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ticketActionButtons: {
|
|
|
|
maxWidth: "50%",
|
|
|
|
flexBasis: "50%",
|
|
|
|
display: "flex",
|
|
|
|
[theme.breakpoints.down("sm")]: {
|
|
|
|
maxWidth: "100%",
|
|
|
|
flexBasis: "100%",
|
|
|
|
marginBottom: "5px",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
mainWrapper: {
|
|
|
|
flex: 1,
|
|
|
|
height: "100%",
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
|
|
|
overflow: "hidden",
|
|
|
|
borderTopLeftRadius: 0,
|
|
|
|
borderBottomLeftRadius: 0,
|
|
|
|
borderLeft: "0",
|
|
|
|
marginRight: -drawerWidth,
|
|
|
|
transition: theme.transitions.create("margin", {
|
|
|
|
easing: theme.transitions.easing.sharp,
|
|
|
|
duration: theme.transitions.duration.leavingScreen,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
|
|
|
|
mainWrapperShift: {
|
|
|
|
borderTopRightRadius: 0,
|
|
|
|
borderBottomRightRadius: 0,
|
|
|
|
transition: theme.transitions.create("margin", {
|
|
|
|
easing: theme.transitions.easing.easeOut,
|
|
|
|
duration: theme.transitions.duration.enteringScreen,
|
|
|
|
}),
|
|
|
|
marginRight: 0,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
const Ticket = () => {
|
|
|
|
const { ticketId } = useParams();
|
|
|
|
const history = useHistory();
|
|
|
|
const classes = useStyles();
|
|
|
|
|
|
|
|
const [drawerOpen, setDrawerOpen] = useState(false);
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
const [contact, setContact] = useState({});
|
|
|
|
const [ticket, setTicket] = useState({});
|
|
|
|
|
2023-06-12 18:55:54 +00:00
|
|
|
const [statusChatEnd, setStatusChatEnd] = useState({})
|
2022-02-28 13:51:11 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setLoading(true);
|
|
|
|
const delayDebounceFn = setTimeout(() => {
|
2023-06-12 18:55:54 +00:00
|
|
|
const fetchTicket = async () => {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// maria julia
|
2023-06-06 17:59:06 +00:00
|
|
|
|
2023-06-12 18:55:54 +00:00
|
|
|
const { data } = await api.get("/tickets/" + ticketId);
|
2023-06-06 17:59:06 +00:00
|
|
|
|
2023-06-12 18:55:54 +00:00
|
|
|
// setContact(data.contact);
|
|
|
|
// setTicket(data);
|
2023-06-06 17:59:06 +00:00
|
|
|
|
2023-06-12 18:55:54 +00:00
|
|
|
setContact(data.contact.contact);
|
|
|
|
setTicket(data.contact);
|
2022-02-28 13:51:11 +00:00
|
|
|
|
2023-06-12 18:55:54 +00:00
|
|
|
setStatusChatEnd(data.statusChatEnd)
|
2022-02-28 13:51:11 +00:00
|
|
|
|
2023-06-12 18:55:54 +00:00
|
|
|
setLoading(false);
|
2022-01-06 01:26:15 +00:00
|
|
|
} catch (err) {
|
2023-06-12 18:55:54 +00:00
|
|
|
setLoading(false);
|
2022-01-06 01:26:15 +00:00
|
|
|
toastError(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
fetchTicket();
|
|
|
|
}, 500);
|
|
|
|
return () => clearTimeout(delayDebounceFn);
|
2023-06-12 18:55:54 +00:00
|
|
|
}, [ticketId, history]);
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-06-06 17:59:06 +00:00
|
|
|
|
|
|
|
const regex = /^[0-9\b]+$/; // Regular expression to match only numbers
|
2023-06-12 18:55:54 +00:00
|
|
|
if (ticketId && !ticketId.match(regex) || ticketId && ticketId.length > 9 ) return
|
2023-06-06 17:59:06 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
|
|
|
|
|
|
|
|
socket.on("connect", () => socket.emit("joinChatBox", ticketId));
|
|
|
|
|
|
|
|
socket.on("ticket", (data) => {
|
|
|
|
if (data.action === "update") {
|
|
|
|
setTicket(data.ticket);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.action === "delete") {
|
|
|
|
toast.success("Ticket deleted sucessfully.");
|
|
|
|
history.push("/tickets");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("contact", (data) => {
|
|
|
|
if (data.action === "update") {
|
|
|
|
setContact((prevState) => {
|
|
|
|
if (prevState.id === data.contact?.id) {
|
|
|
|
return { ...prevState, ...data.contact };
|
|
|
|
}
|
|
|
|
return prevState;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
socket.disconnect();
|
|
|
|
};
|
|
|
|
}, [ticketId, history]);
|
|
|
|
|
|
|
|
const handleDrawerOpen = () => {
|
|
|
|
setDrawerOpen(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDrawerClose = () => {
|
|
|
|
setDrawerOpen(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classes.root} id="drawer-container">
|
|
|
|
<Paper
|
|
|
|
variant="outlined"
|
|
|
|
elevation={0}
|
|
|
|
className={clsx(classes.mainWrapper, {
|
|
|
|
[classes.mainWrapperShift]: drawerOpen,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<TicketHeader loading={loading}>
|
|
|
|
<div className={classes.ticketInfo}>
|
|
|
|
<TicketInfo
|
|
|
|
contact={contact}
|
|
|
|
ticket={ticket}
|
|
|
|
onClick={handleDrawerOpen}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className={classes.ticketActionButtons}>
|
2023-06-12 18:55:54 +00:00
|
|
|
<TicketActionButtons ticket={ticket} statusChatEnd={statusChatEnd}/>
|
2022-01-06 01:26:15 +00:00
|
|
|
</div>
|
|
|
|
</TicketHeader>
|
|
|
|
<ReplyMessageProvider>
|
|
|
|
<MessagesList
|
|
|
|
ticketId={ticketId}
|
|
|
|
isGroup={ticket.isGroup}
|
|
|
|
></MessagesList>
|
|
|
|
<MessageInput ticketStatus={ticket.status} />
|
|
|
|
</ReplyMessageProvider>
|
|
|
|
</Paper>
|
|
|
|
<ContactDrawer
|
|
|
|
open={drawerOpen}
|
|
|
|
handleDrawerClose={handleDrawerClose}
|
|
|
|
contact={contact}
|
|
|
|
loading={loading}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-06-12 18:55:54 +00:00
|
|
|
export default Ticket;
|