2022-01-06 01:26:15 +00:00
|
|
|
import React, { useState, useEffect, useReducer, useContext } from "react";
|
|
|
|
import openSocket from "socket.io-client";
|
|
|
|
|
|
|
|
import { makeStyles } from "@material-ui/core/styles";
|
|
|
|
import List from "@material-ui/core/List";
|
|
|
|
import Paper from "@material-ui/core/Paper";
|
|
|
|
|
|
|
|
import TicketListItem from "../TicketListItem";
|
|
|
|
import TicketsListSkeleton from "../TicketsListSkeleton";
|
|
|
|
|
|
|
|
import useTickets from "../../hooks/useTickets";
|
|
|
|
import { i18n } from "../../translate/i18n";
|
2022-08-08 16:47:28 +00:00
|
|
|
import { AuthContext } from "../../context/Auth/AuthContext";
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const useStyles = makeStyles(theme => ({
|
|
|
|
ticketsListWrapper: {
|
|
|
|
position: "relative",
|
|
|
|
display: "flex",
|
|
|
|
height: "100%",
|
|
|
|
flexDirection: "column",
|
|
|
|
overflow: "hidden",
|
|
|
|
borderTopRightRadius: 0,
|
|
|
|
borderBottomRightRadius: 0,
|
|
|
|
},
|
|
|
|
|
|
|
|
ticketsList: {
|
|
|
|
flex: 1,
|
|
|
|
overflowY: "scroll",
|
|
|
|
...theme.scrollbarStyles,
|
|
|
|
borderTop: "2px solid rgba(0, 0, 0, 0.12)",
|
|
|
|
},
|
|
|
|
|
|
|
|
ticketsListHeader: {
|
|
|
|
color: "rgb(67, 83, 105)",
|
|
|
|
zIndex: 2,
|
|
|
|
backgroundColor: "white",
|
|
|
|
borderBottom: "1px solid rgba(0, 0, 0, 0.12)",
|
|
|
|
display: "flex",
|
|
|
|
alignItems: "center",
|
|
|
|
justifyContent: "space-between",
|
|
|
|
},
|
|
|
|
|
|
|
|
ticketsCount: {
|
|
|
|
fontWeight: "normal",
|
|
|
|
color: "rgb(104, 121, 146)",
|
|
|
|
marginLeft: "8px",
|
|
|
|
fontSize: "14px",
|
|
|
|
},
|
|
|
|
|
|
|
|
noTicketsText: {
|
|
|
|
textAlign: "center",
|
|
|
|
color: "rgb(104, 121, 146)",
|
|
|
|
fontSize: "14px",
|
|
|
|
lineHeight: "1.4",
|
|
|
|
},
|
|
|
|
|
|
|
|
noTicketsTitle: {
|
|
|
|
textAlign: "center",
|
|
|
|
fontSize: "16px",
|
|
|
|
fontWeight: "600",
|
|
|
|
margin: "0px",
|
|
|
|
},
|
|
|
|
|
|
|
|
noTicketsDiv: {
|
|
|
|
display: "flex",
|
|
|
|
height: "100px",
|
|
|
|
margin: 40,
|
|
|
|
flexDirection: "column",
|
|
|
|
alignItems: "center",
|
|
|
|
justifyContent: "center",
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
const reducer = (state, action) => {
|
|
|
|
if (action.type === "LOAD_TICKETS") {
|
2022-08-08 16:47:28 +00:00
|
|
|
const newTickets = action.payload;
|
|
|
|
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
newTickets.forEach(ticket => {
|
2022-07-14 23:00:35 +00:00
|
|
|
|
|
|
|
// console.log('* ticket.unreadMessages: ',ticket.unreadMessages)
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const ticketIndex = state.findIndex(t => t.id === ticket.id);
|
|
|
|
if (ticketIndex !== -1) {
|
|
|
|
state[ticketIndex] = ticket;
|
|
|
|
if (ticket.unreadMessages > 0) {
|
|
|
|
state.unshift(state.splice(ticketIndex, 1)[0]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
state.push(ticket);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return [...state];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === "RESET_UNREAD") {
|
|
|
|
const ticketId = action.payload;
|
|
|
|
|
|
|
|
const ticketIndex = state.findIndex(t => t.id === ticketId);
|
|
|
|
if (ticketIndex !== -1) {
|
|
|
|
state[ticketIndex].unreadMessages = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [...state];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === "UPDATE_TICKET") {
|
|
|
|
const ticket = action.payload;
|
|
|
|
|
2022-08-08 16:47:28 +00:00
|
|
|
// console.log('++++++++++++ UPDATE_TICKET: ',ticket)
|
2022-07-14 23:00:35 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const ticketIndex = state.findIndex(t => t.id === ticket.id);
|
|
|
|
if (ticketIndex !== -1) {
|
|
|
|
state[ticketIndex] = ticket;
|
|
|
|
} else {
|
|
|
|
state.unshift(ticket);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [...state];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === "UPDATE_TICKET_UNREAD_MESSAGES") {
|
2022-07-14 23:00:35 +00:00
|
|
|
|
|
|
|
const message = action.payload.message
|
|
|
|
|
2022-08-08 16:47:28 +00:00
|
|
|
const ticket = action.payload.ticket;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const ticketIndex = state.findIndex(t => t.id === ticket.id);
|
2022-07-14 23:00:35 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
if (ticketIndex !== -1) {
|
2022-08-08 16:47:28 +00:00
|
|
|
|
2022-07-14 23:00:35 +00:00
|
|
|
// console.log('>>>>>> ticketIndex: ', ticketIndex)
|
|
|
|
|
|
|
|
// console.log('&&&&&&& UPDATE_TICKET_UNREAD_MESSAGES ticket: ',ticket, ' |\n MESSAGE: ', message)
|
2022-08-08 16:47:28 +00:00
|
|
|
|
|
|
|
if (!message.fromMe) {
|
|
|
|
ticket.unreadMessages += 1
|
2022-07-14 23:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
state[ticketIndex] = ticket;
|
|
|
|
state.unshift(state.splice(ticketIndex, 1)[0]);
|
2022-08-08 16:47:28 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
} else {
|
|
|
|
state.unshift(ticket);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [...state];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === "UPDATE_TICKET_CONTACT") {
|
|
|
|
const contact = action.payload;
|
|
|
|
const ticketIndex = state.findIndex(t => t.contactId === contact.id);
|
|
|
|
if (ticketIndex !== -1) {
|
|
|
|
state[ticketIndex].contact = contact;
|
|
|
|
}
|
|
|
|
return [...state];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === "DELETE_TICKET") {
|
|
|
|
const ticketId = action.payload;
|
|
|
|
const ticketIndex = state.findIndex(t => t.id === ticketId);
|
|
|
|
if (ticketIndex !== -1) {
|
|
|
|
state.splice(ticketIndex, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [...state];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === "RESET") {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-25 20:19:38 +00:00
|
|
|
const TicketsList = (props) => {
|
|
|
|
const { status, searchParam, showAll, selectedQueueIds, updateCount, style } =
|
|
|
|
props;
|
2022-01-06 01:26:15 +00:00
|
|
|
const classes = useStyles();
|
|
|
|
const [pageNumber, setPageNumber] = useState(1);
|
|
|
|
const [ticketsList, dispatch] = useReducer(reducer, []);
|
|
|
|
const { user } = useContext(AuthContext);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
dispatch({ type: "RESET" });
|
|
|
|
setPageNumber(1);
|
|
|
|
}, [status, searchParam, dispatch, showAll, selectedQueueIds]);
|
|
|
|
|
|
|
|
const { tickets, hasMore, loading } = useTickets({
|
|
|
|
pageNumber,
|
|
|
|
searchParam,
|
|
|
|
status,
|
|
|
|
showAll,
|
|
|
|
queueIds: JSON.stringify(selectedQueueIds),
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!status && !searchParam) return;
|
2022-07-28 21:21:14 +00:00
|
|
|
|
|
|
|
// console.log('lllllllllllllllllllllllllllllllll')
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
dispatch({
|
|
|
|
type: "LOAD_TICKETS",
|
|
|
|
payload: tickets,
|
|
|
|
});
|
|
|
|
}, [tickets, status, searchParam]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
|
|
|
|
|
|
|
|
const shouldUpdateTicket = ticket =>
|
|
|
|
(!ticket.userId || ticket.userId === user?.id || showAll) &&
|
|
|
|
(!ticket.queueId || selectedQueueIds.indexOf(ticket.queueId) > -1);
|
|
|
|
|
|
|
|
const notBelongsToUserQueues = ticket =>
|
|
|
|
ticket.queueId && selectedQueueIds.indexOf(ticket.queueId) === -1;
|
|
|
|
|
|
|
|
socket.on("connect", () => {
|
|
|
|
if (status) {
|
|
|
|
socket.emit("joinTickets", status);
|
|
|
|
} else {
|
|
|
|
socket.emit("joinNotification");
|
2022-05-25 20:19:38 +00:00
|
|
|
}
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
});
|
|
|
|
|
2022-08-08 16:47:28 +00:00
|
|
|
socket.on("ticket", data => {
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
if (data.action === "updateUnread") {
|
|
|
|
dispatch({
|
|
|
|
type: "RESET_UNREAD",
|
|
|
|
payload: data.ticketId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.action === "update" && shouldUpdateTicket(data.ticket)) {
|
|
|
|
dispatch({
|
|
|
|
type: "UPDATE_TICKET",
|
|
|
|
payload: data.ticket,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-08 16:47:28 +00:00
|
|
|
if (data.action === "update" && notBelongsToUserQueues(data.ticket)) {
|
2022-01-06 01:26:15 +00:00
|
|
|
dispatch({ type: "DELETE_TICKET", payload: data.ticket.id });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.action === "delete") {
|
|
|
|
dispatch({ type: "DELETE_TICKET", payload: data.ticketId });
|
2022-08-08 16:47:28 +00:00
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("appMessage", data => {
|
|
|
|
if (data.action === "create" && shouldUpdateTicket(data.ticket)) {
|
2022-07-14 23:00:35 +00:00
|
|
|
|
|
|
|
// console.log('((((((((((((((((((( DATA.MESSAGE: ', data.message)
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
dispatch({
|
|
|
|
type: "UPDATE_TICKET_UNREAD_MESSAGES",
|
2022-07-14 23:00:35 +00:00
|
|
|
// payload: data.ticket,
|
|
|
|
payload: data,
|
2022-01-06 01:26:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("contact", data => {
|
|
|
|
if (data.action === "update") {
|
|
|
|
dispatch({
|
|
|
|
type: "UPDATE_TICKET_CONTACT",
|
|
|
|
payload: data.contact,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
socket.disconnect();
|
|
|
|
};
|
|
|
|
}, [status, showAll, user, selectedQueueIds]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-05-25 20:19:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof updateCount === "function") {
|
|
|
|
updateCount(ticketsList.length);
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [ticketsList]);
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const loadMore = () => {
|
|
|
|
setPageNumber(prevState => prevState + 1);
|
|
|
|
};
|
|
|
|
|
2022-08-08 16:47:28 +00:00
|
|
|
const handleScroll = e => {
|
2022-07-28 17:55:23 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
if (!hasMore || loading) return;
|
|
|
|
|
2022-08-08 16:47:28 +00:00
|
|
|
const { scrollTop, scrollHeight, clientHeight } = e.currentTarget;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
if (scrollHeight - (scrollTop + 100) < clientHeight) {
|
|
|
|
loadMore();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-05-25 20:19:38 +00:00
|
|
|
<Paper className={classes.ticketsListWrapper} style={style}>
|
2022-01-06 01:26:15 +00:00
|
|
|
<Paper
|
|
|
|
square
|
|
|
|
name="closed"
|
|
|
|
elevation={0}
|
|
|
|
className={classes.ticketsList}
|
|
|
|
onScroll={handleScroll}
|
|
|
|
>
|
|
|
|
<List style={{ paddingTop: 0 }}>
|
|
|
|
{ticketsList.length === 0 && !loading ? (
|
|
|
|
<div className={classes.noTicketsDiv}>
|
|
|
|
<span className={classes.noTicketsTitle}>
|
|
|
|
{i18n.t("ticketsList.noTicketsTitle")}
|
|
|
|
</span>
|
|
|
|
<p className={classes.noTicketsText}>
|
|
|
|
{i18n.t("ticketsList.noTicketsMessage")}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
{ticketsList.map(ticket => (
|
|
|
|
<TicketListItem ticket={ticket} key={ticket.id} />
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{loading && <TicketsListSkeleton />}
|
|
|
|
</List>
|
|
|
|
</Paper>
|
2022-05-25 20:19:38 +00:00
|
|
|
</Paper>
|
2022-01-06 01:26:15 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default TicketsList;
|