2022-02-27 20:05:21 +00:00
2022-03-12 05:13:15 +00:00
import React , { useState , useEffect , useRef , useReducer } from 'react' ;
2022-02-27 04:35:56 +00:00
import Button from '@mui/material/Button' ;
import Dialog from '@mui/material/Dialog' ;
2022-03-12 05:13:15 +00:00
import DialogActions from '@mui/material/DialogActions' ;
2022-02-27 04:35:56 +00:00
import DialogContent from '@mui/material/DialogContent' ;
import DialogContentText from '@mui/material/DialogContentText' ;
2022-03-10 11:24:10 +00:00
import DialogTitle from '@mui/material/DialogTitle' ;
2022-02-27 04:35:56 +00:00
import PropTypes from 'prop-types' ;
2022-03-10 11:24:10 +00:00
import Box from '@mui/material/Box' ;
2022-03-12 05:13:15 +00:00
import SelectField from "../../Report/SelectField" ;
2022-03-17 11:58:22 +00:00
2022-03-10 11:24:10 +00:00
import DatePicker from '../../Report/DatePicker'
2022-03-12 05:13:15 +00:00
import TimerPickerSelect from '../TimerPickerSelect'
import TextareaAutosize from '@mui/material/TextareaAutosize' ;
2022-03-17 11:58:22 +00:00
// import { subHours } from "date-fns";
2022-03-12 05:13:15 +00:00
2022-03-10 11:24:10 +00:00
import {
2022-03-12 05:13:15 +00:00
IconButton ,
2022-03-10 11:24:10 +00:00
Paper ,
Table ,
TableBody ,
TableCell ,
TableHead ,
2022-03-12 05:13:15 +00:00
TableRow ,
} from "@material-ui/core" ;
import { DeleteOutline } from "@material-ui/icons" ;
import { toast } from "react-toastify" ; import api from "../../../services/api" ;
import toastError from "../../../errors/toastError" ;
import ConfirmationModal from "../../ConfirmationModal" ;
const reducer = ( state , action ) => {
if ( action . type === "LOAD_SCHEDULES" ) {
const schedulesContact = action . payload ;
const newSchedules = [ ] ;
schedulesContact . forEach ( ( schedule ) => {
const scheduleIndex = state . findIndex ( ( s ) => s . id === schedule . id ) ;
if ( scheduleIndex !== - 1 ) {
state [ scheduleIndex ] = schedule ;
} else {
newSchedules . push ( schedule ) ;
}
} ) ;
return [ ... state , ... newSchedules ] ;
}
if ( action . type === "DELETE_SCHEDULE" ) {
const scheduleId = action . payload ;
const scheduleIndex = state . findIndex ( ( q ) => q . id === scheduleId ) ;
if ( scheduleIndex !== - 1 ) {
state . splice ( scheduleIndex , 1 ) ;
}
return [ ... state ] ;
}
if ( action . type === "RESET" ) {
return [ ] ;
}
} ;
2022-02-27 04:35:56 +00:00
2022-02-27 20:05:21 +00:00
const Item = ( props ) => {
2022-02-27 04:35:56 +00:00
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-03-11 03:01:58 +00:00
2022-02-27 04:35:56 +00:00
2022-03-12 05:13:15 +00:00
const Modal = ( props ) => {
2022-03-07 02:19:35 +00:00
2022-03-12 05:13:15 +00:00
// const [clientSchedules, dispatch] = useReducer(reducer, []);
const [ selectedSchedule , setSelectedSchedule ] = useState ( null ) ;
const [ confirmModalOpen , setConfirmModalOpen ] = useState ( false ) ;
2022-03-07 02:19:35 +00:00
2022-02-27 20:05:21 +00:00
const [ open , setOpen ] = useState ( true ) ;
const [ scroll , /*setScroll*/ ] = useState ( 'body' ) ;
2022-02-28 18:17:36 +00:00
const [ scheduleId , setScheduling ] = useState ( null )
2022-03-07 02:19:35 +00:00
const [ startDate , setDatePicker ] = useState ( new Date ( ) )
2022-02-27 20:05:21 +00:00
const [ timerPicker , setTimerPicker ] = useState ( new Date ( ) )
2022-03-07 02:19:35 +00:00
const [ textArea1 , setTextArea1 ] = useState ( )
2022-03-06 19:37:09 +00:00
2022-02-28 13:51:11 +00:00
2022-03-07 02:19:35 +00:00
const [ data ] = useState ( props . schedules )
2022-03-11 03:01:58 +00:00
2022-03-12 05:13:15 +00:00
const [ schedulesContact , dispatch ] = useReducer ( reducer , [ ] ) ;
2022-03-17 11:58:22 +00:00
2022-02-27 04:35:56 +00:00
2022-02-28 13:51:11 +00:00
const handleCancel = ( event , reason ) => {
2022-02-27 04:35:56 +00:00
2022-02-27 20:05:21 +00:00
if ( reason && reason === "backdropClick" )
2022-02-27 04:35:56 +00:00
return ;
2022-03-07 02:19:35 +00:00
2022-02-27 04:35:56 +00:00
setOpen ( false ) ;
} ;
2022-03-06 19:37:09 +00:00
2022-03-07 02:19:35 +00:00
2022-03-12 05:13:15 +00:00
useEffect ( ( ) => {
( async ( ) => {
try {
const { data } = await api . get ( "/tickets/" + props . ticketId ) ;
dispatch ( { type : "LOAD_SCHEDULES" , payload : data . schedulesContact } ) ;
} catch ( err ) {
toastError ( err ) ;
}
} ) ( ) ;
2022-03-17 11:58:22 +00:00
} , [ props ] ) ;
2022-02-28 13:51:11 +00:00
2022-03-06 19:37:09 +00:00
function formatedTimeHour ( timer ) {
return ` ${ timer . getHours ( ) . toString ( ) . padStart ( 2 , '0' ) } : ${ timer . getMinutes ( ) . toString ( ) . padStart ( 2 , '0' ) } `
}
2022-03-17 11:58:22 +00:00
function formatedFullCurrentDate ( ) {
let dateCurrent = new Date ( )
let day = dateCurrent . getDate ( ) . toString ( ) . padStart ( 2 , '0' ) ;
let month = ( dateCurrent . getMonth ( ) + 1 ) . toString ( ) . padStart ( 2 , '0' ) ;
let year = dateCurrent . getFullYear ( ) ;
return ` ${ year } - ${ month } - ${ day } ` ;
}
2022-03-12 05:13:15 +00:00
2022-03-11 03:01:58 +00:00
2022-03-12 05:13:15 +00:00
const handleCloseConfirmationModal = ( ) => {
setConfirmModalOpen ( false ) ;
setSelectedSchedule ( null ) ;
} ;
const handleDeleteSchedule = async ( scheduleId ) => {
try {
await api . delete ( ` /schedule/ ${ scheduleId } ` ) ;
2022-03-17 11:58:22 +00:00
toast . success ( ( "Lembrete deletado com sucesso!" ) ) ;
2022-03-12 05:13:15 +00:00
dispatch ( { type : "DELETE_SCHEDULE" , payload : scheduleId } ) ;
} catch ( err ) {
toastError ( err ) ;
}
setSelectedSchedule ( null ) ;
} ;
2022-03-07 02:19:35 +00:00
// Get from child 2
const datePickerValue = ( data ) => {
console . log ( 'datePickerValue: ' , ( data ) ) ;
setDatePicker ( data )
2022-03-14 13:21:50 +00:00
2022-03-07 02:19:35 +00:00
}
// Get from child 3
const timerPickerValue = ( data ) => {
console . log ( 'timerPickerValue: ' , ( data ) ) ;
setTimerPicker ( data )
2022-03-14 13:21:50 +00:00
2022-03-07 02:19:35 +00:00
}
2022-03-17 11:58:22 +00:00
2022-03-07 02:19:35 +00:00
const handleChatEnd = ( event , reason ) => {
2022-03-17 11:58:22 +00:00
let dataSendServer = { 'scheduleId' : scheduleId }
2022-02-27 21:04:31 +00:00
if ( reason && reason === "backdropClick" )
return ;
2022-03-06 19:37:09 +00:00
2022-03-17 11:58:22 +00:00
if ( scheduleId === '2' ) {
2022-03-12 05:13:15 +00:00
2022-03-17 11:58:22 +00:00
console . log ( 'Entrou! textArea1: ' , textArea1 )
2022-03-12 05:13:15 +00:00
2022-03-17 11:58:22 +00:00
if ( textArea1 && textArea1 . trim ( ) . length < 5 ) {
alert ( 'Mensagem muito curta!' )
return
}
else if ( ! textArea1 ) {
alert ( 'Defina uma mensagem para enviar para o cliente!' )
return
}
else if ( formatedFullCurrentDate ( ) === startDate ) {
if ( ( new Date ( timerPicker ) . getHours ( ) < new Date ( ) . getHours ( ) && new Date ( timerPicker ) . getMinutes ( ) <= new Date ( ) . getMinutes ( ) ) ||
( new Date ( timerPicker ) . getHours ( ) === new Date ( ) . getHours ( ) && new Date ( timerPicker ) . getMinutes ( ) <= new Date ( ) . getMinutes ( ) ) ||
( new Date ( timerPicker ) . getHours ( ) < new Date ( ) . getHours ( ) && new Date ( timerPicker ) . getMinutes ( ) >= new Date ( ) . getMinutes ( ) ) ||
( new Date ( timerPicker ) . getHours ( ) < new Date ( ) . getHours ) ) {
alert ( 'Horário menor ou igual horário atual!' )
return
}
2022-03-12 05:13:15 +00:00
2022-03-17 11:58:22 +00:00
}
else if ( ( new Date ( timerPicker ) . getHours ( ) > 20 && new Date ( timerPicker ) . getMinutes ( ) > 0 ) ||
( new Date ( timerPicker ) . getHours ( ) < 6 ) ) {
alert ( 'Horário comercial inválido!\n Selecione um horário de lembrete válido entre às 06:00 e 20:00' )
return
}
dataSendServer = {
'scheduleId' : scheduleId ,
'schedulingDate' : startDate + ' ' + formatedTimeHour ( new Date ( ` ${ startDate } ${ timerPicker . getHours ( ) } : ${ timerPicker . getMinutes ( ) } :00 ` ) ) ,
'schedulingTime' : startDate + ' ' + formatedTimeHour ( new Date ( ` ${ startDate } ${ timerPicker . getHours ( ) } : ${ timerPicker . getMinutes ( ) } :00 ` ) ) ,
'message' : textArea1
}
2022-02-28 13:51:11 +00:00
2022-03-17 11:58:22 +00:00
}
props . func ( dataSendServer )
2022-03-10 11:24:10 +00:00
2022-02-27 21:04:31 +00:00
setOpen ( false ) ;
} ;
2022-03-06 19:37:09 +00:00
2022-02-27 21:04:31 +00:00
2022-02-27 20:05:21 +00:00
const descriptionElementRef = useRef ( null ) ;
useEffect ( ( ) => {
2022-02-27 04:35:56 +00:00
if ( open ) {
const { current : descriptionElement } = descriptionElementRef ;
if ( descriptionElement !== null ) {
descriptionElement . focus ( ) ;
}
}
} , [ open ] ) ;
// Get from child 1
const textFieldSelect = ( data ) => {
console . log ( 'textFieldSelect: ' , data ) ;
setScheduling ( data )
}
2022-03-17 11:58:22 +00:00
2022-03-12 05:13:15 +00:00
2022-03-06 19:37:09 +00:00
const handleChange = ( event ) => {
setTextArea1 ( event . target . value ) ;
2022-02-27 21:04:31 +00:00
} ;
2022-03-11 03:01:58 +00:00
2022-02-27 04:35:56 +00:00
2022-03-14 13:21:50 +00:00
return (
2022-02-27 20:05:21 +00:00
2022-03-14 13:21:50 +00:00
< Dialog
open = { open }
onClose = { handleCancel }
// fullWidth={true}
// maxWidth={true}
disableEscapeKeyDown
scroll = { scroll }
aria - labelledby = "scroll-dialog-title"
aria - describedby = "scroll-dialog-description"
>
< DialogTitle id = "scroll-dialog-title" > { props . modal _header } < / D i a l o g T i t l e >
< DialogContent dividers = { scroll === 'body' } >
< DialogContentText
id = "scroll-dialog-description"
ref = { descriptionElementRef }
tabIndex = { - 1 }
>
< / D i a l o g C o n t e n t T e x t >
< Box
sx = { {
width : 500 ,
height : '100%' ,
// backgroundColor: 'primary.dark',
// '&:hover': {backgroundColor: 'primary.main', opacity: [0.9, 0.8, 0.7],},
} } >
< Box sx = { {
display : 'grid' ,
} } >
< Item >
< span > Selecione uma opção para encerrar o Atendimento < / s p a n >
< SelectField func = { textFieldSelect }
emptyField = { false }
header = { 'Opções de encerramento do atendimento' }
currencies = { data . map ( ( obj ) => {
return { 'value' : obj . id , 'label' : obj . name }
} ) } / >
< / I t e m >
< / B o x >
{ scheduleId === '2' &&
< Item >
2022-03-17 11:58:22 +00:00
< span > Lembrete < / s p a n >
2022-03-14 13:21:50 +00:00
< Box sx = { { display : 'grid' , gridTemplateColumns : 'repeat(2, 1fr)' } } >
2022-03-11 03:01:58 +00:00
2022-03-17 11:58:22 +00:00
< Item > < DatePicker func = { datePickerValue } minDate = { true } title = { 'Data do lembrete' } / > < / I t e m >
2022-03-11 03:01:58 +00:00
2022-03-17 11:58:22 +00:00
< Item > < TimerPickerSelect func = { timerPickerValue } title = { 'Hora do lembrete' } / > < / I t e m >
2022-03-14 13:21:50 +00:00
< / B o x >
2022-03-17 11:58:22 +00:00
< Box sx = { { display : 'flex' , flexDirection : 'column' } } >
2022-03-14 13:21:50 +00:00
< Item >
< TextareaAutosize
aria - label = "minimum height"
minRows = { 3 }
value = { textArea1 }
placeholder = { 'Mensagem para lembrar cliente' }
onChange = { handleChange }
style = { { width : '100%' } }
/ >
< / I t e m >
< / B o x >
2022-02-27 04:35:56 +00:00
2022-03-14 13:21:50 +00:00
< / I t e m >
}
2022-02-27 20:05:21 +00:00
2022-03-14 13:21:50 +00:00
{ schedulesContact . length > 0 &&
2022-02-27 04:35:56 +00:00
2022-03-11 03:01:58 +00:00
2022-03-14 13:21:50 +00:00
< Item >
< ConfirmationModal
title = { selectedSchedule && ` Deletar agendamento do dia ${ selectedSchedule . schedulingTime . split ( ' ' ) [ 0 ] } ${ selectedSchedule . schedulingTime . split ( ' ' ) [ 1 ] } ? ` }
open = { confirmModalOpen }
onClose = { handleCloseConfirmationModal }
onConfirm = { ( ) => handleDeleteSchedule ( selectedSchedule . id ) }
>
2022-03-17 11:58:22 +00:00
< span > Deseja realmente deletar esse Lembrete ? < / s p a n >
2022-03-14 13:21:50 +00:00
< / C o n f i r m a t i o n M o d a l >
2022-03-17 11:58:22 +00:00
< span > Lembrete < / s p a n >
2022-03-14 13:21:50 +00:00
< Paper variant = "outlined" >
< Table size = "small" >
< TableHead >
< TableRow >
< TableCell align = "center" >
Data
< / T a b l e C e l l >
< TableCell align = "center" >
Hora
< / T a b l e C e l l >
2022-03-17 11:58:22 +00:00
< TableCell align = "center" >
Mensagem
< / T a b l e C e l l >
2022-03-14 13:21:50 +00:00
< TableCell align = "center" >
Deletar
2022-03-12 05:13:15 +00:00
< / T a b l e C e l l >
< / T a b l e R o w >
2022-03-14 13:21:50 +00:00
< / T a b l e H e a d >
< TableBody >
< >
{ schedulesContact . map ( ( scheduleData , index ) => (
< TableRow key = { scheduleData . id } >
< TableCell align = "center" > { scheduleData . schedulingDate . split ( ' ' ) [ 0 ] } < / T a b l e C e l l >
< TableCell align = "center" > { scheduleData . schedulingTime . split ( ' ' ) [ 1 ] } < / T a b l e C e l l >
2022-03-17 11:58:22 +00:00
< TableCell align = "center" > { scheduleData . message } < / T a b l e C e l l >
2022-03-14 13:21:50 +00:00
< TableCell align = "center" >
< IconButton
size = "small"
onClick = { ( ) => {
setSelectedSchedule ( scheduleData ) ;
setConfirmModalOpen ( true ) ;
} }
>
< DeleteOutline / >
< / I c o n B u t t o n >
< / T a b l e C e l l >
< / T a b l e R o w >
) ) }
< / >
< / T a b l e B o d y >
< / T a b l e >
< / P a p e r >
< / I t e m > }
< / B o x >
< / D i a l o g C o n t e n t >
< DialogActions >
< div style = { { marginRight : '50px' } } >
< Button onClick = { handleCancel } > Cancelar < / B u t t o n >
< / d i v >
< Button onClick = { handleChatEnd } > Ok < / B u t t o n >
< / D i a l o g A c t i o n s >
< / D i a l o g >
2022-02-27 04:35:56 +00:00
) ;
}
export default Modal