Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7317 stevensc 1
import React, { useEffect, useState } from 'react'
2
import { axios, debounce } from '../../../utils'
3
import { Modal } from 'react-bootstrap'
4
import { useDispatch } from 'react-redux'
5
import { addNotification } from '../../../redux/notification/notification.actions'
6
import SendIcon from '@mui/icons-material/Send'
7
import Spinner from '../../UI/Spinner'
8
 
9
const ContactsModal = ({ show, onClose, onComplete }) => {
10
  const [isShow, setIsShow] = useState(show)
11
  const [contacts, setContacts] = useState([])
12
  const [loading, setLoading] = useState(false)
13
  const dispatch = useDispatch()
14
 
15
  const closeModal = () => {
16
    setIsShow(false)
17
    setContacts([])
18
    onClose()
19
  }
20
 
21
  const handleSearch = debounce((value) => getContacts(value), 500)
22
 
23
  const getContacts = (searchValue = '') => {
24
    setLoading(true)
25
    axios
26
      .get(`/chat/users?search=${searchValue.toLowerCase()}`)
27
      .then(({ data: response }) => {
28
        if (!response.success) {
29
          return dispatch(
30
            addNotification({ style: 'danger', msg: 'Ha ocurrido un error' })
31
          )
32
        }
33
        setContacts(response.data)
34
      })
35
      .finally(() => setLoading(false))
36
  }
37
 
38
  const startConversation = ({ link_open_or_create, link_send }) => {
39
    setLoading(true)
40
    axios
41
      .post(link_open_or_create)
42
      .then(({ data: response }) => {
43
        if (response.success) {
44
          closeModal()
45
          onComplete(link_send)
46
        }
47
      })
48
      .finally(() => setLoading(false))
49
  }
50
 
51
  useEffect(() => {
52
    setIsShow(show)
53
  }, [show])
54
 
55
  return (
56
    <Modal show={isShow} onHide={closeModal}>
57
      <Modal.Header closeButton>
58
        <h3 className="card-title font-weight-bold">Iniciar conversación</h3>
59
      </Modal.Header>
60
      <Modal.Body className="pb-3">
61
        <div className="form-group">
62
          <label htmlFor="search-people">Escribe el nombre</label>
63
          <input
64
            type="text"
65
            className="form-control"
66
            placeholder="Escribe el nombre de la persona"
67
            onChange={(e) => handleSearch(e.target.value)}
68
          />
69
        </div>
70
        {loading ? (
71
          <Spinner />
72
        ) : (
73
          <ul className="d-flex flex-column gap-3">
74
            {contacts.map((user) => (
75
              <li key={user.name}>
76
                <div className="d-flex align-items-center justify-content-between gap-3">
77
                  <div className="d-flex align-items-center gap-3">
78
                    <img
79
                      src={user.image}
80
                      alt={`User ${user.name} image`}
81
                      width={40}
82
                      height={40}
83
                      style={{ borderRadius: '50%' }}
84
                    />
85
                    <p>{user.name}</p>
86
                  </div>
87
 
88
                  <button
89
                    className="btn btn-primary"
90
                    onClick={() => startConversation(user)}
91
                  >
92
                    <SendIcon />
93
                  </button>
94
                </div>
95
              </li>
96
            ))}
97
          </ul>
98
        )}
99
      </Modal.Body>
100
    </Modal>
101
  )
102
}
103
 
104
export default ContactsModal