Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
6956 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'
11
 
12
const InmailChat = ({ conversation, changeConversation }) => {
13
  const [isShowConfirm, setIsShowConfirm] = useState(false)
14
  const messagesList = useRef(null)
15
  const options = useRef([])
16
  const dispatch = useDispatch()
17
 
18
  const { messages, loadMore, loading, reset } = useMessages(
19
    conversation?.messages_link
20
  )
21
 
22
  const deleteConversation = () => {
23
    axios.post(conversation.delete_link).then(({ data: response }) => {
24
      const { success, data } = response
25
 
26
      if (!success) {
27
        dispatch(addNotification({ style: 'danger', msg: data }))
28
        return
29
      }
30
 
31
      dispatch(addNotification({ style: 'success', msg: data }))
32
      toggleConfirmModal()
33
      changeConversation(null)
34
    })
35
  }
36
 
37
  const toggleConfirmModal = () => {
38
    setIsShowConfirm(!isShowConfirm)
39
  }
40
 
41
  useEffect(() => {
42
    reset()
43
 
44
    if (conversation.delete_link) {
45
      options.current.push({
46
        label: 'Borrar convesación',
47
        action: toggleConfirmModal,
48
      })
49
    }
50
  }, [conversation])
51
 
52
  return (
53
    <>
54
      <Chat>
6977 stevensc 55
        <Chat.Header
56
          options={options.current}
57
          onClose={() => changeConversation(null)}
58
        >
6961 stevensc 59
          <Chat.Title url={conversation.profile}>
60
            {conversation.name}
61
          </Chat.Title>
6956 stevensc 62
        </Chat.Header>
63
        {!messages.length ? (
64
          <EmptySection
65
            Icon={<InboxIcon />}
66
            message="No hay mensajes en esta conversación"
67
            align="center"
68
          />
69
        ) : (
70
          <Chat.List
71
            messages={messages}
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