3719 |
stevensc |
1 |
import React, { useEffect, useState } from 'react';
|
|
|
2 |
import { connect } from 'react-redux';
|
|
|
3 |
import {
|
|
|
4 |
Avatar,
|
|
|
5 |
List,
|
|
|
6 |
ListItem,
|
|
|
7 |
ListItemAvatar,
|
|
|
8 |
ListItemButton,
|
|
|
9 |
ListItemText
|
|
|
10 |
} from '@mui/material';
|
|
|
11 |
|
|
|
12 |
import { useDebounce } from '@hooks';
|
|
|
13 |
import { getCurrentContacts, startConversation } from '@app/services/chats';
|
|
|
14 |
import { addNotification } from '@app/redux/notification/notification.actions';
|
|
|
15 |
|
|
|
16 |
import Modal from '../UI/modal/Modal';
|
|
|
17 |
import Input from '../UI/inputs/Input';
|
|
|
18 |
|
|
|
19 |
const ContactsModal = ({ show, onClose, onComplete, addNotification }) => {
|
|
|
20 |
const [search, setSearch] = useState('');
|
|
|
21 |
const debounceSearch = useDebounce(search, 500);
|
|
|
22 |
const [contacts, setContacts] = useState([]);
|
|
|
23 |
|
|
|
24 |
const closeModal = () => {
|
|
|
25 |
setContacts([]);
|
|
|
26 |
onClose();
|
|
|
27 |
};
|
|
|
28 |
|
|
|
29 |
const handleStartConversation = async (startConversationUrl) => {
|
|
|
30 |
try {
|
|
|
31 |
const conversation = await startConversation(startConversationUrl);
|
|
|
32 |
onComplete(conversation);
|
|
|
33 |
closeModal();
|
|
|
34 |
} catch (error) {
|
|
|
35 |
addNotification({ style: 'danger', msg: error.message });
|
|
|
36 |
}
|
|
|
37 |
};
|
|
|
38 |
|
|
|
39 |
useEffect(() => {
|
|
|
40 |
getCurrentContacts(debounceSearch)
|
|
|
41 |
.then((currentContacts) => setContacts(currentContacts))
|
|
|
42 |
.catch((error) => {
|
|
|
43 |
addNotification({ style: 'danger', msg: error.message });
|
|
|
44 |
});
|
|
|
45 |
}, [debounceSearch]);
|
|
|
46 |
|
|
|
47 |
return (
|
|
|
48 |
<Modal show={show} title='Iniciar conversación' onClose={closeModal} showFooter={false}>
|
|
|
49 |
<Input
|
|
|
50 |
label='Escribe el nombre'
|
|
|
51 |
placeholder='Escribe el nombre de la persona'
|
|
|
52 |
onChange={(e) => setSearch(e.target.value)}
|
|
|
53 |
/>
|
|
|
54 |
|
|
|
55 |
<List sx={{ width: '100%' }}>
|
|
|
56 |
{contacts.map((user) => {
|
|
|
57 |
const { image, name, link_open_or_create } = user;
|
|
|
58 |
|
|
|
59 |
return (
|
|
|
60 |
<ListItem key={name} disablePadding disableRipple>
|
|
|
61 |
<ListItemButton
|
|
|
62 |
disableRipple
|
|
|
63 |
onClick={() => handleStartConversation(link_open_or_create)}
|
|
|
64 |
>
|
|
|
65 |
<ListItemAvatar>
|
|
|
66 |
<Avatar alt={`${name} image`} src={image} />
|
|
|
67 |
</ListItemAvatar>
|
|
|
68 |
|
|
|
69 |
<ListItemText primary={name} />
|
|
|
70 |
</ListItemButton>
|
|
|
71 |
</ListItem>
|
|
|
72 |
);
|
|
|
73 |
})}
|
|
|
74 |
</List>
|
|
|
75 |
</Modal>
|
|
|
76 |
);
|
|
|
77 |
};
|
|
|
78 |
|
|
|
79 |
const mapDispatchToProps = {
|
|
|
80 |
addNotification: (notification) => addNotification(notification)
|
|
|
81 |
};
|
|
|
82 |
|
|
|
83 |
export default connect(null, mapDispatchToProps)(ContactsModal);
|