Proyectos de Subversion LeadersLinked - SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5 stevensc 1
import React, { useEffect, useRef, useState } from 'react'
2
import { axios, scrollToBottom } from '../../utils'
3
import { useDispatch } from 'react-redux'
4
import { addNotification } from '../../redux/notification/notification.actions'
5
import useMessages from '../../hooks/useMessages'
6
import InboxIcon from '@mui/icons-material/Inbox'
7
 
8
import Chat from './Chat'
9
import EmptySection from '../UI/EmptySection'
10
import ConfirmModal from '../modals/ConfirmModal'
1710 stevensc 11
import MessagesList from './messages-list'
5 stevensc 12
 
13
const InmailChat = ({ conversation, changeConversation }) => {
14
  const [isShowConfirm, setIsShowConfirm] = useState(false)
15
  const messagesList = useRef(null)
16
  const options = useRef([])
17
  const dispatch = useDispatch()
18
 
1710 stevensc 19
  const { messages, loadMore, loading, report } = useMessages(
5 stevensc 20
    conversation?.messages_link
21
  )
22
 
23
  const deleteConversation = () => {
24
    axios.post(conversation.delete_link).then(({ data: response }) => {
25
      const { success, data } = response
26
 
27
      if (!success) {
28
        dispatch(addNotification({ style: 'danger', msg: data }))
29
        return
30
      }
31
 
32
      dispatch(addNotification({ style: 'success', msg: data }))
33
      toggleConfirmModal()
34
      changeConversation(null)
35
    })
36
  }
37
 
38
  const toggleConfirmModal = () => {
39
    setIsShowConfirm(!isShowConfirm)
40
  }
41
 
42
  useEffect(() => {
43
    const opt = []
44
    if (conversation.delete_link) {
45
      opt.push({ label: 'Borrar convesación', action: toggleConfirmModal })
46
    }
47
 
48
    options.current = opt
49
  }, [conversation])
50
 
51
  return (
52
    <>
53
      <Chat>
54
        <Chat.Header
55
          options={options.current}
56
          onClose={() => changeConversation(null)}
57
        >
58
          <Chat.Title url={conversation.profile}>
59
            {conversation.name}
60
          </Chat.Title>
61
        </Chat.Header>
62
        {!messages.length ? (
63
          <EmptySection
64
            Icon={<InboxIcon />}
1710 stevensc 65
            message='No hay mensajes en esta conversación'
66
            align='center'
5 stevensc 67
          />
68
        ) : (
1710 stevensc 69
          <MessagesList
5 stevensc 70
            messages={messages}
1710 stevensc 71
            onReport={report}
5 stevensc 72
            onPagination={loadMore}
73
            loading={loading}
74
            scrollRef={messagesList}
75
          />
76
        )}
77
        <Chat.SubmitForm
78
          sendUrl={conversation.send_link}
79
          uploadUrl={conversation.send_link}
80
          onSubmit={() => scrollToBottom(messagesList)}
81
        />
82
      </Chat>
83
      <ConfirmModal
84
        show={isShowConfirm}
85
        onClose={toggleConfirmModal}
86
        onAccept={deleteConversation}
87
      />
88
    </>
89
  )
90
}
91
 
92
export default InmailChat