improvement: moved user data from localstorage to context

This commit is contained in:
canove
2021-01-13 08:08:25 -03:00
parent 2bec877e4f
commit 3aa287d394
23 changed files with 231 additions and 134 deletions

View File

@@ -1,4 +1,4 @@
import React, { useState, useRef, useEffect } from "react";
import React, { useState, useRef, useEffect, useContext } from "react";
import { useHistory } from "react-router-dom";
import { format } from "date-fns";
@@ -18,6 +18,7 @@ import TicketListItem from "../TicketListItem";
import { i18n } from "../../translate/i18n";
import useTickets from "../../hooks/useTickets";
import alertSound from "../../assets/sound.mp3";
import { AuthContext } from "../../context/Auth/AuthContext";
const useStyles = makeStyles(theme => ({
tabContainer: {
@@ -43,7 +44,7 @@ const NotificationsPopOver = () => {
const classes = useStyles();
const history = useHistory();
const userId = +localStorage.getItem("userId");
const { user } = useContext(AuthContext);
const ticketIdUrl = +history.location.pathname.split("/")[2];
const ticketIdRef = useRef(ticketIdUrl);
const anchorEl = useRef();
@@ -108,7 +109,7 @@ const NotificationsPopOver = () => {
if (
data.action === "create" &&
!data.message.read &&
(data.ticket.userId === userId || !data.ticket.userId)
(data.ticket.userId === user?.id || !data.ticket.userId)
) {
setNotifications(prevState => {
const ticketIndex = prevState.findIndex(t => t.id === data.ticket.id);
@@ -122,7 +123,7 @@ const NotificationsPopOver = () => {
const shouldNotNotificate =
(data.message.ticketId === ticketIdRef.current &&
document.visibilityState === "visible") ||
(data.ticket.userId && data.ticket.userId !== userId) ||
(data.ticket.userId && data.ticket.userId !== user?.id) ||
data.ticket.isGroup;
if (shouldNotNotificate) return;
@@ -134,7 +135,7 @@ const NotificationsPopOver = () => {
return () => {
socket.disconnect();
};
}, [history, userId]);
}, [history, user]);
const handleNotifications = (data, history) => {
const { message, contact, ticket } = data;