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
 
27
  const handleDeleteConversation = async (url) => {
28
    try {
29
      const message = await deleteConversationService(url);
30
      showSuccess(message);
31
      setConversations((prev) => prev.filter((c) => c.uuid !== url));
32
    } catch (error) {
33
      showError(error.message);
34
    }
35
  };
36
 
37
  const deleteConversation = (url) => {
38
    showAlert({
39
      title: 'Borrar conversación',
40
      message: '¿Estás seguro de querer borrar esta conversación?',
41
      onConfirm: () => handleDeleteConversation(url),
42
      onCancel: closeAlert
43
    });
44
  };
45
 
46
  useEffect(() => {
47
    if (!uuid) return;
48
    const conversation = conversations.find((c) => c.uuid === uuid);
49
    setCurrentConversation(conversation);
50
  }, [uuid, conversations]);
51
 
52
  useEffect(() => {
3619 stevensc 53
    execute(uuid ? `/email/${uuid}` : '/email');
3620 stevensc 54
  }, [uuid]);
3610 stevensc 55
 
56
  return {
57
    conversations,
58
    loading,
59
    currentConversation,
60
    setCurrentConversation,
61
    deleteConversation
62
  };
63
}