2023-09-08 19:50:51 +00:00
|
|
|
import React, { useState, useEffect, useContext, useRef, useCallback } from "react"
|
|
|
|
import { useHistory } from "react-router-dom"
|
|
|
|
import { toast } from "react-toastify"
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
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"
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
import DialogActions from "@material-ui/core/DialogActions"
|
|
|
|
import DialogContent from "@material-ui/core/DialogContent"
|
|
|
|
import DialogTitle from "@material-ui/core/DialogTitle"
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
import { i18n } from "../../translate/i18n"
|
|
|
|
import ButtonWithSpinner from "../ButtonWithSpinner"
|
|
|
|
import { AuthContext } from "../../context/Auth/AuthContext"
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
import toastError from "../../errors/toastError"
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
import api from "../../services/api"
|
2023-07-12 14:54:29 +00:00
|
|
|
|
|
|
|
const useStyles = makeStyles((theme) => ({
|
|
|
|
maxWidth: {
|
|
|
|
width: "100%",
|
|
|
|
},
|
|
|
|
paper: {
|
|
|
|
minWidth: "300px"
|
|
|
|
},
|
|
|
|
linearProgress: {
|
|
|
|
marginTop: "5px"
|
|
|
|
}
|
2023-09-08 19:50:51 +00:00
|
|
|
}))
|
2023-07-12 14:54:29 +00:00
|
|
|
|
|
|
|
const ContactCreateTicketModal = ({ modalOpen, onClose, contactId }) => {
|
2023-09-08 19:50:51 +00:00
|
|
|
const { user } = useContext(AuthContext)
|
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
let isMounted = useRef(true)
|
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
const history = useHistory()
|
|
|
|
const [queues, setQueues] = useState([])
|
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
const [selectedQueue, setSelectedQueue] = useState('')
|
2023-07-12 14:54:29 +00:00
|
|
|
const [itemHover, setItemHover] = useState(-1)
|
2023-09-08 19:50:51 +00:00
|
|
|
|
|
|
|
const [selectedWhatsId, setSelectedWhatsId] = useState()
|
|
|
|
const [whatsQueue, setWhatsQueue] = useState()
|
|
|
|
const [disabled, setDisabled] = useState(false)
|
|
|
|
|
|
|
|
const classes = useStyles()
|
2023-07-12 14:54:29 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-09-08 19:50:51 +00:00
|
|
|
|
|
|
|
const delayDebounceFn = setTimeout(() => {
|
|
|
|
|
|
|
|
const fetchMatchQueueUserOfficialWhatsapp = async () => {
|
|
|
|
try {
|
|
|
|
|
|
|
|
const { data } = await api.get("/whatsapp/official/matchQueueUser", { params: { userId: user.id, queueId: selectedQueue }, })
|
|
|
|
|
|
|
|
setQueues(data)
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchMatchQueueUserOfficialWhatsapp()
|
|
|
|
|
|
|
|
}, 500)
|
2023-09-16 14:45:44 +00:00
|
|
|
return () => clearTimeout(delayDebounceFn)
|
2023-09-08 19:50:51 +00:00
|
|
|
|
2023-09-16 14:45:44 +00:00
|
|
|
}, [user, selectedQueue])
|
2023-07-12 14:54:29 +00:00
|
|
|
|
|
|
|
const handleClose = () => {
|
2023-09-08 19:50:51 +00:00
|
|
|
onClose()
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleSaveTicket = useCallback(async (contactId, userId, queueId, selectedWhatsId, whatsQueue, queues) => {
|
|
|
|
|
2023-09-16 14:45:44 +00:00
|
|
|
if (queues && queues.length === 1 && queues[0].disable) {
|
2023-09-08 19:50:51 +00:00
|
|
|
toast.warn('Não é possível criar um Ticket porque a fila a qual você esta adicionado(a) não está associado a nenhum numero!')
|
|
|
|
return
|
|
|
|
}
|
2023-07-12 14:54:29 +00:00
|
|
|
|
|
|
|
if (!contactId || !userId) {
|
|
|
|
console.log("Missing contactId or userId")
|
|
|
|
return
|
|
|
|
};
|
|
|
|
if (!queueId) {
|
|
|
|
toast.warning("Nenhuma Fila Selecionada")
|
|
|
|
return
|
|
|
|
}
|
2023-09-08 19:50:51 +00:00
|
|
|
if (isMounted.current) setLoading(true)
|
|
|
|
|
|
|
|
if (whatsQueue && !selectedWhatsId) {
|
|
|
|
toast.warn('Selecione um numero!')
|
|
|
|
return
|
|
|
|
}
|
2023-07-12 14:54:29 +00:00
|
|
|
|
|
|
|
const delayDebounceFn = setTimeout(() => {
|
|
|
|
const ticketCreate = async () => {
|
|
|
|
try {
|
|
|
|
const { data: ticket } = await api.post("/tickets", {
|
|
|
|
contactId: contactId,
|
|
|
|
userId: userId,
|
|
|
|
queueId: queueId,
|
|
|
|
status: "open",
|
2023-09-08 19:50:51 +00:00
|
|
|
whatsappId: selectedWhatsId
|
|
|
|
})
|
|
|
|
history.push(`/tickets/${ticket.id}`)
|
|
|
|
|
|
|
|
setWhatsQueue(null)
|
|
|
|
setSelectedWhatsId(null)
|
2023-07-12 14:54:29 +00:00
|
|
|
} catch (err) {
|
2023-09-08 19:50:51 +00:00
|
|
|
toastError(err)
|
2023-07-12 14:54:29 +00:00
|
|
|
}
|
2023-09-08 19:50:51 +00:00
|
|
|
if (isMounted.current) setLoading(false)
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
}
|
|
|
|
ticketCreate()
|
|
|
|
}, 300)
|
|
|
|
return () => clearTimeout(delayDebounceFn)
|
2023-07-12 14:54:29 +00:00
|
|
|
|
|
|
|
}, [history])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-09-08 19:50:51 +00:00
|
|
|
|
2023-09-16 14:45:44 +00:00
|
|
|
if (selectedQueue && (selectedQueue.length === 0 || !selectedQueue)) {
|
2023-09-08 19:50:51 +00:00
|
|
|
setDisabled(true)
|
|
|
|
setWhatsQueue(null)
|
2023-07-12 14:54:29 +00:00
|
|
|
}
|
2023-09-08 19:50:51 +00:00
|
|
|
|
|
|
|
if (!Array.isArray(whatsQueue)) {
|
|
|
|
setSelectedWhatsId(null)
|
|
|
|
setWhatsQueue(null)
|
|
|
|
}
|
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
return () => {
|
2023-09-08 19:50:51 +00:00
|
|
|
isMounted.current = false
|
|
|
|
}
|
|
|
|
}, [modalOpen, contactId, user.id, selectedQueue, handleSaveTicket, queues.length, selectedWhatsId, whatsQueue, queues])
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
|
|
if (!selectedQueue) return
|
|
|
|
|
|
|
|
setDisabled(true)
|
|
|
|
|
|
|
|
const delayDebounceFn = setTimeout(() => {
|
|
|
|
|
|
|
|
const fetChMatchQueueOfficialWhatsapp = async () => {
|
|
|
|
try {
|
|
|
|
|
|
|
|
const { data } = await api.get("/whatsapp/official/matchQueue", { params: { userId: user.id, queueId: selectedQueue }, })
|
|
|
|
|
2023-09-16 14:45:44 +00:00
|
|
|
console.log('WHATSAPP DATA: ', data)
|
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
setWhatsQueue(data)
|
|
|
|
|
|
|
|
setDisabled(false)
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetChMatchQueueOfficialWhatsapp()
|
|
|
|
|
|
|
|
}, 500)
|
|
|
|
return () => clearTimeout(delayDebounceFn)
|
|
|
|
|
|
|
|
}, [selectedQueue, user.id])
|
|
|
|
|
2023-09-16 14:45:44 +00:00
|
|
|
useEffect(() => {
|
|
|
|
console.log('selectedWhatsId: ', selectedWhatsId)
|
|
|
|
console.log('whatsQuee: ', whatsQueue)
|
|
|
|
}, [whatsQueue])
|
2023-07-12 14:54:29 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog open={modalOpen} onClose={handleClose} maxWidth="xs" scroll="paper" classes={{ paper: classes.paper }}>
|
|
|
|
<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")}
|
|
|
|
>
|
|
|
|
<MenuItem value={''}> </MenuItem>
|
2023-09-08 19:50:51 +00:00
|
|
|
{queues.map(({ id, color, name, disable }) => (
|
2023-07-12 14:54:29 +00:00
|
|
|
<MenuItem
|
|
|
|
key={id}
|
2023-09-08 19:50:51 +00:00
|
|
|
disabled={disable}
|
2023-07-12 14:54:29 +00:00
|
|
|
value={id}
|
|
|
|
onMouseEnter={() => setItemHover(id)}
|
|
|
|
onMouseLeave={() => setItemHover(-1)}
|
|
|
|
style={{
|
|
|
|
background: id !== itemHover ? "white" : color,
|
|
|
|
}}
|
|
|
|
>{name[0].toUpperCase() + name.slice(1).toLowerCase()}</MenuItem>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
</FormControl>
|
|
|
|
</DialogContent>
|
2023-09-08 19:50:51 +00:00
|
|
|
|
|
|
|
{whatsQueue && Array.isArray(whatsQueue) ?
|
|
|
|
<DialogContent dividers>
|
|
|
|
<FormControl variant="outlined" className={classes.maxWidth}>
|
|
|
|
<InputLabel>{i18n.t("Selecionar Numero")}</InputLabel>
|
|
|
|
<Select
|
|
|
|
value={selectedWhatsId}
|
|
|
|
onChange={(e) => setSelectedWhatsId(e.target.value)}
|
|
|
|
label={i18n.t("Numeros")}
|
|
|
|
>
|
|
|
|
<MenuItem value={''}> </MenuItem>
|
|
|
|
{whatsQueue.map(({ id, number, phoneNumberId }) => (
|
|
|
|
<MenuItem
|
|
|
|
key={id}
|
|
|
|
value={id}
|
|
|
|
>{phoneNumberId ? `${number} OFICIAL` : `${number}`}</MenuItem>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
</FormControl>
|
|
|
|
</DialogContent>
|
|
|
|
: <></>
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-07-12 14:54:29 +00:00
|
|
|
<DialogActions>
|
|
|
|
<Button
|
|
|
|
onClick={handleClose}
|
|
|
|
color="secondary"
|
|
|
|
disabled={loading}
|
|
|
|
variant="outlined"
|
|
|
|
>
|
|
|
|
{i18n.t("newTicketModal.buttons.cancel")}
|
|
|
|
</Button>
|
|
|
|
<ButtonWithSpinner
|
2023-09-08 19:50:51 +00:00
|
|
|
onClick={() => handleSaveTicket(contactId, user.id, selectedQueue, selectedWhatsId, whatsQueue, queues)}
|
2023-07-12 14:54:29 +00:00
|
|
|
variant="contained"
|
|
|
|
color="primary"
|
|
|
|
loading={loading}
|
2023-09-08 19:50:51 +00:00
|
|
|
disabled={disabled}
|
2023-07-12 14:54:29 +00:00
|
|
|
>
|
|
|
|
{i18n.t("newTicketModal.buttons.ok")}
|
|
|
|
</ButtonWithSpinner>
|
|
|
|
</DialogActions>
|
|
|
|
</Dialog>
|
2023-09-08 19:50:51 +00:00
|
|
|
)
|
|
|
|
}
|
2023-07-12 14:54:29 +00:00
|
|
|
|
2023-09-08 19:50:51 +00:00
|
|
|
export default ContactCreateTicketModal
|