| 6956 |
stevensc |
1 |
import React, { useCallback, useEffect, useState } from 'react'
|
|
|
2 |
import { axios } from '../../utils'
|
|
|
3 |
import { Tab, Tabs } from '@mui/material'
|
|
|
4 |
import styled from 'styled-components'
|
|
|
5 |
import SearchIcon from '@mui/icons-material/Search'
|
|
|
6 |
|
|
|
7 |
import Contacts from './Contacts'
|
|
|
8 |
import ContactsModal from '../modals/ContactsModal'
|
|
|
9 |
import CreateGroupModal from '../modals/CreateGroupModal'
|
|
|
10 |
|
|
|
11 |
const notifyAudio = new Audio('/audio/chat.mp3')
|
|
|
12 |
|
|
|
13 |
const StyledTab = styled(Tab)`
|
|
|
14 |
flex-grow: 1;
|
|
|
15 |
color: var(--subtitle-color) !important;
|
|
|
16 |
font-weight: bold !important;
|
|
|
17 |
padding: 0.5rem 1rem !important;
|
|
|
18 |
min-height: auto !important;
|
|
|
19 |
&.Mui-selected {
|
|
|
20 |
color: var(--font-color) !important;
|
|
|
21 |
}
|
|
|
22 |
`
|
|
|
23 |
|
|
|
24 |
const ChatContacts = ({ selectedConversation, changeConversation }) => {
|
|
|
25 |
const [showContactModal, setShowContactModal] = useState(false)
|
|
|
26 |
const [showGroupModal, setShowGroupModal] = useState(false)
|
|
|
27 |
const [conversations, setConversations] = useState([])
|
|
|
28 |
const [search, setSearch] = useState('')
|
|
|
29 |
const [tab, setTab] = useState('user')
|
|
|
30 |
|
|
|
31 |
const options = [
|
|
|
32 |
{
|
|
|
33 |
label: 'Iniciar conversación',
|
|
|
34 |
action: () => setShowContactModal(!showContactModal),
|
|
|
35 |
},
|
|
|
36 |
{ label: 'Crear grupo', action: () => setShowGroupModal(!showGroupModal) },
|
|
|
37 |
]
|
|
|
38 |
|
|
|
39 |
const onCreateConversation = (url) => {
|
|
|
40 |
const conversation = conversations.find((user) => user.url_send === url)
|
|
|
41 |
changeConversation(conversation)
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
const heartBeat = async () => {
|
|
|
45 |
axios.get('/chat/heart-beat').then((response) => {
|
|
|
46 |
const { data: entities, success } = response.data
|
|
|
47 |
|
|
|
48 |
if (!success) {
|
|
|
49 |
return
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
entities.map(
|
|
|
53 |
(entity) =>
|
|
|
54 |
entity.not_received_messages &&
|
|
|
55 |
playNewMessage(entity.url_mark_received)
|
|
|
56 |
)
|
|
|
57 |
|
|
|
58 |
setConversations(entities)
|
|
|
59 |
})
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
const playNewMessage = async (url) => {
|
|
|
63 |
notifyAudio.play()
|
|
|
64 |
const { data } = await axios.post(url)
|
|
|
65 |
return data.success
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
const getConversations = useCallback(
|
|
|
69 |
() => conversations.filter((conversation) => conversation.type === tab),
|
|
|
70 |
[tab, conversations]
|
|
|
71 |
)
|
|
|
72 |
|
|
|
73 |
const filterConversations = useCallback(
|
|
|
74 |
(conversations) =>
|
|
|
75 |
conversations.filter((conversation) =>
|
|
|
76 |
conversation.name.toLowerCase().includes(search.toLowerCase())
|
|
|
77 |
),
|
|
|
78 |
|
|
|
79 |
[search]
|
|
|
80 |
)
|
|
|
81 |
|
|
|
82 |
useEffect(() => {
|
|
|
83 |
const heartBeatInterval = setInterval(heartBeat, 3000)
|
|
|
84 |
heartBeat()
|
|
|
85 |
|
|
|
86 |
return () => {
|
|
|
87 |
clearInterval(heartBeatInterval)
|
|
|
88 |
}
|
|
|
89 |
}, [])
|
|
|
90 |
|
|
|
91 |
return (
|
|
|
92 |
<>
|
|
|
93 |
<Contacts>
|
|
|
94 |
<Contacts.Header options={options}>
|
|
|
95 |
<h1>Chat</h1>
|
|
|
96 |
</Contacts.Header>
|
|
|
97 |
<Tabs
|
|
|
98 |
value={tab}
|
|
|
99 |
onChange={(e, newValue) => setTab(newValue)}
|
|
|
100 |
sx={{
|
|
|
101 |
width: '100%',
|
|
|
102 |
minHeight: 'auto',
|
|
|
103 |
'& .MuiTabs-indicator': {
|
|
|
104 |
backgroundColor: 'var(--font-color)',
|
|
|
105 |
},
|
|
|
106 |
}}
|
|
|
107 |
>
|
|
|
108 |
<StyledTab label="Personas" value="user" disableRipple />
|
|
|
109 |
<StyledTab label="Grupos" value="group" disableRipple />
|
|
|
110 |
</Tabs>
|
|
|
111 |
|
|
|
112 |
<div className="contact__search show">
|
|
|
113 |
<SearchIcon />
|
|
|
114 |
<input
|
|
|
115 |
type="text"
|
|
|
116 |
placeholder="Buscar"
|
|
|
117 |
onChange={(e) => setSearch(e.target.value)}
|
|
|
118 |
/>
|
|
|
119 |
</div>
|
|
|
120 |
|
|
|
121 |
<Contacts.List
|
|
|
122 |
contacts={filterConversations(getConversations())}
|
|
|
123 |
onChange={changeConversation}
|
|
|
124 |
currentConversation={selectedConversation}
|
|
|
125 |
/>
|
|
|
126 |
</Contacts>
|
|
|
127 |
<ContactsModal
|
|
|
128 |
show={showContactModal}
|
|
|
129 |
onClose={() => setShowContactModal(false)}
|
|
|
130 |
onComplete={onCreateConversation}
|
|
|
131 |
/>
|
|
|
132 |
<CreateGroupModal
|
|
|
133 |
isOpen={showGroupModal}
|
|
|
134 |
onClose={() => setShowGroupModal(false)}
|
|
|
135 |
/>
|
|
|
136 |
</>
|
|
|
137 |
)
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
export default ChatContacts
|