Correção do botao restore para desabitar sem precisar receber resposta do servidor

pull/21/head
adriano 2022-12-27 18:19:24 -03:00
parent 1602b71524
commit 56886611a7
4 changed files with 82 additions and 22 deletions

View File

@ -96,6 +96,7 @@ const TableUser = ({ classes, usersOnlineInfo, logout }) => {
</TableRow>
</TableHead>
<TableBody style={{ overflowY: "scroll" }}>
{usersOnlineInfo &&
usersOnlineInfo

View File

@ -168,7 +168,7 @@ const NotificationsPopOver = () => {
return () => {
socket.disconnect();
};
}, [user.id, handleLogout]);
}, [user.id, handleLogout, user.profile ]);

View File

@ -118,6 +118,8 @@ const Connections = () => {
const [disabled, setDisabled] = useState(true);
const [buttons, setClicks] = useState([])
const confirmationModalInitialState = {
action: "",
@ -139,10 +141,24 @@ const Connections = () => {
};
const handleRestartWhatsAppSession = async whatsAppId => {
const handleRestartWhatsAppSession = async whatsapp => {
try {
await api.post(`/restartwhatsappsession/${whatsAppId}`);
whatsapp.disabled = true
setClicks([...buttons, whatsapp.id])
function enable_button(whatsappId) {
setClicks(buttons => buttons.filter(id => { return +id !== +whatsappId }),);
}
setTimeout(enable_button, 25000, whatsapp.id)
await api.post(`/restartwhatsappsession/${whatsapp.id}`);
} catch (err) {
toastError(err);
@ -150,6 +166,24 @@ const Connections = () => {
};
useEffect(() => {
// whatsApps.map((e) => {
// if (buttons.includes(e.id)) {
// e.disabled = true
// }
// })
for (let i = 0; i < whatsApps.length; i++) {
if (buttons.includes(whatsApps[i].id)) {
whatsApps[i].disabled = true
}
}
}, [whatsApps, buttons])
const handleRequestNewQrCode = async whatsAppId => {
try {
await api.put(`/whatsappsession/${whatsAppId}`);
@ -354,6 +388,8 @@ const Connections = () => {
setDisabled(false)
setClicks(buttons => buttons.map((e) => { return { id: e.id, disabled: false } }))
} catch (err) {
console.log(err);
}
@ -579,7 +615,7 @@ const Connections = () => {
size="small"
variant="contained"
color="primary"
onClick={() => handleRestartWhatsAppSession(whatsApp.id)}
onClick={() => handleRestartWhatsAppSession(whatsApp)}
>
Restore

View File

@ -1,6 +1,6 @@
import React, { useContext, useReducer, useEffect, useState } from "react";
import { addHours, addMinutes, addSeconds, intervalToDuration, add } from "date-fns";
import { addHours, addMinutes, addSeconds, intervalToDuration } from "date-fns";
import Paper from "@material-ui/core/Paper";
import Container from "@material-ui/core/Container";
@ -112,6 +112,12 @@ const sumOnlineTimeNow = (oldOnlineTimeSum) => {
let onlineTime = new Date()
if (!oldOnlineTimeSum.onlineTime) {
oldOnlineTimeSum.onlineTime = `${oldOnlineTimeSum.updatedAt.split(' ')[0]} 00:00:00`
}
onlineTime.setUTCHours(new Date(oldOnlineTimeSum.onlineTime).getHours())
onlineTime.setUTCMinutes(new Date(oldOnlineTimeSum.onlineTime).getMinutes())
onlineTime.setUTCSeconds(new Date(oldOnlineTimeSum.onlineTime).getSeconds())
@ -130,7 +136,8 @@ const sumOnlineTimeNow = (oldOnlineTimeSum) => {
onlineTime = addSeconds(onlineTime, newtTime.seconds)
}
const isoDate = new Date(onlineTime);
const isoDate = new Date(onlineTime);
const newOnlinetime = isoDate.toJSON().slice(0, 19).replace('T', ' ');
return newOnlinetime
@ -196,7 +203,7 @@ const reducer = (state, action) => {
}
}
if ("onlineTime" in onlineUser) {
@ -205,7 +212,7 @@ const reducer = (state, action) => {
// console.log(' ffffffffffffffffffffffffffff ')
state[index].sumOnlineTime.sum = onlineUser.onlineTime.split(" ")[1]
} else if (!("sumOnlineTime" in state[index])) {
} else if (!("sumOnlineTime" in state[index])) {
state[index].sumOnlineTime = {
userId: onlineUser.userId,
@ -300,26 +307,38 @@ const Dashboard = () => {
useEffect(() => {
if (!usersOnlineInfo || usersOnlineInfo.length == 0) return
if (!usersOnlineInfo || usersOnlineInfo.length === 0) return
if (_fifo) {
clearInterval(_fifo);
}
_fifo = setInterval(() => {
usersOnlineInfo.map((e) => {
if (e.statusOnline && e.statusOnline.status === 'online') {
let onlineTimeCurrent = sumOnlineTimeNow({onlineTime: e.statusOnline.onlineTime, updatedAt: e.statusOnline.updatedAt})
for (let i = 0; i < usersOnlineInfo.length; i++) {
dispatch({ type: "UPDATE_STATUS_ONLINE", payload: { userId: e.id, status: e.statusOnline.status, onlineTime: onlineTimeCurrent } });
if (usersOnlineInfo[i].statusOnline && usersOnlineInfo[i].statusOnline.status === 'online') {
let onlineTimeCurrent = sumOnlineTimeNow({ onlineTime: usersOnlineInfo[i].statusOnline.onlineTime, updatedAt: usersOnlineInfo[i].statusOnline.updatedAt })
dispatch({ type: "UPDATE_STATUS_ONLINE", payload: { userId: usersOnlineInfo[i].id, status: usersOnlineInfo[i].statusOnline.status, onlineTime: onlineTimeCurrent } });
}
}
// usersOnlineInfo.map((e) => {
// if (e.statusOnline && e.statusOnline.status === 'online') {
// let onlineTimeCurrent = sumOnlineTimeNow({onlineTime: e.statusOnline.onlineTime, updatedAt: e.statusOnline.updatedAt})
// dispatch({ type: "UPDATE_STATUS_ONLINE", payload: { userId: e.id, status: e.statusOnline.status, onlineTime: onlineTimeCurrent } });
// }
// })
})
}, 3000);
@ -568,11 +587,15 @@ const Dashboard = () => {
</Paper>
</Grid>
<TableUser
classes={classes}
usersOnlineInfo={usersOnlineInfo}
logout={handleLogouOnlineUser}
/>
{usersOnlineInfo &&
<TableUser
classes={classes}
usersOnlineInfo={usersOnlineInfo}
logout={handleLogouOnlineUser}
/>
}
</Grid>
</Paper>
</Grid>