refactor: mais tres ws connecoes movidas para conecao global
parent
a5d3f19e1a
commit
4d3cab5333
|
@ -1,7 +1,9 @@
|
||||||
import React, { useContext, useState, useEffect, useReducer, useRef } from "react"
|
import React, { useContext, useState, useEffect, useReducer, useRef } from "react"
|
||||||
|
|
||||||
import { isSameDay, parseISO, format } from "date-fns"
|
import { isSameDay, parseISO, format } from "date-fns"
|
||||||
import openSocket from "socket.io-client"
|
//import openSocket from "socket.io-client"
|
||||||
|
import { socket } from "../../services/socket"
|
||||||
|
|
||||||
import clsx from "clsx"
|
import clsx from "clsx"
|
||||||
import { AuthContext } from "../../context/Auth/AuthContext"
|
import { AuthContext } from "../../context/Auth/AuthContext"
|
||||||
|
|
||||||
|
@ -438,11 +440,17 @@ const MessagesList = ({ ticketId, isGroup }) => {
|
||||||
}, [pageNumber, ticketId])
|
}, [pageNumber, ticketId])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
|
//const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
|
||||||
|
|
||||||
socket.on("connect", () => socket.emit("joinChatBox", ticketId))
|
const onConnectMessagesList = () => {
|
||||||
|
socket.emit("joinChatBox", ticketId)
|
||||||
|
}
|
||||||
|
|
||||||
socket.on("appMessage", (data) => {
|
onConnectMessagesList()
|
||||||
|
|
||||||
|
socket.on("connect", onConnectMessagesList)
|
||||||
|
|
||||||
|
onAppMessageMessagesList = (data) => {
|
||||||
|
|
||||||
if (data.action === "create") {
|
if (data.action === "create") {
|
||||||
|
|
||||||
|
@ -454,10 +462,13 @@ const MessagesList = ({ ticketId, isGroup }) => {
|
||||||
if (data.action === "update") {
|
if (data.action === "update") {
|
||||||
dispatch({ type: "UPDATE_MESSAGE", payload: data.message })
|
dispatch({ type: "UPDATE_MESSAGE", payload: data.message })
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
socket.on("appMessage", onAppMessageMessagesList)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
socket.disconnect()
|
socket.off("connect", onConnectMessagesList)
|
||||||
|
socket.off("appMessage", onAppMessageMessagesList)
|
||||||
}
|
}
|
||||||
}, [ticketId])
|
}, [ticketId])
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,9 @@ import React, { useState, useEffect } from "react"
|
||||||
import { useParams, useHistory } from "react-router-dom"
|
import { useParams, useHistory } from "react-router-dom"
|
||||||
|
|
||||||
import { toast } from "react-toastify"
|
import { toast } from "react-toastify"
|
||||||
import openSocket from "socket.io-client"
|
//import openSocket from "socket.io-client"
|
||||||
|
import { socket } from "../../services/socket"
|
||||||
|
|
||||||
import clsx from "clsx"
|
import clsx from "clsx"
|
||||||
|
|
||||||
import { Paper, makeStyles } from "@material-ui/core"
|
import { Paper, makeStyles } from "@material-ui/core"
|
||||||
|
@ -132,11 +134,15 @@ const Ticket = () => {
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
|
//const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
|
||||||
|
|
||||||
socket.on("connect", () => socket.emit("joinChatBox", ticketId))
|
const onConnectTicket = () => socket.emit("joinChatBox", ticketId)
|
||||||
|
|
||||||
socket.on("ticket", (data) => {
|
onConnectTicket()
|
||||||
|
|
||||||
|
socket.on("connect", onConnectTicket)
|
||||||
|
|
||||||
|
const onTicketTicket = (data) => {
|
||||||
if (data.action === "update") {
|
if (data.action === "update") {
|
||||||
setTicket(data.ticket)
|
setTicket(data.ticket)
|
||||||
}
|
}
|
||||||
|
@ -145,9 +151,11 @@ const Ticket = () => {
|
||||||
toast.success("Ticket deleted sucessfully.")
|
toast.success("Ticket deleted sucessfully.")
|
||||||
history.push("/tickets")
|
history.push("/tickets")
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
socket.on("contact", (data) => {
|
socket.on("ticket", onTicketTicket)
|
||||||
|
|
||||||
|
const onContactTicket = (data) => {
|
||||||
if (data.action === "update") {
|
if (data.action === "update") {
|
||||||
setContact((prevState) => {
|
setContact((prevState) => {
|
||||||
if (prevState.id === data.contact?.id) {
|
if (prevState.id === data.contact?.id) {
|
||||||
|
@ -156,9 +164,11 @@ const Ticket = () => {
|
||||||
return prevState
|
return prevState
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
socket.on("remoteTickesControllIdleOpen", (data) => {
|
socket.on("contact", onContactTicket)
|
||||||
|
|
||||||
|
const onRemoteTicketsControllIdleOpenTicket = (data) => {
|
||||||
if (data.action === "update") {
|
if (data.action === "update") {
|
||||||
let url_ticketId
|
let url_ticketId
|
||||||
try {
|
try {
|
||||||
|
@ -175,10 +185,15 @@ const Ticket = () => {
|
||||||
console.log('error on try do the send seen: ', error)
|
console.log('error on try do the send seen: ', error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
socket.on("remoteTickesControllIdleOpen", onRemoteTicketsControllIdleOpenTicket)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
socket.disconnect()
|
socket.off("connect", onConnectTicket)
|
||||||
|
socket.off("ticket", onTicketTicket)
|
||||||
|
socket.off("contact", onContactTicket)
|
||||||
|
socket.off("remoteTickesControllIdleOpen", onRemoteTicketsControllIdleOpenTicket)
|
||||||
}
|
}
|
||||||
}, [ticketId, history])
|
}, [ticketId, history])
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@ import FindInPageIcon from '@material-ui/icons/FindInPage'
|
||||||
|
|
||||||
import FormControlLabel from "@material-ui/core/FormControlLabel"
|
import FormControlLabel from "@material-ui/core/FormControlLabel"
|
||||||
import Switch from "@material-ui/core/Switch"
|
import Switch from "@material-ui/core/Switch"
|
||||||
import openSocket from "socket.io-client"
|
|
||||||
|
|
||||||
import NewTicketModal from "../NewTicketModal"
|
import NewTicketModal from "../NewTicketModal"
|
||||||
import TicketsList from "../TicketsList"
|
import TicketsList from "../TicketsList"
|
||||||
|
|
|
@ -31,7 +31,7 @@ import { i18n } from '../translate/i18n'
|
||||||
import { WhatsAppsContext } from '../context/WhatsApp/WhatsAppsContext'
|
import { WhatsAppsContext } from '../context/WhatsApp/WhatsAppsContext'
|
||||||
import { AuthContext } from '../context/Auth/AuthContext'
|
import { AuthContext } from '../context/Auth/AuthContext'
|
||||||
import { Can } from '../components/Can'
|
import { Can } from '../components/Can'
|
||||||
import openSocket from 'socket.io-client'
|
//import openSocket from 'socket.io-client'
|
||||||
import { socket } from '../services/socket'
|
import { socket } from '../services/socket'
|
||||||
import api from '../services/api'
|
import api from '../services/api'
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue