2022-12-11 07:47:32 +00:00
|
|
|
import React, { useEffect, useState, useContext } from "react";
|
2022-01-06 01:26:15 +00:00
|
|
|
import QRCode from "qrcode.react";
|
|
|
|
import openSocket from "socket.io-client";
|
|
|
|
import toastError from "../../errors/toastError";
|
|
|
|
|
|
|
|
import { Dialog, DialogContent, Paper, Typography } from "@material-ui/core";
|
|
|
|
import { i18n } from "../../translate/i18n";
|
|
|
|
import api from "../../services/api";
|
|
|
|
|
2022-12-11 07:47:32 +00:00
|
|
|
import { AuthContext } from "../../context/Auth/AuthContext";
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const QrcodeModal = ({ open, onClose, whatsAppId }) => {
|
2022-12-11 07:47:32 +00:00
|
|
|
|
|
|
|
const { user } = useContext(AuthContext);
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const [qrCode, setQrCode] = useState("");
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchSession = async () => {
|
|
|
|
if (!whatsAppId) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { data } = await api.get(`/whatsapp/${whatsAppId}`);
|
|
|
|
setQrCode(data.qrcode);
|
|
|
|
} catch (err) {
|
|
|
|
toastError(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
fetchSession();
|
|
|
|
}, [whatsAppId]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!whatsAppId) return;
|
|
|
|
const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
|
|
|
|
|
|
|
|
socket.on("whatsappSession", data => {
|
|
|
|
if (data.action === "update" && data.session.id === whatsAppId) {
|
|
|
|
setQrCode(data.session.qrcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.action === "update" && data.session.qrcode === "") {
|
|
|
|
onClose();
|
|
|
|
}
|
2022-12-11 07:47:32 +00:00
|
|
|
|
|
|
|
if (data.action === "error") {
|
|
|
|
|
|
|
|
console.log('user.profile: ', user.profile)
|
|
|
|
|
|
|
|
if(user.profile === 'master'){
|
|
|
|
|
|
|
|
alert(data.msg)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
socket.disconnect();
|
|
|
|
};
|
2022-12-11 07:47:32 +00:00
|
|
|
}, [whatsAppId, onClose, user.profile]);
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog open={open} onClose={onClose} maxWidth="lg" scroll="paper">
|
|
|
|
<DialogContent>
|
|
|
|
<Paper elevation={0}>
|
|
|
|
<Typography color="primary" gutterBottom>
|
|
|
|
{i18n.t("qrCode.message")}
|
|
|
|
</Typography>
|
|
|
|
{qrCode ? (
|
|
|
|
<QRCode value={qrCode} size={256} />
|
|
|
|
) : (
|
|
|
|
<span>Waiting for QR Code</span>
|
|
|
|
)}
|
|
|
|
</Paper>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default React.memo(QrcodeModal);
|