2022-05-16 02:48:06 +00:00
import React , { useState , useEffect , useReducer } from "react" ;
import MainContainer from "../../components/MainContainer" ;
import api from "../../services/api" ;
2022-03-21 05:06:56 +00:00
//import { data } from '../../components/Report/MTable/data';
2022-05-16 02:48:06 +00:00
import DatePicker1 from '../../components/Report/DatePicker'
import DatePicker2 from '../../components/Report/DatePicker'
2022-03-21 05:06:56 +00:00
//import { Button } from "@material-ui/core";
2022-05-16 02:48:06 +00:00
2022-03-21 05:06:56 +00:00
import PropTypes from 'prop-types' ;
import Box from '@mui/material/Box' ;
2022-05-16 02:48:06 +00:00
2022-03-21 05:06:56 +00:00
import SearchIcon from "@material-ui/icons/Search" ;
import TextField from "@material-ui/core/TextField" ;
import InputAdornment from "@material-ui/core/InputAdornment" ;
import Button from "@material-ui/core/Button" ;
import MaterialTable from 'material-table' ;
import Delete from '@material-ui/icons/Delete' ;
2022-05-16 02:48:06 +00:00
import Edit from '@material-ui/icons/Edit' ;
2022-03-21 05:06:56 +00:00
2022-03-23 02:59:01 +00:00
import { render } from '@testing-library/react' ;
// import Modal from "../../../..ChatEnd/ModalChatEnd";
2022-05-16 02:48:06 +00:00
import Modal from "../../components/ModalUpdateScheduleReminder" ;
2022-03-23 02:59:01 +00:00
2022-03-29 21:32:48 +00:00
import openSocket from "socket.io-client" ;
2022-05-16 02:48:06 +00:00
import { toast } from "react-toastify" ;
import toastError from "../../errors/toastError" ;
2022-03-21 05:06:56 +00:00
import ConfirmationModal from "../../components/ConfirmationModal" ;
2022-05-16 02:48:06 +00:00
const reducerQ = ( state , action ) => {
2022-03-21 05:06:56 +00:00
2022-05-16 02:48:06 +00:00
if ( action . type === 'LOAD_QUERY' ) {
const queries = action . payload
2022-03-21 05:06:56 +00:00
const newQueries = [ ]
queries . forEach ( ( query ) => {
2022-05-16 02:48:06 +00:00
2022-03-21 05:06:56 +00:00
const queryIndex = state . findIndex ( ( q ) => q . id === query . id )
2022-05-16 02:48:06 +00:00
if ( queryIndex !== - 1 ) {
2022-03-21 05:06:56 +00:00
state [ queryIndex ] = query
}
2022-05-16 02:48:06 +00:00
else {
2022-03-21 05:06:56 +00:00
newQueries . push ( query )
}
} )
return [ ... state , ... newQueries ]
}
2022-03-29 21:32:48 +00:00
// if (action.type === "UPDATE_SCHEDULING") {
// const scheduling = action.payload;
// const schedulingIndex = state.findIndex((u) => u.id === +scheduling.id);
2022-03-30 14:09:20 +00:00
//
2022-03-29 21:32:48 +00:00
// if (schedulingIndex !== -1) {
// state[schedulingIndex] = scheduling;
// return [...state];
// } else {
// return [scheduling, ...state];
// }
// }
2022-05-16 02:48:06 +00:00
if ( action . type === "DELETE_SCHEDULING" ) {
2022-03-21 05:06:56 +00:00
const scheduleId = action . payload ;
2022-03-29 21:32:48 +00:00
const scheduleIndex = state . findIndex ( ( u ) => u . id === scheduleId ) ;
2022-05-16 02:48:06 +00:00
2022-03-21 05:06:56 +00:00
if ( scheduleIndex !== - 1 ) {
state . splice ( scheduleIndex , 1 ) ;
}
2022-05-16 02:48:06 +00:00
return [ ... state ] ;
2022-03-29 21:32:48 +00:00
2022-03-21 05:06:56 +00:00
}
2022-03-29 21:32:48 +00:00
// if (action.type === "DELETE_QUEUE") {
// const queueId = action.payload;
// const queueIndex = state.findIndex((q) => q.id === queueId);
// if (queueIndex !== -1) {
// state.splice(queueIndex, 1);
// }
// return [...state];
// }
2022-05-16 02:48:06 +00:00
2022-03-21 05:06:56 +00:00
if ( action . type === "RESET" ) {
return [ ] ;
}
}
2022-03-24 22:16:31 +00:00
// const reducer = (state, action) => {
2022-05-16 02:48:06 +00:00
2022-03-24 22:16:31 +00:00
// if (action.type === "LOAD_STATUS_CHAT_END") {
// const users = action.payload;
// const newUsers = [];
// users.forEach((user) => {
// const userIndex = state.findIndex((u) => u.id === user.id);
// if (userIndex !== -1) {
// state[userIndex] = user;
// } else {
// newUsers.push(user);
// }
// });
// return [...state, ...newUsers];
// }
// if (action.type === "RESET") {
// return [];
// }
// };
2022-03-21 05:06:56 +00:00
2022-05-16 02:48:06 +00:00
2022-03-21 05:06:56 +00:00
function Item ( props ) {
const { sx , ... other } = props ;
return (
< Box
sx = { {
bgcolor : ( theme ) => ( theme . palette . mode === 'dark' ? '#101010' : '#fff' ) ,
color : ( theme ) => ( theme . palette . mode === 'dark' ? 'grey.300' : 'grey.800' ) ,
border : '1px solid' ,
borderColor : ( theme ) =>
theme . palette . mode === 'dark' ? 'grey.800' : 'grey.300' ,
p : 1 ,
m : 1 ,
borderRadius : 2 ,
fontSize : '0.875rem' ,
fontWeight : '700' ,
... sx ,
} }
{ ... other }
/ >
) ;
}
Item . propTypes = {
sx : PropTypes . oneOfType ( [
PropTypes . arrayOf (
PropTypes . oneOfType ( [ PropTypes . func , PropTypes . object , PropTypes . bool ] ) ,
) ,
PropTypes . func ,
PropTypes . object ,
] ) ,
2022-05-16 02:48:06 +00:00
} ;
2022-03-21 05:06:56 +00:00
2022-05-16 02:48:06 +00:00
const SchedulesReminder = ( ) => {
2022-03-21 05:06:56 +00:00
//--------
const [ searchParam ] = useState ( "" ) ;
2022-03-24 22:16:31 +00:00
const [ loading , setLoading ] = useState ( null ) ;
2022-03-21 05:06:56 +00:00
//const [hasMore, setHasMore] = useState(false);
2022-05-16 02:48:06 +00:00
const [ pageNumber , setPageNumber ] = useState ( 1 ) ;
2022-03-24 22:16:31 +00:00
// const [users, dispatch] = useReducer(reducer, []);
2022-03-21 05:06:56 +00:00
//const [columns, setColums] = useState([])
2022-05-16 02:48:06 +00:00
const [ startDate , setDatePicker1 ] = useState ( new Date ( ) )
const [ endDate , setDatePicker2 ] = useState ( new Date ( ) )
const [ query , dispatchQ ] = useReducer ( reducerQ , [ ] )
2022-03-21 05:06:56 +00:00
const [ contactNumber , setContactNumber ] = useState ( "" ) ;
2022-05-16 02:48:06 +00:00
2022-03-21 05:06:56 +00:00
const [ resetChild , setReset ] = useState ( false )
const [ selectedSchedule , setSelectedSchedule ] = useState ( null ) ;
2022-05-16 02:48:06 +00:00
const [ confirmModalOpen , setConfirmModalOpen ] = useState ( false ) ;
2022-03-21 05:06:56 +00:00
const [ dataRows , setData ] = useState ( [ ] ) ;
2022-03-23 02:59:01 +00:00
const [ statusEndChat , setStatusEndChat ] = useState ( null )
2022-03-29 21:32:48 +00:00
useEffect ( ( ) => {
2022-05-16 02:48:06 +00:00
const socket = openSocket ( process . env . REACT _APP _BACKEND _URL ) ;
2022-03-29 21:32:48 +00:00
socket . on ( "schedulingNotify" , ( data ) => {
2022-05-16 02:48:06 +00:00
setLoading ( true ) ;
2022-03-29 21:32:48 +00:00
// if (data.action === "update" || data.action === "create") {
2022-03-30 14:09:20 +00:00
//
2022-03-29 21:32:48 +00:00
// // dispatchQ({ type: "UPDATE_SCHEDULING", payload: data.schedulingNotifyCreate });
// }
if ( data . action === "delete" ) {
2022-05-16 02:48:06 +00:00
2022-03-29 21:32:48 +00:00
dispatchQ ( { type : "DELETE_SCHEDULING" , payload : + data . schedulingNotifyId } ) ;
//handleDeleteRows(data.schedulingNotifyId)
}
setLoading ( false ) ;
} ) ;
return ( ) => {
socket . disconnect ( ) ;
} ;
} , [ ] ) ;
2022-05-16 02:48:06 +00:00
2022-03-21 05:06:56 +00:00
useEffect ( ( ) => {
2022-03-24 22:16:31 +00:00
// dispatch({ type: "RESET" });
2022-03-21 05:06:56 +00:00
dispatchQ ( { type : "RESET" } )
2022-05-16 02:48:06 +00:00
2022-03-21 05:06:56 +00:00
setPageNumber ( 1 ) ;
} , [ searchParam ] ) ;
2022-03-24 22:16:31 +00:00
//natalia
2022-03-21 05:06:56 +00:00
useEffect ( ( ) => {
2022-03-24 22:16:31 +00:00
// setLoading(true);
2022-03-21 05:06:56 +00:00
const delayDebounceFn = setTimeout ( ( ) => {
2022-03-24 22:16:31 +00:00
const fetchStatusChatEnd = async ( ) => {
2022-03-21 05:06:56 +00:00
try {
2022-03-24 22:16:31 +00:00
const statusChatEndLoad = await api . get ( "/statusChatEnd" , {
2022-03-21 05:06:56 +00:00
params : { searchParam , pageNumber } ,
2022-05-16 02:48:06 +00:00
} ) ;
2022-03-24 22:16:31 +00:00
// dispatch({ type: "LOAD_STATUS_CHAT_END", payload: statusChatEndLoad.data });
2022-05-16 02:48:06 +00:00
2022-03-24 22:16:31 +00:00
2022-03-30 14:09:20 +00:00
// setStatusEndChat(statusChatEndLoad.data.filter(status => (status.id == '2' || status.id == '3')))
setStatusEndChat ( statusChatEndLoad . data . filter ( status => ( ` ${ status . id } ` === '2' || ` ${ status . id } ` === '3' ) ) )
2022-03-21 05:06:56 +00:00
//setHasMore(data.hasMore);
2022-03-24 22:16:31 +00:00
// setLoading(false);
2022-03-21 05:06:56 +00:00
} catch ( err ) {
console . log ( err ) ;
}
} ;
2022-03-24 22:16:31 +00:00
fetchStatusChatEnd ( ) ;
2022-03-21 05:06:56 +00:00
} , 500 ) ;
return ( ) => clearTimeout ( delayDebounceFn ) ;
} , [ searchParam , pageNumber ] ) ;
2022-05-16 02:48:06 +00:00
useEffect ( ( ) => {
setLoading ( true ) ;
2022-03-21 05:06:56 +00:00
const delayDebounceFn = setTimeout ( ( ) => {
const fetchQueries = async ( ) => {
2022-05-16 02:48:06 +00:00
try {
2022-03-21 05:06:56 +00:00
2022-05-16 02:48:06 +00:00
const dataQuery = await api . get ( "/schedules/" , { params : { contactNumber , startDate , endDate } , } ) ;
2022-03-21 05:06:56 +00:00
dispatchQ ( { type : "RESET" } )
2022-05-16 02:48:06 +00:00
dispatchQ ( { type : "LOAD_QUERY" , payload : dataQuery . data } ) ;
2022-03-24 22:16:31 +00:00
setLoading ( false ) ;
2022-03-21 05:06:56 +00:00
} catch ( err ) {
console . log ( err ) ;
}
} ;
fetchQueries ( ) ;
} , 500 ) ;
return ( ) => clearTimeout ( delayDebounceFn ) ;
} , [ contactNumber , startDate , endDate ] ) ;
2022-03-24 22:16:31 +00:00
2022-05-16 02:48:06 +00:00
2022-03-24 22:16:31 +00:00
useEffect ( ( ) => {
2022-05-16 02:48:06 +00:00
if ( ! loading ) {
2022-03-28 19:28:35 +00:00
setData ( query . map ( ( { scheduleReminder , ... others } ) => (
2022-03-30 14:09:20 +00:00
{ ... others , 'scheduleReminder' : ` ${ others . statusChatEndId } ` === '3' ? 'Agendamento' : 'Lembrete' }
2022-05-16 02:48:06 +00:00
) ) )
2022-03-24 22:16:31 +00:00
}
2022-03-30 14:09:20 +00:00
} , [ loading , query ] )
2022-03-21 05:06:56 +00:00
2022-05-16 02:48:06 +00:00
// Get from child 1
const datePicker1Value = ( data ) => {
2022-03-21 05:06:56 +00:00
setDatePicker1 ( data )
2022-05-16 02:48:06 +00:00
}
// Get from child 2
const datePicker2Value = ( data ) => {
2022-03-21 05:06:56 +00:00
setDatePicker2 ( data )
2022-05-16 02:48:06 +00:00
}
2022-03-21 05:06:56 +00:00
2022-05-16 02:48:06 +00:00
const handleSearch = ( event ) => {
setContactNumber ( event . target . value . toLowerCase ( ) ) ;
} ;
2022-03-21 05:06:56 +00:00
2022-05-16 02:48:06 +00:00
const handleClear = ( ) => {
2022-03-21 05:06:56 +00:00
2022-05-16 02:48:06 +00:00
setContactNumber ( '' )
2022-03-21 05:06:56 +00:00
2022-05-16 02:48:06 +00:00
setReset ( true )
2022-03-21 05:06:56 +00:00
2022-05-16 02:48:06 +00:00
}
2022-03-21 05:06:56 +00:00
2022-05-16 02:48:06 +00:00
const handleCloseConfirmationModal = ( ) => {
setConfirmModalOpen ( false ) ;
2022-03-21 05:06:56 +00:00
2022-05-16 02:48:06 +00:00
setSelectedSchedule ( null ) ;
} ;
// const handleDeleteRows = (id) => {
// let _data = [...dataRows];
// _data.forEach(rd => {
// _data = _data.filter(t => t.id !== id);
// });
// setData(_data);
// };
const handleDeleteSchedule = async ( scheduleId ) => {
try {
await api . delete ( ` /schedule/ ${ scheduleId } ` ) ;
toast . success ( ( "Lembrete/Agendamento deletado com sucesso!" ) ) ;
//handleDeleteRows(scheduleId)
} catch ( err ) {
toastError ( err ) ;
}
setSelectedSchedule ( null ) ;
} ;
2022-03-21 05:06:56 +00:00
2022-03-24 22:16:31 +00:00
2022-05-16 02:48:06 +00:00
const handleUpdateSchedule = async ( scheduleData , rowsDataNew ) => {
try {
2022-03-28 19:28:35 +00:00
2022-05-16 02:48:06 +00:00
await api . post ( "/schedule" , scheduleData ) ;
toast . success ( ( "Lembrete/Agendamento atualizado com sucesso!" ) ) ;
//////////////////
2022-03-28 19:28:35 +00:00
2022-05-16 02:48:06 +00:00
const dataUpdate = [ ... dataRows ] ;
const index = rowsDataNew . tableData [ 'id' ] ;
dataUpdate [ index ] = rowsDataNew ;
setData ( [ ... dataUpdate ] . map ( ( { scheduleReminder , ... others } ) => (
{ ... others , 'scheduleReminder' : ` ${ others . statusChatEndId } ` === '3' ? 'Agendamento' : 'Lembrete' }
2022-03-28 19:28:35 +00:00
) ) ) ;
2022-03-24 22:16:31 +00:00
2022-03-23 02:59:01 +00:00
2022-03-24 22:16:31 +00:00
2022-05-16 02:48:06 +00:00
/////////////////
2022-03-24 22:16:31 +00:00
2022-05-16 02:48:06 +00:00
} catch ( err ) {
toastError ( err ) ;
}
//
setSelectedSchedule ( null ) ;
} ;
const chatEndVal = ( data , rowsDataNew ) => {
if ( data ) {
2022-03-23 02:59:01 +00:00
2022-03-28 19:28:35 +00:00
2022-05-16 02:48:06 +00:00
handleUpdateSchedule ( data , rowsDataNew )
}
}
const handleModal = ( rowData ) => {
// NATY
render ( < Modal
modal _header = { 'Editar Lembrete/Agendamentos' }
func = { chatEndVal }
statusChatEnd = { statusEndChat }
2022-03-28 19:28:35 +00:00
// textBoxFieldSelected={'2'}
2022-03-24 22:16:31 +00:00
rowData = { rowData }
2022-05-16 02:48:06 +00:00
/ > )
} ;
return (
2022-03-23 02:59:01 +00:00
2022-05-16 02:48:06 +00:00
< MainContainer >
< Box sx = { { display : 'grid' , gridTemplateColumns : 'repeat(3, 1fr)' } } >
< Item >
< TextField
placeholder = 'Numero/Nome...'
type = "search"
value = { contactNumber }
onChange = { handleSearch }
InputProps = { {
startAdornment : (
< InputAdornment position = "start" >
< SearchIcon style = { { color : "gray" } } / >
< / I n p u t A d o r n m e n t >
) ,
} }
/ >
< / I t e m >
< Item > < DatePicker1 func = { datePicker1Value } minDate = { false } startEmpty = { true } reset = { resetChild } setReset = { setReset } title = { 'Data inicio' } / > < / I t e m >
< Item > < DatePicker2 func = { datePicker2Value } minDate = { false } startEmpty = { true } reset = { resetChild } setReset = { setReset } title = { 'Data fim' } / > < / I t e m >
< Item sx = { { gridColumn : '4 / 5' } } >
{ /* <Button size="small" variant="contained" onClick={()=>{handleQuery()}}>GO</Button>*/ }
< Button
variant = "contained"
color = "primary"
onClick = { ( e ) => handleClear ( ) }
>
{ "CLEAR" }
< / B u t t o n >
< / I t e m >
< / B o x >
< Box sx = { {
display : 'grid' ,
} } >
< Item sx = { { gridColumn : '1' , gridRow : 'span 1' } } >
< ConfirmationModal
title = { selectedSchedule && ` Deletar ${ '' + selectedSchedule . statusChatEndId === '2' ? 'lembrete' : 'agendamento' } do dia ${ selectedSchedule . schedulingDate . split ( ' ' ) [ 0 ] } ${ selectedSchedule . schedulingDate . split ( ' ' ) [ 1 ] } ? ` }
open = { confirmModalOpen }
onClose = { handleCloseConfirmationModal }
onConfirm = { ( ) => handleDeleteSchedule ( selectedSchedule . id ) }
>
< span > Deseja realmente deletar esse lembrete / agendamento ? < / s p a n >
< / C o n f i r m a t i o n M o d a l >
< MaterialTable
title = "Lembretes/Agendamentos"
columns = {
[
{ title : 'Foto' , field : 'ticket.contact.profilePicUrl' , render : rowData => < img src = { rowData [ 'ticket.contact.profilePicUrl' ] } alt = "imagem de perfil do whatsapp" style = { { width : 40 , borderRadius : '50%' } } / > } ,
{ title : 'Nome' , field : 'ticket.contact.name' } ,
{ title : 'Contato' , field : 'ticket.contact.number' } ,
{ title : 'Lemb/Agen' , field : 'scheduleReminder' } ,
{ title : 'Envio' , field : 'schedulingTime' } ,
{ title : 'Data' , field : 'schedulingDate' } ,
{ title : 'Mensagem' , field : 'message' , width : "80%" } ,
]
}
data = { dataRows }
// icons={tableIcons}
actions = { [
{
icon : Edit ,
tooltip : 'Editar' ,
onClick : ( event , rowData ) => {
setSelectedSchedule ( rowData ) ;
handleModal ( rowData )
}
} ,
{
icon : Delete ,
tooltip : 'Deletar' ,
onClick : ( event , rowData ) => {
setSelectedSchedule ( rowData ) ;
setConfirmModalOpen ( true ) ;
}
// onClick: handleDeleteRows
}
] }
options = {
{
search : true ,
selection : false ,
paging : false ,
padding : 'dense' ,
sorting : true ,
//loadingType: 'linear',
searchFieldStyle : {
width : 300 ,
} ,
pageSize : 20 ,
headerStyle : {
position : "sticky" ,
top : "0"
} ,
maxBodyHeight : "400px" ,
rowStyle : {
fontSize : 12 ,
2022-03-24 22:16:31 +00:00
}
2022-05-16 02:48:06 +00:00
} }
/ >
< / I t e m >
< / B o x >
< / M a i n C o n t a i n e r >
2022-03-21 05:06:56 +00:00
)
2022-05-16 02:48:06 +00:00
2022-03-21 05:06:56 +00:00
} ;
export default SchedulesReminder ;