Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6977 | | 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()
6978 stevensc 43
  }, [conversation])
6956 stevensc 44
 
6978 stevensc 45
  useEffect(() => {
46
    const opt = []
6956 stevensc 47
    if (conversation.delete_link) {
6978 stevensc 48
      opt.push({ label: 'Borrar convesación', action: toggleConfirmModal })
6956 stevensc 49
    }
6978 stevensc 50
 
51
    options.current = opt
6956 stevensc 52
  }, [conversation])
53
 
54
  return (
55
    <>
56
      <Chat>
6977 stevensc 57
        <Chat.Header
58
          options={options.current}
59
          onClose={() => changeConversation(null)}
60
        >
6961 stevensc 61
          <Chat.Title url={conversation.profile}>
62
            {conversation.name}
63
          </Chat.Title>
6956 stevensc 64
        </Chat.Header>
65
        {!messages.length ? (
66
          <EmptySection
67
            Icon={<InboxIcon />}
68
            message="No hay mensajes en esta conversación"
69
            align="center"
70
          />
71
        ) : (
72
          <Chat.List
73
            messages={messages}
74
            onPagination={loadMore}
75
            loading={loading}
76
            scrollRef={messagesList}
77
          />
78
        )}
79
        <Chat.SubmitForm
80
          sendUrl={conversation.send_link}
81
          uploadUrl={conversation.send_link}
82
          onSubmit={() => scrollToBottom(messagesList)}
83
        />
84
      </Chat>
85
      <ConfirmModal
86
        show={isShowConfirm}
87
        onClose={toggleConfirmModal}
88
        onAccept={deleteConversation}
89
      />
90
    </>
91
  )
92
}
93
 
94
export default InmailChat