refactor: mais 7 conexoes movidas para conexao ws global

websocket
willian-pessoa 2024-05-09 15:31:30 -03:00
parent 4d3cab5333
commit 5981f9dd5e
7 changed files with 109 additions and 85 deletions

View File

@ -3,8 +3,8 @@ import React, { useState, useEffect, useRef, useContext } from "react"
import * as Yup from "yup"
import { Formik, Form, Field } from "formik"
import { toast } from "react-toastify"
import openSocket from 'socket.io-client'
//import openSocket from 'socket.io-client'
import { socket } from "../../services/socket"
import {
makeStyles,
@ -87,9 +87,9 @@ const PositionModal = ({
useEffect(() => {
const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
//const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
socket.on('settings', (data) => {
const onSettingsPosition = (data) => {
if (data.action === 'update') {
setSettings((prevState) => {
const aux = [...prevState]
@ -98,10 +98,12 @@ const PositionModal = ({
return aux
})
}
})
}
socket.on('settings', onSettingsPosition)
return () => {
socket.disconnect()
socket.off("settings", onSettingsPosition)
}
}, [])

View File

@ -1,6 +1,8 @@
import React, { useEffect, useState, useContext } from "react";
import QRCode from "qrcode.react";
import openSocket from "socket.io-client";
//import openSocket from "socket.io-client";
import { socket } from "../../services/socket";
import toastError from "../../errors/toastError";
import { Dialog, DialogContent, Paper, Typography } from "@material-ui/core";
@ -31,9 +33,9 @@ const QrcodeModal = ({ open, onClose, whatsAppId }) => {
useEffect(() => {
if (!whatsAppId) return;
const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
//const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
socket.on("whatsappSession", data => {
const onWhatsAppSessionQrCode = data => {
if (data.action === "update" && data.session.id === whatsAppId) {
setQrCode(data.session.qrcode);
}
@ -53,10 +55,12 @@ const QrcodeModal = ({ open, onClose, whatsAppId }) => {
}
}
});
}
socket.on("whatsappSession", onWhatsAppSessionQrCode);
return () => {
socket.disconnect();
socket.off("whatsappSession", onWhatsAppSessionQrCode);
};
}, [whatsAppId, onClose, user.profile]);

View File

@ -23,8 +23,8 @@ import { IconButton, InputAdornment } from "@material-ui/core"
import { Colorize } from "@material-ui/icons"
import { AuthContext } from '../../context/Auth/AuthContext'
import openSocket from 'socket.io-client'
//import openSocket from 'socket.io-client'
import { socket } from "../../services/socket"
const useStyles = makeStyles(theme => ({
@ -92,9 +92,9 @@ const QueueModal = ({ open, onClose, queueId }) => {
}, [setting])
useEffect(() => {
const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
//const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
socket.on('settings', (data) => {
const onSettingsQueueModal = (data) => {
if (data.action === 'update') {
setSettings((prevState) => {
const aux = [...prevState]
@ -103,10 +103,12 @@ const QueueModal = ({ open, onClose, queueId }) => {
return aux
})
}
})
}
socket.on('settings', onSettingsQueueModal)
return () => {
socket.disconnect()
socket.off("settings", onSettingsQueueModal)
}
}, [])

View File

@ -3,8 +3,8 @@ import React, { useState, useEffect, useRef, useContext } from "react"
import * as Yup from "yup"
import { Formik, Form, Field } from "formik"
import { toast } from "react-toastify"
import openSocket from 'socket.io-client'
//import openSocket from 'socket.io-client'
import { socket } from "../../services/socket"
import {
makeStyles,
@ -101,9 +101,9 @@ const QuickAnswersModal = ({
}, [open,])
useEffect(() => {
const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
//const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
socket.on('settings', (data) => {
const onSettingsQuickAnswersModal = (data) => {
if (data.action === 'update') {
setSettings((prevState) => {
const aux = [...prevState]
@ -112,10 +112,12 @@ const QuickAnswersModal = ({
return aux
})
}
})
}
socket.on('settings', onSettingsQuickAnswersModal)
return () => {
socket.disconnect()
socket.off("settings", onSettingsQuickAnswersModal)
}
}, [])

View File

@ -1,6 +1,7 @@
import React, { useState, useContext, useMemo, useEffect } from "react"
import { useHistory } from "react-router-dom"
import openSocket from "socket.io-client"
//import openSocket from "socket.io-client"
import { socket } from "../../services/socket"
import Button from "@material-ui/core/Button"
import Dialog from "@material-ui/core/Dialog"
@ -110,9 +111,9 @@ const TransferTicketModal = ({ modalOpen, onClose, ticketid }) => {
useEffect(() => {
const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
//const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
socket.on('settings', (data) => {
const onSettingsTransferTicketModal = (data) => {
console.log('settings updated ----------------------------xxxxxxxxxxxx')
if (data.action === 'update') {
@ -123,10 +124,12 @@ const TransferTicketModal = ({ modalOpen, onClose, ticketid }) => {
return aux
})
}
})
}
socket.on('settings', onSettingsTransferTicketModal)
return () => {
socket.disconnect()
socket.off("settings", onSettingsTransferTicketModal)
}
}, [])

View File

@ -1,7 +1,8 @@
import React, { useState, useCallback, useEffect, useReducer, useContext } from 'react'
import { toast } from 'react-toastify'
import openSocket from 'socket.io-client'
//import openSocket from 'socket.io-client'
import { socket } from '../../services/socket'
import { makeStyles } from '@material-ui/core/styles'
import { green } from '@material-ui/core/colors'
@ -296,10 +297,9 @@ const Campaign = () => {
}
useEffect(() => {
const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
//const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
socket.on("contactsBulkInsertOnQueueStatus", (data) => {
const onContactsBulkCampaing = (data) => {
if (data.action === 'update') {
if (String(data.insertOnQueue.adminId) === String(user.id)) {
@ -312,19 +312,24 @@ const Campaign = () => {
}
}
})
}
socket.on('campaign', (data) => {
socket.on("contactsBulkInsertOnQueueStatus", onContactsBulkCampaing)
const onCampaignCampaign = (data) => {
if (data.action === 'update') {
dispatch({ type: "UPDATE_CAMPAIGNS", payload: data.campaign })
}
})
}
socket.on('campaign', onCampaignCampaign)
return () => {
socket.disconnect()
socket.off("campaign", onCampaignCampaign)
socket.off("contactsBulkInsertOnQueueStatus", onContactsBulkCampaing)
}
}, [user.id])

View File

@ -2,7 +2,8 @@ import React, { useState, useCallback, useEffect, useContext } from 'react'
import { toast } from 'react-toastify'
import { format, parseISO } from 'date-fns'
import openSocket from 'socket.io-client'
//import openSocket from 'socket.io-client'
import { socket } from '../../services/socket'
import { makeStyles } from '@material-ui/core/styles'
import { green, red, yellow, grey } from '@material-ui/core/colors'
@ -451,15 +452,17 @@ const Connections = () => {
}, [])
useEffect(() => {
const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
//const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
socket.on('diskSpaceMonit', (data) => {
const onDiskSpaceMonitConnections = (data) => {
if (data.action === 'update') {
setDiskSpaceInfo(data.diskSpace)
}
})
}
socket.on('settings', (data) => {
socket.on('diskSpaceMonit', onDiskSpaceMonitConnections)
const onSettingsConnections = (data) => {
if (data.action === 'update') {
setSettings((prevState) => {
const aux = [...prevState]
@ -468,10 +471,13 @@ const Connections = () => {
return aux
})
}
})
}
socket.on('settings', onSettingsConnections)
return () => {
socket.disconnect()
socket.off("settings", onSettingsConnections)
socket.off("diskSpaceMonit", onDiskSpaceMonitConnections)
}
}, [])