2023-06-28 22:48:29 +00:00
|
|
|
import React, { useState, useEffect, useContext, useRef, useCallback } from "react";
|
2023-06-26 17:46:29 +00:00
|
|
|
import { useHistory } from "react-router-dom";
|
|
|
|
import { toast } from "react-toastify";
|
|
|
|
|
|
|
|
import Button from "@material-ui/core/Button";
|
|
|
|
import Dialog from "@material-ui/core/Dialog";
|
|
|
|
import Select from "@material-ui/core/Select";
|
|
|
|
import FormControl from "@material-ui/core/FormControl";
|
|
|
|
import InputLabel from "@material-ui/core/InputLabel";
|
|
|
|
import MenuItem from "@material-ui/core/MenuItem";
|
2023-06-28 22:48:29 +00:00
|
|
|
import LinearProgress from "@material-ui/core/LinearProgress";
|
2023-06-26 17:46:29 +00:00
|
|
|
import { makeStyles } from "@material-ui/core";
|
|
|
|
|
|
|
|
import DialogActions from "@material-ui/core/DialogActions";
|
|
|
|
import DialogContent from "@material-ui/core/DialogContent";
|
|
|
|
import DialogTitle from "@material-ui/core/DialogTitle";
|
|
|
|
|
|
|
|
import { i18n } from "../../translate/i18n";
|
|
|
|
import ButtonWithSpinner from "../ButtonWithSpinner";
|
|
|
|
import { AuthContext } from "../../context/Auth/AuthContext";
|
|
|
|
|
|
|
|
import toastError from "../../errors/toastError";
|
|
|
|
|
|
|
|
import api from "../../services/api";
|
|
|
|
|
|
|
|
const useStyles = makeStyles((theme) => ({
|
|
|
|
maxWidth: {
|
|
|
|
width: "100%",
|
|
|
|
},
|
|
|
|
paper: {
|
|
|
|
minWidth: "300px"
|
2023-06-28 22:48:29 +00:00
|
|
|
},
|
|
|
|
linearProgress: {
|
|
|
|
marginTop: "5px"
|
2023-06-26 17:46:29 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
const ContactCreateTicketModal = ({ modalOpen, onClose, contactId }) => {
|
|
|
|
const { user } = useContext(AuthContext);
|
2023-06-28 22:48:29 +00:00
|
|
|
let isMounted = useRef(true)
|
2023-06-26 17:46:29 +00:00
|
|
|
|
|
|
|
const history = useHistory();
|
|
|
|
const [queues, setQueues] = useState([]);
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [selectedQueue, setSelectedQueue] = useState('');
|
|
|
|
const classes = useStyles();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const userQueues = user.queues.map(({ id, name, color }) => { return { id, name, color } })
|
2023-06-28 22:48:29 +00:00
|
|
|
if (userQueues.length === 1) setSelectedQueue(userQueues[0].id)
|
2023-06-26 17:46:29 +00:00
|
|
|
setQueues(userQueues)
|
|
|
|
}, [user]);
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
onClose();
|
|
|
|
};
|
|
|
|
|
2023-06-28 22:48:29 +00:00
|
|
|
const handleSaveTicket = useCallback(async (contactId, userId, queueId) => {
|
|
|
|
if (!contactId || !userId) {
|
|
|
|
console.log("Missing contactId or userId")
|
|
|
|
return
|
|
|
|
};
|
|
|
|
if (!queueId) {
|
2023-06-26 17:46:29 +00:00
|
|
|
toast.warning("Nenhuma Fila Selecionada")
|
|
|
|
return
|
|
|
|
}
|
2023-06-28 22:48:29 +00:00
|
|
|
if (isMounted.current) setLoading(true);
|
2023-06-26 17:46:29 +00:00
|
|
|
try {
|
|
|
|
const { data: ticket } = await api.post("/tickets", {
|
|
|
|
contactId: contactId,
|
2023-06-28 22:48:29 +00:00
|
|
|
userId: userId,
|
|
|
|
queueId: queueId,
|
2023-06-26 17:46:29 +00:00
|
|
|
status: "open",
|
|
|
|
});
|
|
|
|
history.push(`/tickets/${ticket.id}`);
|
|
|
|
} catch (err) {
|
|
|
|
toastError(err);
|
|
|
|
}
|
2023-06-28 22:48:29 +00:00
|
|
|
if (isMounted.current) setLoading(false);
|
|
|
|
}, [history])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (modalOpen && queues.length <= 1) {
|
|
|
|
handleSaveTicket(contactId, user.id, selectedQueue)
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
isMounted.current = false;
|
|
|
|
};
|
|
|
|
}, [modalOpen, contactId, user.id, selectedQueue, handleSaveTicket, queues.length]);
|
|
|
|
|
|
|
|
if (modalOpen && queues.length <= 1) {
|
|
|
|
return <LinearProgress />
|
|
|
|
}
|
2023-06-26 17:46:29 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog open={modalOpen} onClose={handleClose} maxWidth="xs" scroll="paper" classes={{ paper: classes.paper }}>
|
2023-06-28 22:48:29 +00:00
|
|
|
<DialogTitle id="form-dialog-title">
|
|
|
|
{i18n.t("newTicketModal.title")}
|
|
|
|
</DialogTitle>
|
|
|
|
<DialogContent dividers>
|
|
|
|
<FormControl variant="outlined" className={classes.maxWidth}>
|
|
|
|
<InputLabel>{i18n.t("Selecionar Fila")}</InputLabel>
|
|
|
|
<Select
|
|
|
|
value={selectedQueue}
|
|
|
|
onChange={(e) => setSelectedQueue(e.target.value)}
|
|
|
|
label={i18n.t("Filas")}
|
2023-06-26 17:46:29 +00:00
|
|
|
>
|
2023-06-28 22:48:29 +00:00
|
|
|
<MenuItem value={''}> </MenuItem>
|
|
|
|
{queues.map(({ id, name }) => (
|
|
|
|
<MenuItem key={id} value={id}>{name[0].toUpperCase() + name.slice(1).toLowerCase()}</MenuItem>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
</FormControl>
|
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
|
|
|
<Button
|
|
|
|
onClick={handleClose}
|
|
|
|
color="secondary"
|
|
|
|
disabled={loading}
|
|
|
|
variant="outlined"
|
|
|
|
>
|
|
|
|
{i18n.t("newTicketModal.buttons.cancel")}
|
|
|
|
</Button>
|
|
|
|
<ButtonWithSpinner
|
|
|
|
onClick={() => handleSaveTicket(contactId, user.id, selectedQueue)}
|
|
|
|
variant="contained"
|
|
|
|
color="primary"
|
|
|
|
loading={loading}
|
|
|
|
>
|
|
|
|
{i18n.t("newTicketModal.buttons.ok")}
|
|
|
|
</ButtonWithSpinner>
|
|
|
|
</DialogActions>
|
2023-06-26 17:46:29 +00:00
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ContactCreateTicketModal;
|