Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6990 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6956 stevensc 1
import React, { useCallback, useEffect, useRef, useState } from 'react'
2
import { axios } from '../../utils'
6981 stevensc 3
import { useParams } from 'react-router-dom'
6956 stevensc 4
import { addNotification } from '../../redux/notification/notification.actions'
5
import { Col, Container, Row } from 'react-bootstrap'
6981 stevensc 6
import { useDispatch, useSelector } from 'react-redux'
6956 stevensc 7
import QuestionAnswerRoundedIcon from '@mui/icons-material/QuestionAnswerRounded'
8
 
9
import EmptySection from '../../components/UI/EmptySection'
10
import InmailChat from '../../components/chat/InmailChat'
11
import Contacts from '../../components/chat/Contacts'
12
import StartConversationModal from '../../components/modals/StartConversationModal'
6991 stevensc 13
import SearchInput from '../../components/UI/SearchInput'
6956 stevensc 14
 
15
const InmailPage = () => {
16
  const [conversations, setConversations] = useState([])
17
  const [currentConversation, setCurrentConversation] = useState(null)
18
  const [search, setSearch] = useState('')
19
  const [loading, setLoading] = useState(false)
20
  const [showConversationModal, setShowConversationModal] = useState(false)
21
  const options = useRef([
22
    {
23
      label: 'Iniciar conversación',
24
      action: () => toggleConversationModal(),
25
    },
26
  ])
6968 stevensc 27
  const { uuid } = useParams()
6956 stevensc 28
  const labels = useSelector(({ intl }) => intl.labels)
29
  const dispatch = useDispatch()
30
 
31
  const getConversations = async () => {
6984 stevensc 32
    setLoading(true)
33
    axios
34
      .get(window.location.href)
35
      .then((response) => {
36
        const { data, success } = response.data
37
 
38
        if (!success) {
39
          return
40
        }
41
 
42
        const resConversations = data
43
 
6990 stevensc 44
        const diff1 = conversations.filter(
45
          ({ uuid }) => !resConversations.some((conv) => conv.uuid === uuid)
6984 stevensc 46
        )
6990 stevensc 47
        const diff2 = resConversations.filter(
48
          ({ uuid }) => !conversations.some((conv) => conv.uuid === uuid)
49
        )
6984 stevensc 50
 
6990 stevensc 51
        if (!diff1.concat(diff2).length) {
6984 stevensc 52
          return
53
        }
54
 
6987 stevensc 55
        setConversations(resConversations)
6984 stevensc 56
      })
57
      .catch((error) => {
58
        const errorMessage = new Error(error)
59
        dispatch(
60
          addNotification({ style: 'danger', msg: errorMessage.message })
61
        )
62
      })
63
      .finally(() => setLoading(false))
6956 stevensc 64
  }
65
 
66
  const changeConversation = (conversation) => {
67
    setCurrentConversation(conversation)
68
  }
69
 
70
  const filterConversations = useCallback(
71
    () =>
72
      conversations.filter((conversation) =>
73
        conversation.name.toLowerCase().includes(search.toLowerCase())
74
      ),
75
 
76
    [search, conversations]
77
  )
78
 
6990 stevensc 79
  const toggleConversationModal = () => {
80
    setShowConversationModal(!showConversationModal)
81
  }
82
 
6956 stevensc 83
  useEffect(() => {
84
    if (loading) return
85
 
86
    const messagesInterval = setTimeout(() => {
87
      getConversations()
88
    }, 2000)
89
 
90
    return () => {
91
      clearTimeout(messagesInterval)
92
    }
93
  }, [loading])
94
 
6968 stevensc 95
  useEffect(() => {
6985 stevensc 96
    if (uuid && !currentConversation) {
6968 stevensc 97
      const conversation = conversations.find((c) => c.uuid === uuid)
6980 stevensc 98
      conversation && setCurrentConversation(conversation)
6968 stevensc 99
    }
6980 stevensc 100
  }, [conversations, uuid])
6968 stevensc 101
 
6956 stevensc 102
  return (
103
    <>
104
      <Container>
105
        <Row>
6976 stevensc 106
          <Col
107
            md="4"
6977 stevensc 108
            className={currentConversation ? 'd-none d-md-flex' : 'd-flex'}
6976 stevensc 109
          >
6956 stevensc 110
            <Contacts>
6959 stevensc 111
              <Contacts.Header options={options.current}>
112
                <h1>Inmail</h1>
6956 stevensc 113
              </Contacts.Header>
114
 
6991 stevensc 115
              <SearchInput onChange={(e) => setSearch(e.target.value)} />
6956 stevensc 116
 
117
              <Contacts.List
118
                contacts={filterConversations()}
119
                onChange={changeConversation}
120
                currentConversation={currentConversation}
121
              />
122
            </Contacts>
123
          </Col>
6976 stevensc 124
          <Col
125
            md="8"
6977 stevensc 126
            className={currentConversation ? 'd-flex' : 'd-none d-md-flex'}
6976 stevensc 127
          >
6956 stevensc 128
            {currentConversation ? (
129
              <InmailChat
130
                conversation={currentConversation}
131
                changeConversation={changeConversation}
132
              />
133
            ) : (
134
              <EmptySection
135
                message={labels.select_conversation}
136
                Icon={<QuestionAnswerRoundedIcon />}
137
                align="center"
138
              />
139
            )}
140
          </Col>
141
        </Row>
142
      </Container>
143
      <StartConversationModal
144
        show={showConversationModal}
145
        setConversation={changeConversation}
146
        onClose={toggleConversationModal}
147
      />
148
    </>
149
  )
150
}
151
 
152
export default InmailPage