index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import React, { useEffect, useState, useContext } from "react";
  2. import QRCode from "qrcode.react";
  3. import toastError from "../../errors/toastError";
  4. import { Dialog, DialogContent, Paper, Typography, useTheme } from "@material-ui/core";
  5. import { i18n } from "../../translate/i18n";
  6. import api from "../../services/api";
  7. import { SocketContext } from "../../context/Socket/SocketContext";
  8. const QrcodeModal = ({ open, onClose, whatsAppId }) => {
  9. const [qrCode, setQrCode] = useState("");
  10. const theme = useTheme();
  11. const socketManager = useContext(SocketContext);
  12. useEffect(() => {
  13. const fetchSession = async () => {
  14. if (!whatsAppId) return;
  15. try {
  16. const { data } = await api.get(`/whatsapp/${whatsAppId}`);
  17. setQrCode(data.qrcode);
  18. } catch (err) {
  19. toastError(err);
  20. }
  21. };
  22. fetchSession();
  23. }, [whatsAppId]);
  24. useEffect(() => {
  25. if (!whatsAppId) return;
  26. const companyId = localStorage.getItem("companyId");
  27. const socket = socketManager.getSocket(companyId);
  28. socket.on(`company-${companyId}-whatsappSession`, (data) => {
  29. if (data.action === "update" && data.session.id === whatsAppId) {
  30. setQrCode(data.session.qrcode);
  31. }
  32. if (data.action === "update" && data.session.qrcode === "") {
  33. onClose();
  34. }
  35. });
  36. return () => {
  37. socket.disconnect();
  38. };
  39. }, [whatsAppId, onClose, socketManager]);
  40. return (
  41. <Dialog open={open} onClose={onClose} maxWidth="lg" scroll="paper">
  42. <DialogContent>
  43. <Paper elevation={0} style={{ display: "flex", alignItems: "center" }}>
  44. <div style={{ marginRight: "20px" }}>
  45. <Typography variant="h2" component="h2" color="textPrimary" gutterBottom style={{ fontFamily: "Montserrat", fontWeight: "bold", fontSize:"20px",}}>
  46. {i18n.t("qrCodeModal.title")}
  47. </Typography>
  48. <Typography variant="body1" color="textPrimary" gutterBottom>
  49. {i18n.t("qrCodeModal.steps.one")}
  50. </Typography>
  51. <Typography variant="body1" color="textPrimary" gutterBottom>
  52. {i18n.t("qrCodeModal.steps.two.partOne")} <svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 8c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm0 2c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm0 6c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z"></path></svg> {i18n.t("qrCodeModal.steps.two.partTwo")} <svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true"><path d="M19.14 12.94c.04-.3.06-.61.06-.94 0-.32-.02-.64-.07-.94l2.03-1.58c.18-.14.23-.41.12-.61l-1.92-3.32c-.12-.22-.37-.29-.59-.22l-2.39.96c-.5-.38-1.03-.7-1.62-.94l-.36-2.54c-.04-.24-.24-.41-.48-.41h-3.84c-.24 0-.43.17-.47.41l-.36 2.54c-.59.24-1.13.57-1.62.94l-2.39-.96c-.22-.08-.47 0-.59.22L2.74 8.87c-.12.21-.08.47.12.61l2.03 1.58c-.05.3-.09.63-.09.94s.02.64.07.94l-2.03 1.58c-.18.14-.23.41-.12.61l1.92 3.32c.12.22.37.29.59.22l2.39-.96c.5.38 1.03.7 1.62.94l.36 2.54c.05.24.24.41.48.41h3.84c.24 0 .44-.17.47-.41l.36-2.54c.59-.24 1.13-.56 1.62-.94l2.39.96c.22.08.47 0 .59-.22l1.92-3.32c.12-.22.07-.47-.12-.61l-2.01-1.58zM12 15.6c-1.98 0-3.6-1.62-3.6-3.6s1.62-3.6 3.6-3.6 3.6 1.62 3.6 3.6-1.62 3.6-3.6 3.6z"></path></svg> {i18n.t("qrCodeModal.steps.two.partThree")}
  53. </Typography>
  54. <Typography variant="body1" color="textPrimary" gutterBottom>
  55. {i18n.t("qrCodeModal.steps.three")}
  56. </Typography>
  57. <Typography variant="body1" color="textPrimary" gutterBottom>
  58. {i18n.t("qrCodeModal.steps.four")}
  59. </Typography>
  60. </div>
  61. <div>
  62. {qrCode ? (
  63. <QRCode value={qrCode} size={256} />
  64. ) : (
  65. <span>{i18n.t("qrCodeModal.waiting")}</span>
  66. )}
  67. </div>
  68. </Paper>
  69. </DialogContent>
  70. </Dialog>
  71. );
  72. };
  73. export default React.memo(QrcodeModal);