2023-07-20 15:30:26 +00:00
|
|
|
import React, { useEffect, useReducer, useState, useContext } from 'react'
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-07-20 15:30:26 +00:00
|
|
|
import openSocket from 'socket.io-client'
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
IconButton,
|
|
|
|
makeStyles,
|
|
|
|
Paper,
|
|
|
|
Table,
|
|
|
|
TableBody,
|
|
|
|
TableCell,
|
|
|
|
TableHead,
|
|
|
|
TableRow,
|
|
|
|
Typography,
|
2023-07-20 15:30:26 +00:00
|
|
|
} from '@material-ui/core'
|
|
|
|
|
|
|
|
import MainContainer from '../../components/MainContainer'
|
|
|
|
import MainHeader from '../../components/MainHeader'
|
|
|
|
import MainHeaderButtonsWrapper from '../../components/MainHeaderButtonsWrapper'
|
|
|
|
import TableRowSkeleton from '../../components/TableRowSkeleton'
|
|
|
|
import Title from '../../components/Title'
|
|
|
|
import { i18n } from '../../translate/i18n'
|
|
|
|
import toastError from '../../errors/toastError'
|
|
|
|
import api from '../../services/api'
|
|
|
|
import { DeleteOutline, Edit } from '@material-ui/icons'
|
|
|
|
import QueueModal from '../../components/QueueModal'
|
|
|
|
import { toast } from 'react-toastify'
|
|
|
|
import ConfirmationModal from '../../components/ConfirmationModal'
|
|
|
|
|
|
|
|
import { AuthContext } from '../../context/Auth/AuthContext'
|
|
|
|
import { Can } from '../../components/Can'
|
2022-01-10 23:06:15 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const useStyles = makeStyles((theme) => ({
|
|
|
|
mainPaper: {
|
|
|
|
flex: 1,
|
|
|
|
padding: theme.spacing(1),
|
2023-07-20 15:30:26 +00:00
|
|
|
overflowY: 'scroll',
|
2022-01-06 01:26:15 +00:00
|
|
|
...theme.scrollbarStyles,
|
|
|
|
},
|
|
|
|
customTableCell: {
|
2023-07-20 15:30:26 +00:00
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
2022-01-06 01:26:15 +00:00
|
|
|
},
|
2023-07-20 15:30:26 +00:00
|
|
|
}))
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const reducer = (state, action) => {
|
2023-07-20 15:30:26 +00:00
|
|
|
if (action.type === 'LOAD_QUEUES') {
|
|
|
|
const queues = action.payload
|
|
|
|
const newQueues = []
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
queues.forEach((queue) => {
|
2023-07-20 15:30:26 +00:00
|
|
|
const queueIndex = state.findIndex((q) => q.id === queue.id)
|
2022-01-06 01:26:15 +00:00
|
|
|
if (queueIndex !== -1) {
|
2023-07-20 15:30:26 +00:00
|
|
|
state[queueIndex] = queue
|
2022-01-06 01:26:15 +00:00
|
|
|
} else {
|
2023-07-20 15:30:26 +00:00
|
|
|
newQueues.push(queue)
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
2023-07-20 15:30:26 +00:00
|
|
|
})
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-07-20 15:30:26 +00:00
|
|
|
return [...state, ...newQueues]
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 15:30:26 +00:00
|
|
|
if (action.type === 'UPDATE_QUEUES') {
|
|
|
|
const queue = action.payload
|
|
|
|
const queueIndex = state.findIndex((u) => u.id === queue.id)
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
if (queueIndex !== -1) {
|
2023-07-20 15:30:26 +00:00
|
|
|
state[queueIndex] = queue
|
|
|
|
return [...state]
|
2022-01-06 01:26:15 +00:00
|
|
|
} else {
|
2023-07-20 15:30:26 +00:00
|
|
|
return [queue, ...state]
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-20 15:30:26 +00:00
|
|
|
if (action.type === 'DELETE_QUEUE') {
|
|
|
|
const queueId = action.payload
|
|
|
|
const queueIndex = state.findIndex((q) => q.id === queueId)
|
2022-01-06 01:26:15 +00:00
|
|
|
if (queueIndex !== -1) {
|
2023-07-20 15:30:26 +00:00
|
|
|
state.splice(queueIndex, 1)
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
2023-07-20 15:30:26 +00:00
|
|
|
return [...state]
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 15:30:26 +00:00
|
|
|
if (action.type === 'RESET') {
|
|
|
|
return []
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
2023-07-20 15:30:26 +00:00
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const Queues = () => {
|
2023-07-20 15:30:26 +00:00
|
|
|
const classes = useStyles()
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-07-20 15:30:26 +00:00
|
|
|
const { user } = useContext(AuthContext)
|
2022-01-10 23:06:15 +00:00
|
|
|
|
2023-07-20 15:30:26 +00:00
|
|
|
const [queues, dispatch] = useReducer(reducer, [])
|
|
|
|
const [loading, setLoading] = useState(false)
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-07-20 15:30:26 +00:00
|
|
|
const [queueModalOpen, setQueueModalOpen] = useState(false)
|
|
|
|
const [selectedQueue, setSelectedQueue] = useState(null)
|
|
|
|
const [confirmModalOpen, setConfirmModalOpen] = useState(false)
|
|
|
|
|
|
|
|
const [settings, setSettings] = useState([])
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-07-20 15:30:26 +00:00
|
|
|
;(async () => {
|
|
|
|
setLoading(true)
|
2022-01-06 01:26:15 +00:00
|
|
|
try {
|
2023-07-20 15:30:26 +00:00
|
|
|
const { data } = await api.get('/queue')
|
|
|
|
dispatch({ type: 'LOAD_QUEUES', payload: data })
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-07-20 15:30:26 +00:00
|
|
|
setLoading(false)
|
2022-01-06 01:26:15 +00:00
|
|
|
} catch (err) {
|
2023-07-20 15:30:26 +00:00
|
|
|
toastError(err)
|
|
|
|
setLoading(false)
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
2023-07-20 15:30:26 +00:00
|
|
|
})()
|
|
|
|
}, [])
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-07-20 15:30:26 +00:00
|
|
|
const fetchSession = async () => {
|
|
|
|
try {
|
|
|
|
const { data } = await api.get('/settings')
|
|
|
|
setSettings(data)
|
|
|
|
} catch (err) {
|
|
|
|
toastError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fetchSession()
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const getSettingValue = (key) => {
|
|
|
|
const { value } = settings.find((s) => s.key === key)
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-07-20 15:30:26 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const socket = openSocket(process.env.REACT_APP_BACKEND_URL)
|
|
|
|
|
|
|
|
socket.on('queue', (data) => {
|
|
|
|
if (data.action === 'update' || data.action === 'create') {
|
|
|
|
dispatch({ type: 'UPDATE_QUEUES', payload: data.queue })
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 15:30:26 +00:00
|
|
|
if (data.action === 'delete') {
|
|
|
|
dispatch({ type: 'DELETE_QUEUE', payload: data.queueId })
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
2023-07-20 15:30:26 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
socket.on('settings', (data) => {
|
|
|
|
if (data.action === 'update') {
|
|
|
|
setSettings((prevState) => {
|
|
|
|
const aux = [...prevState]
|
|
|
|
const settingIndex = aux.findIndex((s) => s.key === data.setting.key)
|
|
|
|
aux[settingIndex].value = data.setting.value
|
|
|
|
return aux
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
return () => {
|
2023-07-20 15:30:26 +00:00
|
|
|
socket.disconnect()
|
|
|
|
}
|
|
|
|
}, [])
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const handleOpenQueueModal = () => {
|
2023-07-20 15:30:26 +00:00
|
|
|
setQueueModalOpen(true)
|
|
|
|
setSelectedQueue(null)
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const handleCloseQueueModal = () => {
|
2023-07-20 15:30:26 +00:00
|
|
|
setQueueModalOpen(false)
|
|
|
|
setSelectedQueue(null)
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const handleEditQueue = (queue) => {
|
2023-07-20 15:30:26 +00:00
|
|
|
setSelectedQueue(queue)
|
|
|
|
setQueueModalOpen(true)
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const handleCloseConfirmationModal = () => {
|
2023-07-20 15:30:26 +00:00
|
|
|
setConfirmModalOpen(false)
|
|
|
|
setSelectedQueue(null)
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const handleDeleteQueue = async (queueId) => {
|
|
|
|
try {
|
2023-07-20 15:30:26 +00:00
|
|
|
await api.delete(`/queue/${queueId}`)
|
|
|
|
toast.success(i18n.t('Queue deleted successfully!'))
|
2022-01-06 01:26:15 +00:00
|
|
|
} catch (err) {
|
2023-07-20 15:30:26 +00:00
|
|
|
toastError(err)
|
2022-01-06 01:26:15 +00:00
|
|
|
}
|
2023-07-20 15:30:26 +00:00
|
|
|
setSelectedQueue(null)
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2022-06-15 23:36:21 +00:00
|
|
|
return (
|
2022-01-10 23:06:15 +00:00
|
|
|
<Can
|
2022-06-15 23:36:21 +00:00
|
|
|
role={user.profile}
|
|
|
|
perform="queues-view:show"
|
|
|
|
yes={() => (
|
|
|
|
<MainContainer>
|
2022-01-10 23:06:15 +00:00
|
|
|
<ConfirmationModal
|
|
|
|
title={
|
|
|
|
selectedQueue &&
|
2023-07-20 15:30:26 +00:00
|
|
|
`${i18n.t('queues.confirmationModal.deleteTitle')} ${
|
|
|
|
selectedQueue.name
|
2022-01-10 23:06:15 +00:00
|
|
|
}?`
|
|
|
|
}
|
|
|
|
open={confirmModalOpen}
|
|
|
|
onClose={handleCloseConfirmationModal}
|
|
|
|
onConfirm={() => handleDeleteQueue(selectedQueue.id)}
|
2022-01-06 01:26:15 +00:00
|
|
|
>
|
2023-07-20 15:30:26 +00:00
|
|
|
{i18n.t('queues.confirmationModal.deleteMessage')}
|
2022-01-10 23:06:15 +00:00
|
|
|
</ConfirmationModal>
|
|
|
|
<QueueModal
|
|
|
|
open={queueModalOpen}
|
|
|
|
onClose={handleCloseQueueModal}
|
|
|
|
queueId={selectedQueue?.id}
|
|
|
|
/>
|
|
|
|
<MainHeader>
|
2023-07-20 15:30:26 +00:00
|
|
|
<Title>{i18n.t('queues.title')}</Title>
|
2022-06-15 23:36:21 +00:00
|
|
|
|
|
|
|
<Can
|
|
|
|
role={user.profile}
|
|
|
|
perform="show-icon-add-queue"
|
|
|
|
yes={() => (
|
|
|
|
<MainHeaderButtonsWrapper>
|
|
|
|
<Button
|
|
|
|
variant="contained"
|
|
|
|
color="primary"
|
|
|
|
onClick={handleOpenQueueModal}
|
|
|
|
>
|
2023-07-20 15:30:26 +00:00
|
|
|
{i18n.t('queues.buttons.add')}
|
2022-06-15 23:36:21 +00:00
|
|
|
</Button>
|
|
|
|
</MainHeaderButtonsWrapper>
|
|
|
|
)}
|
|
|
|
/>
|
2022-01-10 23:06:15 +00:00
|
|
|
</MainHeader>
|
|
|
|
<Paper className={classes.mainPaper} variant="outlined">
|
|
|
|
<Table size="small">
|
|
|
|
<TableHead>
|
|
|
|
<TableRow>
|
2022-01-06 01:26:15 +00:00
|
|
|
<TableCell align="center">
|
2023-07-20 15:30:26 +00:00
|
|
|
{i18n.t('queues.table.name')}
|
2022-01-06 01:26:15 +00:00
|
|
|
</TableCell>
|
|
|
|
<TableCell align="center">
|
2023-07-20 15:30:26 +00:00
|
|
|
{i18n.t('queues.table.color')}
|
2022-01-06 01:26:15 +00:00
|
|
|
</TableCell>
|
|
|
|
<TableCell align="center">
|
2023-07-20 15:30:26 +00:00
|
|
|
{i18n.t('queues.table.greeting')}
|
2022-01-10 23:06:15 +00:00
|
|
|
</TableCell>
|
|
|
|
<TableCell align="center">
|
2023-07-20 15:30:26 +00:00
|
|
|
{i18n.t('queues.table.actions')}
|
2022-01-06 01:26:15 +00:00
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
2022-01-10 23:06:15 +00:00
|
|
|
</TableHead>
|
|
|
|
<TableBody>
|
|
|
|
<>
|
|
|
|
{queues.map((queue) => (
|
|
|
|
<TableRow key={queue.id}>
|
|
|
|
<TableCell align="center">{queue.name}</TableCell>
|
|
|
|
<TableCell align="center">
|
|
|
|
<div className={classes.customTableCell}>
|
|
|
|
<span
|
|
|
|
style={{
|
|
|
|
backgroundColor: queue.color,
|
|
|
|
width: 60,
|
|
|
|
height: 20,
|
2023-07-20 15:30:26 +00:00
|
|
|
alignSelf: 'center',
|
2022-01-10 23:06:15 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</TableCell>
|
|
|
|
<TableCell align="center">
|
|
|
|
<div className={classes.customTableCell}>
|
|
|
|
<Typography
|
2023-07-20 15:30:26 +00:00
|
|
|
style={{ width: 300, align: 'center' }}
|
2022-01-10 23:06:15 +00:00
|
|
|
noWrap
|
|
|
|
variant="body2"
|
|
|
|
>
|
|
|
|
{queue.greetingMessage}
|
|
|
|
</Typography>
|
|
|
|
</div>
|
|
|
|
</TableCell>
|
2022-06-15 23:36:21 +00:00
|
|
|
|
2022-01-10 23:06:15 +00:00
|
|
|
<TableCell align="center">
|
2022-06-15 23:36:21 +00:00
|
|
|
<Can
|
|
|
|
role={user.profile}
|
|
|
|
perform="show-icon-edit-queue"
|
|
|
|
yes={() => (
|
2023-07-20 15:30:26 +00:00
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
margin: 0,
|
|
|
|
padding: 0,
|
|
|
|
border: 'none',
|
|
|
|
display: 'inline',
|
|
|
|
}}
|
2022-06-15 23:36:21 +00:00
|
|
|
>
|
2023-07-20 15:30:26 +00:00
|
|
|
{(settings &&
|
|
|
|
settings.length > 0 &&
|
|
|
|
getSettingValue('editQueue') &&
|
|
|
|
getSettingValue('editQueue') === 'enabled') |
|
|
|
|
(user.profile === 'master') ? (
|
|
|
|
<IconButton
|
|
|
|
size="small"
|
|
|
|
onClick={() => handleEditQueue(queue)}
|
|
|
|
>
|
|
|
|
<Edit />
|
|
|
|
</IconButton>
|
|
|
|
) : (
|
|
|
|
<div></div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
// <IconButton
|
|
|
|
// size="small"
|
|
|
|
// onClick={() => handleEditQueue(queue)}
|
|
|
|
// >
|
|
|
|
// <Edit />
|
|
|
|
// </IconButton>
|
2022-06-15 23:36:21 +00:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Can
|
|
|
|
role={user.profile}
|
|
|
|
perform="show-icon-delete-queue"
|
|
|
|
yes={() => (
|
|
|
|
<IconButton
|
|
|
|
size="small"
|
|
|
|
onClick={() => {
|
2023-07-20 15:30:26 +00:00
|
|
|
setSelectedQueue(queue)
|
|
|
|
setConfirmModalOpen(true)
|
2022-06-15 23:36:21 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<DeleteOutline />
|
|
|
|
</IconButton>
|
|
|
|
)}
|
|
|
|
/>
|
2022-01-10 23:06:15 +00:00
|
|
|
</TableCell>
|
|
|
|
</TableRow>
|
|
|
|
))}
|
|
|
|
{loading && <TableRowSkeleton columns={4} />}
|
|
|
|
</>
|
|
|
|
</TableBody>
|
|
|
|
</Table>
|
|
|
|
</Paper>
|
|
|
|
</MainContainer>
|
2022-06-15 23:36:21 +00:00
|
|
|
)}
|
|
|
|
/>
|
2023-07-20 15:30:26 +00:00
|
|
|
)
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-07-20 15:30:26 +00:00
|
|
|
export default Queues
|