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'
1684 stevensc 2
import { useDispatch } from 'react-redux'
3
 
5 stevensc 4
import { axios, scrollToBottom } from '../../utils'
5
import { addNotification } from '../../redux/notification/notification.actions'
1684 stevensc 6
import useMessages from '../../hooks/useMessages'
5 stevensc 7
 
8
import Chat from './Chat'
1684 stevensc 9
import MessagesList from './messages-list'
5 stevensc 10
import ConferenceModal from '../modals/ConferenceModal'
11
import ConfirmModal from '../modals/ConfirmModal'
12
 
13
const CHAT_TABS = {
14
  CHAT: 'CHAT',
15
  DEFAULT: 'DEFAULT',
16
  GROUP_MEMBERS: 'GROUP_MEMBERS',
1668 stevensc 17
  ADD_GROUP_MEMBER: 'ADD_GROUP_MEMBER'
5 stevensc 18
}
19
 
523 stevensc 20
const ChatBox = ({ activeTab, entity, changeTab, changeConversation }) => {
5 stevensc 21
  const {
22
    url_get_all_messages,
23
    url_mark_received,
24
    not_received_messages,
25
    not_seen_messages,
26
    url_zoom,
27
    url_send,
28
    url_upload,
29
    url_mark_seen,
30
    // url_close,
31
    url_add_user_to_group, // Group url
32
    url_delete, // Group url
33
    url_get_contact_group_list, // Group url
34
    url_leave, // Group url
35
    name,
1668 stevensc 36
    profile
5 stevensc 37
  } = entity
38
 
1677 stevensc 39
  const { messages, loadMore, loading, report } =
40
    useMessages(url_get_all_messages)
5 stevensc 41
  const [isShowConferenceModal, setisShowConferenceModal] = useState(false)
42
  const [isShowConfirmModal, setisShowConfirmModal] = useState(false)
43
  const [options, setOptions] = useState([])
44
  const scrollRef = useRef(null)
45
  const actionUrl = useRef('')
46
  const dispatch = useDispatch()
47
 
48
  const toggleConfirmModal = () => {
49
    setisShowConfirmModal(!isShowConfirmModal)
50
  }
51
 
52
  const toggleConferenceModal = () => {
53
    setisShowConferenceModal(!isShowConferenceModal)
54
  }
55
 
56
  const deleteGroup = async (url) => {
57
    axios.post(url).then(({ data: response }) => {
58
      const { data, success } = response
59
      if (!success) {
60
        const errorMessage =
61
          typeof data.data === 'string' ? data.data : 'Ha ocurrido un error'
62
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
63
        return
64
      }
65
 
66
      toggleConfirmModal()
67
      changeConversation(null)
68
      changeTab(CHAT_TABS.DEFAULT)
69
    })
70
  }
71
 
72
  useEffect(() => {
73
    if (not_seen_messages) axios.post(url_mark_seen)
74
    if (not_received_messages) axios.post(url_mark_received)
75
    if (options.length) setOptions([])
76
  }, [entity])
77
 
78
  useEffect(() => {
79
    if (url_leave) {
80
      setOptions((prevOptions) =>
81
        prevOptions.concat({
82
          url: url_leave,
83
          label: 'Dejar Grupo',
84
          action: () => {
85
            toggleConfirmModal()
86
            actionUrl.current = url_leave
1668 stevensc 87
          }
5 stevensc 88
        })
89
      )
90
    }
91
    if (url_delete) {
92
      setOptions((prevOptions) =>
93
        prevOptions.concat({
94
          url: url_delete,
95
          label: 'Eliminar Grupo',
96
          action: () => {
97
            toggleConfirmModal()
98
            actionUrl.current = url_delete
1668 stevensc 99
          }
5 stevensc 100
        })
101
      )
102
    }
103
    if (url_get_contact_group_list) {
104
      setOptions((prevOptions) =>
105
        prevOptions.concat({
106
          url: url_get_contact_group_list,
107
          label: 'Integrantes',
1668 stevensc 108
          action: () => changeTab(CHAT_TABS.GROUP_MEMBERS)
5 stevensc 109
        })
110
      )
111
    }
112
    if (url_add_user_to_group) {
113
      setOptions((prevOptions) =>
114
        prevOptions.concat({
115
          url: url_add_user_to_group,
116
          label: 'Agregar Contactos',
1668 stevensc 117
          action: () => changeTab(CHAT_TABS.ADD_GROUP_MEMBER)
5 stevensc 118
        })
119
      )
120
    }
121
    if (url_zoom) {
122
      setOptions((prevOptions) =>
123
        prevOptions.concat({
124
          url: url_zoom,
125
          label: 'Crear Conferencia',
1668 stevensc 126
          action: toggleConferenceModal
5 stevensc 127
        })
128
      )
129
    }
130
  }, [entity])
131
 
132
  return (
133
    <>
134
      <Chat>
135
        <Chat.Header
523 stevensc 136
          onClose={() =>
137
            activeTab === CHAT_TABS.DEFAULT
138
              ? changeConversation(null)
139
              : changeTab(CHAT_TABS.DEFAULT)
140
          }
5 stevensc 141
          options={options}
142
        >
143
          <Chat.Title url={profile}>{name}</Chat.Title>
144
        </Chat.Header>
145
 
1684 stevensc 146
        <MessagesList
1677 stevensc 147
          messages={messages}
148
          onPagination={loadMore}
149
          loading={loading}
150
          scrollRef={scrollRef}
151
          onReport={report}
152
        />
5 stevensc 153
 
154
        <Chat.SubmitForm
155
          sendUrl={url_send}
156
          uploadUrl={url_upload}
157
          onSubmit={() => scrollToBottom(scrollRef)}
158
        />
159
      </Chat>
1684 stevensc 160
 
5 stevensc 161
      <ConferenceModal
162
        show={isShowConferenceModal}
163
        zoomUrl={url_zoom}
164
        onClose={toggleConferenceModal}
165
      />
1684 stevensc 166
 
5 stevensc 167
      <ConfirmModal
168
        show={isShowConfirmModal}
169
        onClose={toggleConfirmModal}
170
        onAccept={() => deleteGroup(actionUrl.current)}
171
      />
172
    </>
173
  )
174
}
175
 
176
export default ChatBox