3719 |
stevensc |
1 |
import React, { useState, useEffect } from 'react';
|
|
|
2 |
import { useDispatch } from 'react-redux';
|
|
|
3 |
|
|
|
4 |
import { axios } from '@utils/index.js';
|
|
|
5 |
import { addNotification } from '../../redux/notification/notification.actions';
|
|
|
6 |
|
|
|
7 |
import Modal from '@components/UI/modal/Modal';
|
|
|
8 |
import Input from '../UI/inputs/Input';
|
|
|
9 |
|
|
|
10 |
const AddMemberModal = ({ isShow = false, linkInvite = '', handleClose = function () {} }) => {
|
|
|
11 |
const [users, setUsers] = useState([]);
|
|
|
12 |
const [search, setSearch] = useState('');
|
|
|
13 |
const dispatch = useDispatch();
|
|
|
14 |
|
|
|
15 |
const handleChange = ({ target }) => setSearch(target.value);
|
|
|
16 |
|
|
|
17 |
const searchMember = (url, search) => {
|
|
|
18 |
if (!url) return;
|
|
|
19 |
axios
|
|
|
20 |
.get(url + '?search=' + search)
|
|
|
21 |
.then((response) => {
|
|
|
22 |
const { data } = response.data;
|
|
|
23 |
setUsers(data);
|
|
|
24 |
})
|
|
|
25 |
.catch((err) => {
|
|
|
26 |
dispatch(addNotification({ style: 'danger', msg: err.message }));
|
|
|
27 |
});
|
|
|
28 |
};
|
|
|
29 |
|
|
|
30 |
const invite = (uuid) => {
|
|
|
31 |
const formData = new FormData();
|
|
|
32 |
formData.append('id', uuid);
|
|
|
33 |
axios
|
|
|
34 |
.post(linkInvite, formData)
|
|
|
35 |
.then((response) => {
|
|
|
36 |
const { data, success } = response.data;
|
|
|
37 |
|
|
|
38 |
if (!success) {
|
|
|
39 |
throw new Error(data);
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
dispatch(addNotification({ style: 'success', msg: data }));
|
|
|
43 |
handleClose();
|
|
|
44 |
})
|
|
|
45 |
.catch((err) => {
|
|
|
46 |
dispatch(addNotification({ style: 'danger', msg: err.message }));
|
|
|
47 |
});
|
|
|
48 |
};
|
|
|
49 |
|
|
|
50 |
useEffect(() => {
|
|
|
51 |
searchMember(linkInvite, search);
|
|
|
52 |
}, [search, linkInvite]);
|
|
|
53 |
|
|
|
54 |
return (
|
|
|
55 |
<Modal title='Agregar miembro' show={isShow} onClose={handleClose} showFooter={false}>
|
|
|
56 |
<Input name='search' placeholder='Escribe el nombre del usuario' onChange={handleChange} />
|
|
|
57 |
|
|
|
58 |
<ul className='d-flex flex-column w-100' style={{ gap: '1rem' }}>
|
|
|
59 |
{users.map((element, index) => (
|
|
|
60 |
<li key={index}>
|
|
|
61 |
<div
|
|
|
62 |
className='d-flex align-items-center justify-content-between flex-column flex-md-row'
|
|
|
63 |
style={{ gap: '.5rem' }}
|
|
|
64 |
>
|
|
|
65 |
<span>{element.text}</span>
|
|
|
66 |
<button className='btn btn-primary' onClick={() => invite(element.value)}>
|
|
|
67 |
Invitar
|
|
|
68 |
</button>
|
|
|
69 |
</div>
|
|
|
70 |
</li>
|
|
|
71 |
))}
|
|
|
72 |
</ul>
|
|
|
73 |
</Modal>
|
|
|
74 |
);
|
|
|
75 |
};
|
|
|
76 |
|
|
|
77 |
export default AddMemberModal;
|