Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

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