Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 3208 Rev 3520
Línea 1... Línea 1...
1
import { useEffect, useState } from 'react'
1
import { useEffect, useState } from 'react';
2
import { useDispatch } from 'react-redux'
2
import { useDispatch } from 'react-redux';
Línea 3... Línea 3...
3
 
3
 
4
import { getConversations } from '@app/services/chats'
4
import { getConversations } from '@app/services/chats';
Línea 5... Línea 5...
5
import { addNotification } from '@app/redux/notification/notification.actions'
5
import { addNotification } from '@app/redux/notification/notification.actions';
6
 
6
 
7
export function useConversations(url) {
7
export function useConversations(url) {
8
  const [conversations, setConversations] = useState([])
8
  const [conversations, setConversations] = useState([]);
9
  const [currentConversation, setCurrentConversation] = useState(null)
9
  const [currentConversation, setCurrentConversation] = useState(null);
Línea 10... Línea 10...
10
  const [loading, setLoading] = useState(false)
10
  const [loading, setLoading] = useState(false);
11
  const dispatch = useDispatch()
11
  const dispatch = useDispatch();
12
 
12
 
13
  const heartBeat = async (url) => {
13
  const heartBeat = async (url) => {
Línea 14... Línea 14...
14
    setLoading(true)
14
    setLoading(true);
15
    try {
15
    try {
16
      const results = await getConversations(url)
16
      const results = await getConversations(url);
17
 
17
 
18
      if (JSON.stringify(conversations) !== JSON.stringify(results)) {
18
      if (JSON.stringify(conversations) !== JSON.stringify(results)) {
19
        setConversations(results)
19
        setConversations(results);
20
      }
20
      }
21
    } catch (error) {
21
    } catch (error) {
22
      dispatch(addNotification({ style: 'danger', msg: error.message }))
22
      dispatch(addNotification({ style: 'danger', msg: error.message }));
Línea 23... Línea 23...
23
    } finally {
23
    } finally {
24
      setLoading(false)
24
      setLoading(false);
25
    }
25
    }
Línea 26... Línea 26...
26
  }
26
  };
27
 
27
 
28
  const setConversation = (conversation) => {
28
  const setConversation = (conversation) => {
29
    setCurrentConversation(conversation)
29
    setCurrentConversation(conversation);
Línea 30... Línea 30...
30
  }
30
  };
31
 
31
 
32
  useEffect(() => {
32
  useEffect(() => {
33
    const conversationsInterval = setInterval(() => {
33
    const conversationsInterval = setInterval(() => {
Línea 34... Línea 34...
34
      heartBeat(url)
34
      heartBeat(url);
35
    }, 2000)
35
    }, 2000);
36
 
36
 
37
    return () => {
37
    return () => {
38
      clearInterval(conversationsInterval)
38
      clearInterval(conversationsInterval);
39
    }
39
    };
40
  }, [url, conversations])
40
  }, [url, conversations]);