2023-07-12 14:54:29 +00:00
|
|
|
import React, { useState, useContext, useMemo } from "react";
|
2022-01-06 01:26:15 +00:00
|
|
|
import { useHistory } from "react-router-dom";
|
|
|
|
|
|
|
|
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";
|
|
|
|
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 api from "../../services/api";
|
|
|
|
import ButtonWithSpinner from "../ButtonWithSpinner";
|
|
|
|
import toastError from "../../errors/toastError";
|
2023-07-12 14:54:29 +00:00
|
|
|
|
|
|
|
import { WhatsAppsContext } from "../../context/WhatsApp/WhatsAppsContext";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const useStyles = makeStyles((theme) => ({
|
2023-07-12 14:54:29 +00:00
|
|
|
maxWidth: {
|
|
|
|
width: "100%",
|
|
|
|
},
|
2022-01-06 01:26:15 +00:00
|
|
|
}));
|
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
// Receive array of queues arrays
|
|
|
|
// Return a new array with unique queues from all arrays has passed by the parameter
|
|
|
|
const queueArraysToOneArray = (array) => {
|
|
|
|
if (!array) return []
|
|
|
|
const map = {}
|
|
|
|
const uniqueQueuesAvailable = []
|
|
|
|
array.forEach((queues) => {
|
|
|
|
queues.forEach(({ id, name, color }) => {
|
|
|
|
if (!map[id]) {
|
|
|
|
map[id] = true
|
|
|
|
uniqueQueuesAvailable.push({ id, name, color })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return uniqueQueuesAvailable
|
|
|
|
}
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const TransferTicketModal = ({ modalOpen, onClose, ticketid }) => {
|
|
|
|
const history = useHistory();
|
2023-07-12 14:54:29 +00:00
|
|
|
const { whatsApps } = useContext(WhatsAppsContext);
|
2022-01-06 01:26:15 +00:00
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [selectedQueue, setSelectedQueue] = useState('');
|
|
|
|
const classes = useStyles();
|
2023-07-12 14:54:29 +00:00
|
|
|
const queues = useMemo(() => {
|
|
|
|
if (!whatsApps) return []
|
|
|
|
const whatsAppsQueues = whatsApps.map(({ queues }) => queues)
|
|
|
|
//const whatsAppsQueues = whatsApps.filter(({ status }) => status === "CONNECTED" ).map(({ queues }) => queues)
|
|
|
|
const uniqueQueuesAvailable = queueArraysToOneArray(whatsAppsQueues)
|
|
|
|
return uniqueQueuesAvailable
|
|
|
|
}, [whatsApps])
|
|
|
|
const [itemHover, setItemHover] = useState(-1)
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
onClose();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSaveTicket = async e => {
|
|
|
|
e.preventDefault();
|
|
|
|
if (!ticketid) return;
|
2023-07-12 14:54:29 +00:00
|
|
|
if (!selectedQueue) return;
|
2022-01-06 01:26:15 +00:00
|
|
|
setLoading(true);
|
|
|
|
try {
|
|
|
|
let data = {};
|
|
|
|
|
|
|
|
if (selectedQueue && selectedQueue !== null) {
|
|
|
|
data.queueId = selectedQueue
|
|
|
|
}
|
|
|
|
|
2022-04-18 18:21:28 +00:00
|
|
|
// test del PARA APARECER NA FILA DE OUTRO ATENDENTE E O MESMO CLICAR EM ACEITAR AO INVES DE ENVIAR PARA ATENDENDO
|
|
|
|
data.status = 'pending'
|
2023-07-12 14:54:29 +00:00
|
|
|
data.transfer = true
|
2022-04-18 18:21:28 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
await api.put(`/tickets/${ticketid}`, data);
|
|
|
|
|
|
|
|
setLoading(false);
|
|
|
|
history.push(`/tickets`);
|
|
|
|
} catch (err) {
|
|
|
|
setLoading(false);
|
|
|
|
toastError(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog open={modalOpen} onClose={handleClose} maxWidth="lg" scroll="paper">
|
|
|
|
<form onSubmit={handleSaveTicket}>
|
|
|
|
<DialogTitle id="form-dialog-title">
|
|
|
|
{i18n.t("transferTicketModal.title")}
|
|
|
|
</DialogTitle>
|
|
|
|
<DialogContent dividers>
|
|
|
|
<FormControl variant="outlined" className={classes.maxWidth}>
|
|
|
|
<InputLabel>{i18n.t("transferTicketModal.fieldQueueLabel")}</InputLabel>
|
|
|
|
<Select
|
|
|
|
value={selectedQueue}
|
|
|
|
onChange={(e) => setSelectedQueue(e.target.value)}
|
|
|
|
label={i18n.t("transferTicketModal.fieldQueuePlaceholder")}
|
2023-07-12 14:54:29 +00:00
|
|
|
required
|
2022-01-06 01:26:15 +00:00
|
|
|
>
|
2023-07-12 14:54:29 +00:00
|
|
|
<MenuItem style={{ background: "white", }} value={''}> </MenuItem>
|
2022-01-06 01:26:15 +00:00
|
|
|
{queues.map((queue) => (
|
2023-07-12 14:54:29 +00:00
|
|
|
<MenuItem
|
|
|
|
key={queue.id}
|
|
|
|
value={queue.id}
|
|
|
|
onMouseEnter={() => setItemHover(queue.id)}
|
|
|
|
onMouseLeave={() => setItemHover(-1)}
|
|
|
|
style={{
|
|
|
|
background: queue.id !== itemHover ? "white" : queue.color,
|
|
|
|
}}
|
|
|
|
>{queue.name}
|
|
|
|
</MenuItem>
|
2022-01-06 01:26:15 +00:00
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
</FormControl>
|
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
|
|
|
<Button
|
|
|
|
onClick={handleClose}
|
|
|
|
color="secondary"
|
|
|
|
disabled={loading}
|
|
|
|
variant="outlined"
|
|
|
|
>
|
|
|
|
{i18n.t("transferTicketModal.buttons.cancel")}
|
|
|
|
</Button>
|
|
|
|
<ButtonWithSpinner
|
|
|
|
variant="contained"
|
|
|
|
type="submit"
|
|
|
|
color="primary"
|
|
|
|
loading={loading}
|
|
|
|
>
|
|
|
|
{i18n.t("transferTicketModal.buttons.ok")}
|
|
|
|
</ButtonWithSpinner>
|
|
|
|
</DialogActions>
|
|
|
|
</form>
|
2023-07-12 14:54:29 +00:00
|
|
|
</Dialog >
|
2022-01-06 01:26:15 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
export default TransferTicketModal;
|