improvement: focus app when clicking desktop notification

This commit is contained in:
canove
2020-09-25 22:12:31 -03:00
parent eb23775948
commit 9d87522ad9
3 changed files with 29 additions and 30 deletions

View File

@@ -338,7 +338,7 @@ const MessagesList = () => {
} }
if (data.action === "delete") { if (data.action === "delete") {
toast.success("The deleted sucessfully."); toast.success("Ticket deleted sucessfully.");
history.push("/tickets"); history.push("/tickets");
} }
}); });

View File

@@ -108,39 +108,40 @@ const NotificationsPopOver = () => {
(data.ticket.userId !== userId && data.ticket.userId) (data.ticket.userId !== userId && data.ticket.userId)
) )
return; return;
showDesktopNotification(data); else {
// show desktop notification
const { message, contact, ticket } = data;
const options = {
body: `${message.body} - ${format(new Date(), "HH:mm")}`,
icon: contact.profilePicUrl,
tag: ticket.id,
};
let notification = new Notification(
`${i18n.t("tickets.notification.message")} ${contact.name}`,
options
);
notification.onclick = function (event) {
event.preventDefault(); //
window.focus();
history.push(`/tickets/${ticket.id}`);
};
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") {
notification.close();
}
});
soundAlert.current.play();
}
} }
}); });
return () => { return () => {
socket.disconnect(); socket.disconnect();
}; };
}, [userId]); }, [history, userId]);
const showDesktopNotification = ({ message, contact, ticket }) => {
const options = {
body: `${message.body} - ${format(new Date(), "HH:mm")}`,
icon: contact.profilePicUrl,
tag: ticket.id,
};
let notification = new Notification(
`${i18n.t("tickets.notification.message")} ${contact.name}`,
options
);
notification.onclick = function (event) {
event.preventDefault(); //
history.push(`/tickets/${ticket.id}`);
};
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") {
notification.close();
}
});
soundAlert.current.play();
};
const handleClick = useCallback(() => { const handleClick = useCallback(() => {
setIsOpen(!isOpen); setIsOpen(!isOpen);

View File

@@ -1,5 +1,4 @@
import React, { useState } from "react"; import React, { useState } from "react";
import { useHistory } from "react-router-dom";
import { toast } from "react-toastify"; import { toast } from "react-toastify";
@@ -12,7 +11,6 @@ import ConfirmationModal from "../ConfirmationModal";
const TicketOptionsMenu = ({ ticket, menuOpen, handleClose, anchorEl }) => { const TicketOptionsMenu = ({ ticket, menuOpen, handleClose, anchorEl }) => {
const [confirmationOpen, setConfirmationOpen] = useState(false); const [confirmationOpen, setConfirmationOpen] = useState(false);
const history = useHistory();
const handleDeleteTicket = async () => { const handleDeleteTicket = async () => {
try { try {