Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 11350 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11350 Rev 15761
Línea -... Línea 1...
-
 
1
/* eslint-disable react/prop-types */
1
import {axios} from "../../utils";
2
import { axios } from '../../utils'
2
import React, { useEffect, useRef } from "react";
3
import React, { useEffect, useRef } from 'react'
3
import { connect } from "react-redux";
4
import { connect } from 'react-redux'
4
import {
5
import {
5
  setHaveNewMessage,
6
  setHaveNewMessage,
6
  setUnreadMessages,
7
  setUnreadMessages,
7
  setShouldAlertNewMessage,
8
  setShouldAlertNewMessage
8
} from "../../redux/chat/chat.actions";
9
} from '../../redux/chat/chat.actions'
Línea 9... Línea 10...
9
 
10
 
Línea 10... Línea 11...
10
const notifyAudio = new Audio("/audio/chat.mp3");
11
const notifyAudio = new Audio('/audio/chat.mp3')
11
 
-
 
12
const ChatHelper = (props) => {
12
 
13
  // redux destructuring
13
const ChatHelper = (props) => {
14
  // redux states
14
  // redux states
15
  const { unreadMessages, shouldAlertNewMessage } = props;
15
  const { unreadMessages, shouldAlertNewMessage } = props
16
  // redux actions
16
  // redux actions
17
  const { setUnreadMessages, setShouldAlertNewMessage } = props;
17
  const { setUnreadMessages, setShouldAlertNewMessage } = props
18
  // refs
-
 
Línea 19... Línea 18...
19
  const unreads = useRef([]);
18
  // refs
20
  const shouldNotify = useRef(false);
19
  const unreads = useRef([])
21
 
20
 
22
  useEffect(() => {
21
  useEffect(() => {
23
    clearInterval(checkChatInterval);
22
    clearInterval(checkChatInterval)
24
    const checkChatInterval = setInterval(() => {
23
    const checkChatInterval = setInterval(() => {
25
      axios.get("/chat/heart-beat").then((response) => {
24
      axios.get('/chat/heart-beat').then((response) => {
26
        const resData = response.data;
25
        const resData = response.data
27
        if (resData.success) {
26
        if (resData.success) {
28
          resData.data.map((entity) => {
27
          resData.data.map((entity) => {
29
            handleNewMessage(entity);
28
            handleNewMessage(entity)
30
          });
29
          })
31
        }
30
        }
32
      });
31
      })
33
    }, 1000);
32
    }, 1000)
34
    return () => {
33
    return () => {
Línea 35... Línea 34...
35
      clearInterval(checkChatInterval);
34
      clearInterval(checkChatInterval)
36
    };
35
    }
37
  }, [unreadMessages, setShouldAlertNewMessage]);
36
  }, [unreadMessages, setShouldAlertNewMessage])
38
 
37
 
39
  const handleNewMessage = async (entity) => {
38
  const handleNewMessage = async (entity) => {
40
    const existingUnread = unreads.current.findIndex(
39
    const existingUnread = unreads.current.findIndex(
41
      (unread) => unread === entity.id
40
      (unread) => unread === entity.id
42
    );
41
    )
43
    if (entity.not_received_messages) {
42
    if (entity.not_received_messages) {
44
      const resData = (await axios.post(entity.url_mark_received)).data;
43
      const resData = (await axios.post(entity.url_mark_received)).data
45
      notifyAudio.play();
44
      notifyAudio.play()
46
    }
45
    }
47
    if (entity.not_seen_messages) {
46
    if (entity.not_seen_messages) {
48
      if (existingUnread === -1) {
47
      if (existingUnread === -1) {
49
        unreads.current = [...unreads.current, entity.id];
48
        unreads.current = [...unreads.current, entity.id]
50
      }
49
      }
51
    } else {
50
    } else {
52
      if (existingUnread !== -1) {
51
      if (existingUnread !== -1) {
53
        unreads.current = unreads.current.filter(
52
        unreads.current = unreads.current.filter(
54
          (unread) => unread !== entity.id
53
          (unread) => unread !== entity.id
55
        );
54
        )
56
      }
55
      }
57
    }
56
    }
58
    // redux new message
57
    // redux new message
Línea 59... Línea 58...
59
    if (JSON.stringify(unreadMessages) !== JSON.stringify(unreads.current)) {
58
    if (JSON.stringify(unreadMessages) !== JSON.stringify(unreads.current)) {
60
      setUnreadMessages(unreads.current);
59
      setUnreadMessages(unreads.current)
Línea 61... Línea 60...
61
    }
60
    }
62
  };
61
  }
63
 
62
 
64
  return <div></div>;
63
  return <div></div>
Línea 65... Línea 64...
65
};
64
}
66
 
65
 
67
const mapStateToProps = (state) => ({
66
const mapStateToProps = (state) => ({
68
  unreadMessages: state.chat.unreadMessages,
67
  unreadMessages: state.chat.unreadMessages,
69
  shouldAlertNewMessage: state.chat.shouldAlertNewMessage,
68
  shouldAlertNewMessage: state.chat.shouldAlertNewMessage
70
});
69
})
Línea 71... Línea 70...
71
 
70