Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6340 stevensc 1
/* eslint-disable react/prop-types */
2
import React, { useEffect, useState } from 'react'
3
import { axios } from '../../utils'
4
import DesktopChat from '../templates/DesktopChat'
5
import MobileChat from '../templates/MobileChat'
6
 
7
const notifyAudio = new Audio('/audio/chat.mp3')
8
 
9
const Page = ({ backendVars }) => {
10
  const { timezones } = backendVars
11
  const [chatUsers, setChatUsers] = useState([])
12
  const [chatGroups, setChatGroups] = useState([])
13
  const [activeChat, setActiveChat] = useState(null)
14
 
15
  const heartBeat = async () => {
16
    axios.get('/chat/heart-beat').then(({ data: response }) => {
17
      const { data, success } = response
18
      if (success) {
19
        const entities = data
20
        let updatedChatUsers = []
21
        let updatedChatGroups = []
22
 
23
        entities.map(async (entity) => {
24
          if (entity.not_received_messages) {
25
            handlePlayNewMessage(entity.url_mark_received)
26
          }
27
 
28
          if (entity.is_open) {
29
            manageOpenConversations(entity)
30
          }
31
 
32
          switch (entity.type) {
33
            case 'user':
34
              updatedChatUsers = [...updatedChatUsers, entity]
35
              break
36
            case 'group':
37
              updatedChatGroups = [...updatedChatGroups, entity]
38
              break
39
            default:
40
              break
41
          }
42
        })
43
        setChatUsers(updatedChatUsers)
44
        setChatGroups(updatedChatGroups)
45
      }
46
    })
47
  }
48
 
49
  const manageOpenConversations = (entity) => {
50
    if (activeChat) {
51
      axios.post(entity.url_close)
52
      return
53
    }
54
 
55
    setActiveChat(entity)
56
  }
57
 
58
  const handlePlayNewMessage = async (setReceivedLink) => {
59
    notifyAudio.play()
60
    const { data } = await axios.post(setReceivedLink)
61
    return data.success
62
  }
63
 
64
  useEffect(() => {
65
    const heartBeatInterval = setInterval(heartBeat, 3000)
66
    heartBeat()
67
    return () => {
68
      clearInterval(heartBeatInterval)
69
    }
70
  }, [])
71
 
72
  return (
73
    <>
74
      {window.innerWidth >= 768 ? (
75
        <DesktopChat
76
          chatUsers={chatUsers}
77
          chatGroups={chatGroups}
78
          timezones={timezones}
79
        />
80
      ) : (
81
        <MobileChat
82
          chatGroups={chatGroups}
83
          chatUsers={chatUsers}
84
          timezones={timezones}
85
        />
86
      )}
87
    </>
88
  )
89
}
90
 
91
export default Page