Proyectos de Subversion LeadersLinked - SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3610 stevensc 1
import { useEffect, useState } from 'react';
2
import { useParams } from 'react-router-dom';
3
 
4
import { useAlert, useAlertModal, useApi } from '@shared/hooks';
5
import {
6
  getConversations,
7
  deleteConversation as deleteConversationService
8
} from '@inmail/services';
9
 
10
export function useConversations() {
11
  const { uuid } = useParams();
12
  const [conversations, setConversations] = useState([]);
13
  const [currentConversation, setCurrentConversation] = useState(null);
14
 
15
  const { showSuccess, showError } = useAlert();
16
  const { showAlert, closeAlert } = useAlertModal();
17
 
18
  const { loading, execute } = useApi(getConversations, {
19
    onSuccess: (data) => {
20
      setConversations(data);
21
    },
22
    onError: (error) => {
23
      showError(error.message);
24
    }
25
  });
26
 
3628 stevensc 27
  const { execute: executeDeleteConversation } = useApi(deleteConversationService, {
28
    onError: (error) => {
29
      showError(error.message);
30
    }
31
  });
32
 
3610 stevensc 33
  const handleDeleteConversation = async (url) => {
3628 stevensc 34
    executeDeleteConversation(url).then((data) => {
35
      showSuccess(data);
3610 stevensc 36
      setConversations((prev) => prev.filter((c) => c.uuid !== url));
3628 stevensc 37
    });
3610 stevensc 38
  };
39
 
40
  const deleteConversation = (url) => {
41
    showAlert({
42
      title: 'Borrar conversación',
43
      message: '¿Estás seguro de querer borrar esta conversación?',
44
      onConfirm: () => handleDeleteConversation(url),
45
      onCancel: closeAlert
46
    });
47
  };
48
 
49
  useEffect(() => {
50
    if (!uuid) return;
51
    const conversation = conversations.find((c) => c.uuid === uuid);
52
    setCurrentConversation(conversation);
53
  }, [uuid, conversations]);
54
 
55
  useEffect(() => {
3625 stevensc 56
    execute(uuid ? `/inmail/${uuid}` : '/inmail');
3620 stevensc 57
  }, [uuid]);
3610 stevensc 58
 
59
  return {
60
    conversations,
61
    loading,
62
    currentConversation,
63
    setCurrentConversation,
64
    deleteConversation
65
  };
66
}