Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5359 | Rev 5867 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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