| 6956 |
stevensc |
1 |
import React, { useCallback, useEffect, useRef, useState } from 'react'
|
|
|
2 |
import { axios } from '../../utils'
|
|
|
3 |
import { useDispatch, useSelector } from 'react-redux'
|
|
|
4 |
import { addNotification } from '../../redux/notification/notification.actions'
|
|
|
5 |
import { Col, Container, Row } from 'react-bootstrap'
|
|
|
6 |
import QuestionAnswerRoundedIcon from '@mui/icons-material/QuestionAnswerRounded'
|
|
|
7 |
import SearchIcon from '@mui/icons-material/Search'
|
|
|
8 |
|
|
|
9 |
import EmptySection from '../../components/UI/EmptySection'
|
|
|
10 |
import InmailChat from '../../components/chat/InmailChat'
|
|
|
11 |
import Contacts from '../../components/chat/Contacts'
|
|
|
12 |
import StartConversationModal from '../../components/modals/StartConversationModal'
|
|
|
13 |
|
|
|
14 |
const InmailPage = () => {
|
|
|
15 |
const [conversations, setConversations] = useState([])
|
|
|
16 |
const [currentConversation, setCurrentConversation] = useState(null)
|
|
|
17 |
const [search, setSearch] = useState('')
|
|
|
18 |
const [loading, setLoading] = useState(false)
|
|
|
19 |
const [showConversationModal, setShowConversationModal] = useState(false)
|
|
|
20 |
const options = useRef([
|
|
|
21 |
{
|
|
|
22 |
label: 'Iniciar conversación',
|
|
|
23 |
action: () => toggleConversationModal(),
|
|
|
24 |
},
|
|
|
25 |
])
|
|
|
26 |
const labels = useSelector(({ intl }) => intl.labels)
|
|
|
27 |
const dispatch = useDispatch()
|
|
|
28 |
|
|
|
29 |
const toggleConversationModal = () => {
|
|
|
30 |
setShowConversationModal(!showConversationModal)
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
const getConversations = async () => {
|
|
|
34 |
try {
|
|
|
35 |
setLoading(true)
|
|
|
36 |
const { data } = await axios.get(window.location.href)
|
|
|
37 |
if (data.success) setConversations(data.data)
|
|
|
38 |
} catch (error) {
|
|
|
39 |
const errorMessage = new Error(error)
|
|
|
40 |
dispatch(addNotification({ style: 'danger', msg: errorMessage.message }))
|
|
|
41 |
} finally {
|
|
|
42 |
setLoading(false)
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
const changeConversation = (conversation) => {
|
|
|
47 |
setCurrentConversation(conversation)
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
const filterConversations = useCallback(
|
|
|
51 |
() =>
|
|
|
52 |
conversations.filter((conversation) =>
|
|
|
53 |
conversation.name.toLowerCase().includes(search.toLowerCase())
|
|
|
54 |
),
|
|
|
55 |
|
|
|
56 |
[search, conversations]
|
|
|
57 |
)
|
|
|
58 |
|
|
|
59 |
useEffect(() => {
|
|
|
60 |
if (loading) return
|
|
|
61 |
|
|
|
62 |
const messagesInterval = setTimeout(() => {
|
|
|
63 |
getConversations()
|
|
|
64 |
}, 2000)
|
|
|
65 |
|
|
|
66 |
return () => {
|
|
|
67 |
clearTimeout(messagesInterval)
|
|
|
68 |
}
|
|
|
69 |
}, [loading])
|
|
|
70 |
|
|
|
71 |
return (
|
|
|
72 |
<>
|
|
|
73 |
<Container>
|
|
|
74 |
<Row>
|
|
|
75 |
<Col md="4">
|
|
|
76 |
<Contacts>
|
|
|
77 |
<Contacts.Header options={options}>
|
|
|
78 |
<h1>Chat</h1>
|
|
|
79 |
</Contacts.Header>
|
|
|
80 |
|
|
|
81 |
<div className="contact__search show">
|
|
|
82 |
<SearchIcon />
|
|
|
83 |
<input
|
|
|
84 |
type="text"
|
|
|
85 |
placeholder="Buscar"
|
|
|
86 |
onChange={(e) => setSearch(e.target.value)}
|
|
|
87 |
/>
|
|
|
88 |
</div>
|
|
|
89 |
|
|
|
90 |
<Contacts.List
|
|
|
91 |
contacts={filterConversations()}
|
|
|
92 |
onChange={changeConversation}
|
|
|
93 |
currentConversation={currentConversation}
|
|
|
94 |
/>
|
|
|
95 |
</Contacts>
|
|
|
96 |
</Col>
|
|
|
97 |
<Col md="8" className="d-flex">
|
|
|
98 |
{currentConversation ? (
|
|
|
99 |
<InmailChat
|
|
|
100 |
conversation={currentConversation}
|
|
|
101 |
changeConversation={changeConversation}
|
|
|
102 |
/>
|
|
|
103 |
) : (
|
|
|
104 |
<EmptySection
|
|
|
105 |
message={labels.select_conversation}
|
|
|
106 |
Icon={<QuestionAnswerRoundedIcon />}
|
|
|
107 |
align="center"
|
|
|
108 |
/>
|
|
|
109 |
)}
|
|
|
110 |
</Col>
|
|
|
111 |
</Row>
|
|
|
112 |
</Container>
|
|
|
113 |
<StartConversationModal
|
|
|
114 |
show={showConversationModal}
|
|
|
115 |
setConversation={changeConversation}
|
|
|
116 |
onClose={toggleConversationModal}
|
|
|
117 |
/>
|
|
|
118 |
</>
|
|
|
119 |
)
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
export default InmailPage
|