improvement: change notifications logic

This commit is contained in:
canove
2020-09-25 19:20:31 -03:00
parent 5396837db4
commit 72c3332401
2 changed files with 37 additions and 4 deletions

View File

@@ -49,7 +49,9 @@ const NotificationsPopOver = () => {
const ticketId = +history.location.pathname.split("/")[2]; const ticketId = +history.location.pathname.split("/")[2];
const anchorEl = useRef(); const anchorEl = useRef();
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
// const [notifications, setNotifications] = useState([]); const [notifications, setNotifications] = useState([]);
const { tickets } = useTickets({ withUnreadMessages: "true" });
useEffect(() => { useEffect(() => {
if (!("Notification" in window)) { if (!("Notification" in window)) {
@@ -59,12 +61,41 @@ const NotificationsPopOver = () => {
} }
}, []); }, []);
useEffect(() => {
setNotifications(tickets);
}, [tickets]);
useEffect(() => { useEffect(() => {
const socket = openSocket(process.env.REACT_APP_BACKEND_URL); const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
socket.emit("joinNotification"); socket.emit("joinNotification");
socket.on("ticket", data => {
if (data.action === "updateUnread") {
setNotifications(prevState => {
const ticketIndex = prevState.findIndex(t => t.id === data.ticketId);
if (ticketIndex !== -1) {
prevState.splice(ticketIndex, 1);
return [...prevState];
}
return prevState;
});
}
});
socket.on("appMessage", data => { socket.on("appMessage", data => {
if (data.action === "create") { if (
(data.action === "create" && data.ticket.userId === userId) ||
!data.ticket.userId
) {
setNotifications(prevState => {
const ticketIndex = prevState.findIndex(t => t.id === data.ticket.id);
if (ticketIndex !== -1) {
prevState[ticketIndex] = data.ticket;
return [...prevState];
}
return [data.ticket, ...prevState];
});
if ( if (
(ticketId && (ticketId &&
data.message.ticketId === +ticketId && data.message.ticketId === +ticketId &&
@@ -81,8 +112,6 @@ const NotificationsPopOver = () => {
}; };
}, [history, ticketId, userId]); }, [history, ticketId, userId]);
const { tickets: notifications } = useTickets({ withUnreadMessages: "true" });
const showDesktopNotification = ({ message, contact, ticket }) => { const showDesktopNotification = ({ message, contact, ticket }) => {
const options = { const options = {
body: `${message.body} - ${format(new Date(), "HH:mm")}`, body: `${message.body} - ${format(new Date(), "HH:mm")}`,

View File

@@ -96,6 +96,7 @@ const reducer = (state, action) => {
if (ticketIndex !== -1) { if (ticketIndex !== -1) {
state[ticketIndex].unreadMessages = 0; state[ticketIndex].unreadMessages = 0;
} }
return [...state]; return [...state];
} }
@@ -108,6 +109,7 @@ const reducer = (state, action) => {
} else { } else {
state.unshift(ticket); state.unshift(ticket);
} }
return [...state]; return [...state];
} }
@@ -119,6 +121,7 @@ const reducer = (state, action) => {
state[ticketIndex] = ticket; state[ticketIndex] = ticket;
state.unshift(state.splice(ticketIndex, 1)[0]); state.unshift(state.splice(ticketIndex, 1)[0]);
} }
return [...state]; return [...state];
} }
@@ -128,6 +131,7 @@ const reducer = (state, action) => {
if (ticketIndex !== -1) { if (ticketIndex !== -1) {
state.splice(ticketIndex, 1); state.splice(ticketIndex, 1);
} }
return [...state]; return [...state];
} }