5033 |
stevensc |
1 |
/* eslint-disable react/prop-types */
|
|
|
2 |
import React, { useEffect, useState } from 'react'
|
|
|
3 |
import { axios } from '../../../utils'
|
|
|
4 |
import { Modal } from 'react-bootstrap'
|
|
|
5 |
import { useDispatch } from 'react-redux'
|
|
|
6 |
import { addNotification } from '../../../redux/notification/notification.actions'
|
|
|
7 |
import SendIcon from '@mui/icons-material/Send'
|
|
|
8 |
|
|
|
9 |
// Components
|
|
|
10 |
import Spinner from '../../../shared/loading-spinner/Spinner'
|
|
|
11 |
|
|
|
12 |
const ContactsModal = ({ show, setConversation }) => {
|
|
|
13 |
let axiosThrottle = null
|
|
|
14 |
const [isShow, setIsShow] = useState(show)
|
|
|
15 |
const [searchResult, setSearchResult] = useState({})
|
|
|
16 |
const [loading, setLoading] = useState(false)
|
|
|
17 |
const dispatch = useDispatch()
|
|
|
18 |
|
|
|
19 |
const closeModal = () => {
|
|
|
20 |
setIsShow(false)
|
|
|
21 |
setSearchResult({})
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
const handleSearch = (contact) => {
|
|
|
25 |
clearTimeout(axiosThrottle)
|
|
|
26 |
axiosThrottle = setTimeout(() => {
|
|
|
27 |
fetchContacts(contact)
|
|
|
28 |
}, 500);
|
|
|
29 |
};
|
|
|
30 |
|
|
|
31 |
const fetchContacts = async (searchParam = '') => {
|
|
|
32 |
setLoading(true)
|
|
|
33 |
await axios.get(`/chat/users?search=${searchParam.toLowerCase()}`)
|
|
|
34 |
.then(({ data: response }) => {
|
|
|
35 |
if (!response.success) return dispatch(addNotification({ style: 'danger', msg: 'Ha ocurrido un error' }))
|
|
|
36 |
setSearchResult(response.data)
|
|
|
37 |
})
|
|
|
38 |
.finally(() => setLoading(false))
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
const startConversation = async (send_url = '', name = '') => {
|
|
|
42 |
const formData = new FormData()
|
|
|
43 |
const fullName = name.split(' ')
|
|
|
44 |
formData.append("message", `Hola, ${fullName[0]}`)
|
|
|
45 |
|
|
|
46 |
setLoading(true)
|
|
|
47 |
await axios
|
|
|
48 |
.post(send_url, formData)
|
|
|
49 |
.then(({ data: response }) => {
|
|
|
50 |
if (response.success) {
|
|
|
51 |
setConversation(send_url)
|
|
|
52 |
closeModal()
|
|
|
53 |
}
|
|
|
54 |
})
|
|
|
55 |
.finally(() => setLoading(false))
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
useEffect(() => {
|
|
|
59 |
setIsShow(show)
|
|
|
60 |
}, [show])
|
|
|
61 |
|
|
|
62 |
return (
|
|
|
63 |
<Modal show={isShow} onHide={closeModal}>
|
|
|
64 |
<Modal.Header closeButton>
|
|
|
65 |
<h3 className="card-title font-weight-bold">Iniciar conversación</h3>
|
|
|
66 |
</Modal.Header>
|
|
|
67 |
<div className="form-group">
|
|
|
68 |
<label htmlFor="search-people">Escribe el nombre</label>
|
|
|
69 |
<input type="text" className="form-control" placeholder="Escribe el nombre de la persona" onChange={(e) => handleSearch(e.target.value)} />
|
|
|
70 |
</div>
|
|
|
71 |
<div className='container'>
|
|
|
72 |
{loading
|
|
|
73 |
? <Spinner />
|
|
|
74 |
: Object.entries(searchResult).map(([key, value]) => {
|
|
|
75 |
return (
|
|
|
76 |
<div className='row' key={key}>
|
|
|
77 |
<div className='col-8'>
|
|
|
78 |
<p>{value}</p>
|
|
|
79 |
</div>
|
|
|
80 |
<div className='col-4'>
|
|
|
81 |
<button className='btn btn-primary' onClick={() => startConversation(key, value)}>
|
|
|
82 |
<SendIcon />
|
|
|
83 |
</button>
|
|
|
84 |
</div>
|
|
|
85 |
</div>
|
|
|
86 |
)
|
|
|
87 |
})}
|
|
|
88 |
</div>
|
|
|
89 |
</Modal >
|
|
|
90 |
)
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
export default ContactsModal
|